oak_small_hetero_pipeline.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <opencv2/gapi.hpp>
  2. #include <opencv2/gapi/core.hpp>
  3. #include <opencv2/gapi/cpu/core.hpp>
  4. #include <opencv2/gapi/gframe.hpp>
  5. #include <opencv2/gapi/media.hpp>
  6. #include <opencv2/gapi/oak/oak.hpp>
  7. #include <opencv2/gapi/streaming/format.hpp> // BGR accessor
  8. #include <opencv2/highgui.hpp> // CommandLineParser
  9. const std::string keys =
  10. "{ h help | | Print this help message }"
  11. "{ output | output.png | Path to the output file }";
  12. int main(int argc, char *argv[]) {
  13. cv::CommandLineParser cmd(argc, argv, keys);
  14. if (cmd.has("help")) {
  15. cmd.printMessage();
  16. return 0;
  17. }
  18. const std::string output_name = cmd.get<std::string>("output");
  19. std::vector<int> h = {1, 0, -1,
  20. 2, 0, -2,
  21. 1, 0, -1};
  22. std::vector<int> v = { 1, 2, 1,
  23. 0, 0, 0,
  24. -1, -2, -1};
  25. cv::Mat hk(3, 3, CV_32SC1, h.data());
  26. cv::Mat vk(3, 3, CV_32SC1, v.data());
  27. // Heterogeneous pipeline:
  28. // OAK camera -> Sobel -> streaming accessor (CPU)
  29. cv::GFrame in;
  30. cv::GFrame sobel = cv::gapi::oak::sobelXY(in, hk, vk);
  31. // Default camera and then sobel work only with nv12 format
  32. cv::GMat out = cv::gapi::streaming::Y(sobel);
  33. auto args = cv::compile_args(cv::gapi::oak::ColorCameraParams{},
  34. cv::gapi::oak::kernels());
  35. auto pipeline = cv::GComputation(cv::GIn(in), cv::GOut(out)).compileStreaming(std::move(args));
  36. // Graph execution /////////////////////////////////////////////////////////
  37. cv::Mat out_mat(1920, 1080, CV_8UC1);
  38. pipeline.setSource(cv::gapi::wip::make_src<cv::gapi::oak::ColorCamera>());
  39. pipeline.start();
  40. // pull 1 frame
  41. pipeline.pull(cv::gout(out_mat));
  42. cv::imwrite(output_name, out_mat);
  43. std::cout << "Pipeline finished: " << output_name << " file has been written." << std::endl;
  44. }