oak_rgb_camera_encoding.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <fstream>
  2. #include <opencv2/gapi.hpp>
  3. #include <opencv2/gapi/core.hpp>
  4. #include <opencv2/gapi/gframe.hpp>
  5. #include <opencv2/gapi/oak/oak.hpp>
  6. #include <opencv2/gapi/streaming/format.hpp> // BGR accessor
  7. #include <opencv2/highgui.hpp> // CommandLineParser
  8. const std::string keys =
  9. "{ h help | | Print this help message }"
  10. "{ output | output.h265 | Path to the output .h265 video file }";
  11. #ifdef HAVE_OAK
  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. cv::gapi::oak::EncoderConfig cfg;
  20. cfg.profile = cv::gapi::oak::EncoderConfig::Profile::H265_MAIN;
  21. cv::GFrame in;
  22. cv::GArray<uint8_t> encoded = cv::gapi::oak::encode(in, cfg);
  23. auto args = cv::compile_args(cv::gapi::oak::ColorCameraParams{}, cv::gapi::oak::kernels());
  24. auto pipeline = cv::GComputation(cv::GIn(in), cv::GOut(encoded)).compileStreaming(std::move(args));
  25. // Graph execution /////////////////////////////////////////////////////////
  26. pipeline.setSource(cv::gapi::wip::make_src<cv::gapi::oak::ColorCamera>());
  27. pipeline.start();
  28. std::vector<uint8_t> out_h265_data;
  29. std::ofstream out_h265_file;
  30. out_h265_file.open(output_name, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
  31. // Pull 300 frames from the camera
  32. uint32_t frames = 300;
  33. uint32_t pulled = 0;
  34. while (pipeline.pull(cv::gout(out_h265_data))) {
  35. if (out_h265_file.is_open()) {
  36. out_h265_file.write(reinterpret_cast<const char*>(out_h265_data.data()),
  37. out_h265_data.size());
  38. }
  39. if (pulled++ == frames) {
  40. pipeline.stop();
  41. break;
  42. }
  43. }
  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