oak_rgb_camera_encoding.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. int main(int argc, char *argv[]) {
  12. cv::CommandLineParser cmd(argc, argv, keys);
  13. if (cmd.has("help")) {
  14. cmd.printMessage();
  15. return 0;
  16. }
  17. const std::string output_name = cmd.get<std::string>("output");
  18. cv::gapi::oak::EncoderConfig cfg;
  19. cfg.profile = cv::gapi::oak::EncoderConfig::Profile::H265_MAIN;
  20. cv::GFrame in;
  21. cv::GArray<uint8_t> encoded = cv::gapi::oak::encode(in, cfg);
  22. auto args = cv::compile_args(cv::gapi::oak::ColorCameraParams{}, cv::gapi::oak::kernels());
  23. auto pipeline = cv::GComputation(cv::GIn(in), cv::GOut(encoded)).compileStreaming(std::move(args));
  24. // Graph execution /////////////////////////////////////////////////////////
  25. pipeline.setSource(cv::gapi::wip::make_src<cv::gapi::oak::ColorCamera>());
  26. pipeline.start();
  27. std::vector<uint8_t> out_h265_data;
  28. std::ofstream out_h265_file;
  29. out_h265_file.open(output_name, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
  30. // Pull 300 frames from the camera
  31. uint32_t frames = 300;
  32. uint32_t pulled = 0;
  33. while (pipeline.pull(cv::gout(out_h265_data))) {
  34. if (out_h265_file.is_open()) {
  35. out_h265_file.write(reinterpret_cast<const char*>(out_h265_data.data()),
  36. out_h265_data.size());
  37. }
  38. if (pulled++ == frames) {
  39. pipeline.stop();
  40. break;
  41. }
  42. }
  43. std::cout << "Pipeline finished: " << output_name << " file has been written." << std::endl;
  44. }