Skip to content

Instantly share code, notes, and snippets.

@Dobiasd
Last active December 28, 2020 12:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Dobiasd/3140cfd9f539b6adb346e0b4a0ce157b to your computer and use it in GitHub Desktop.
Save Dobiasd/3140cfd9f539b6adb346e0b4a0ce157b to your computer and use it in GitHub Desktop.
// Example code for how to:
// - load an image using OpenCV
// - convert it to a fdeep::tensor3
// - use it as input for a forward pass on an image classification model
// - print the class number
// compile with:
// g++ -std=c++14 -O3 opencv_example.cpp -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -o opencv_example
#include <fdeep/fdeep.hpp>
#include <opencv2/opencv.hpp>
int main()
{
const cv::Mat image = cv::imread("image.jpg");
cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
assert(image.isContinuous());
const auto model = fdeep::load_model("model.json");
// Use the correct scaling, i.e. low and high.
const auto input = fdeep::tensor5_from_bytes(image.ptr(),
static_cast<std::size_t>(image.rows),
static_cast<std::size_t>(image.cols),
static_cast<std::size_t>(image.channels()),
0.0f, 1.0f);
const auto result = model.predict_class({input});
std::cout << result << std::endl;
}
@kshitij-netskope
Copy link

Hi Dobias,
Thanks for this snippet. This is similar to what I am looking for. Although, I have a different question, I was not able to get OpenCV working in my environment with Keras. Can you point me any documentation that would help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment