oak_small_hetero_pipeline.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #ifdef HAVE_OAK
  13. int main(int argc, char *argv[]) {
  14. cv::CommandLineParser cmd(argc, argv, keys);
  15. if (cmd.has("help")) {
  16. cmd.printMessage();
  17. return 0;
  18. }
  19. const std::string output_name = cmd.get<std::string>("output");
  20. std::vector<int> h = {1, 0, -1,
  21. 2, 0, -2,
  22. 1, 0, -1};
  23. std::vector<int> v = { 1, 2, 1,
  24. 0, 0, 0,
  25. -1, -2, -1};
  26. cv::Mat hk(3, 3, CV_32SC1, h.data());
  27. cv::Mat vk(3, 3, CV_32SC1, v.data());
  28. // Heterogeneous pipeline:
  29. // OAK camera -> Sobel -> streaming accessor (CPU)
  30. cv::GFrame in;
  31. cv::GFrame sobel = cv::gapi::oak::sobelXY(in, hk, vk);
  32. // Default camera and then sobel work only with nv12 format
  33. cv::GMat out = cv::gapi::streaming::Y(sobel);
  34. auto args = cv::compile_args(cv::gapi::oak::ColorCameraParams{},
  35. cv::gapi::oak::kernels());
  36. auto pipeline = cv::GComputation(cv::GIn(in), cv::GOut(out)).compileStreaming(std::move(args));
  37. // Graph execution /////////////////////////////////////////////////////////
  38. cv::Mat out_mat(1920, 1080, CV_8UC1);
  39. pipeline.setSource(cv::gapi::wip::make_src<cv::gapi::oak::ColorCamera>());
  40. pipeline.start();
  41. // pull 1 frame
  42. pipeline.pull(cv::gout(out_mat));
  43. cv::imwrite(output_name, out_mat);
  44. std::cout << "Pipeline finished: " << output_name << " file has been written." << std::endl;
  45. }
  46. #else // HAVE_OAK
  47. int main() {
  48. GAPI_Assert(false && "Built without OAK support");
  49. return -1;
  50. }
  51. #endif // HAVE_OAK