Advertisement
Guest User

Untitled

a guest
Feb 18th, 2022
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.92 KB | None | 0 0
  1. // Copyright 2019 The MediaPipe Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. //      http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // An example of sending OpenCV webcam frames into a MediaPipe graph.
  16. #include <cstdlib>
  17.  
  18. #include "absl/flags/flag.h"
  19. #include "absl/flags/parse.h"
  20. #include "mediapipe/framework/calculator_framework.h"
  21. #include "mediapipe/framework/formats/image_frame.h"
  22. #include "mediapipe/framework/formats/image_frame_opencv.h"
  23. #include "mediapipe/framework/port/file_helpers.h"
  24. #include "mediapipe/framework/port/opencv_highgui_inc.h"
  25. #include "mediapipe/framework/port/opencv_imgproc_inc.h"
  26. #include "mediapipe/framework/port/opencv_video_inc.h"
  27. #include "mediapipe/framework/port/parse_text_proto.h"
  28. #include "mediapipe/framework/port/status.h"
  29. #include "mediapipe/calculators/util/landmarks_to_render_data_calculator.pb.h"
  30. #include "mediapipe/framework/formats/landmark.pb.h"
  31.  
  32. constexpr char kInputStream[] = "input_video";
  33. constexpr char kOutputStream[] = "output_video";
  34. constexpr char kDetectionsStream[] = "face_landmarks_with_iris";
  35. constexpr char kWindowName[] = "MediaPipe";
  36.  
  37. ABSL_FLAG(std::string, calculator_graph_config_file, "",
  38.           "Name of file containing text format CalculatorGraphConfig proto.");
  39. ABSL_FLAG(std::string, input_video_path, "",
  40.           "Full path of video to load. "
  41.           "If not provided, attempt to use a webcam.");
  42. ABSL_FLAG(std::string, output_video_path, "",
  43.           "Full path of where to save result (.mp4 only). "
  44.           "If not provided, show result in a window.");
  45.  
  46. absl::Status RunMPPGraph() {
  47.   std::string calculator_graph_config_contents;
  48.   MP_RETURN_IF_ERROR(mediapipe::file::GetContents(
  49.       absl::GetFlag(FLAGS_calculator_graph_config_file),
  50.       &calculator_graph_config_contents));
  51.   LOG(INFO) << "Get calculator graph config contents: "
  52.             << calculator_graph_config_contents;
  53.   mediapipe::CalculatorGraphConfig config =
  54.       mediapipe::ParseTextProtoOrDie<mediapipe::CalculatorGraphConfig>(
  55.           calculator_graph_config_contents);
  56.  
  57.   LOG(INFO) << "Initialize the calculator graph.";
  58.   mediapipe::CalculatorGraph graph;
  59.   MP_RETURN_IF_ERROR(graph.Initialize(config));
  60.  
  61.   LOG(INFO) << "Initialize the camera or load the video.";
  62.   cv::VideoCapture capture;
  63.   const bool load_video = !absl::GetFlag(FLAGS_input_video_path).empty();
  64.   if (load_video) {
  65.     capture.open(absl::GetFlag(FLAGS_input_video_path));
  66.   } else {
  67.     capture.open(1);
  68.   }
  69.   RET_CHECK(capture.isOpened());
  70.  
  71.   cv::VideoWriter writer;
  72.   const bool save_video = !absl::GetFlag(FLAGS_output_video_path).empty();
  73.   if (!save_video) {
  74.     cv::namedWindow(kWindowName, /*flags=WINDOW_AUTOSIZE*/ 1);
  75. #if (CV_MAJOR_VERSION >= 3) && (CV_MINOR_VERSION >= 2)
  76.     capture.set(cv::CAP_PROP_FRAME_WIDTH, 640);
  77.     capture.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
  78.     capture.set(cv::CAP_PROP_FPS, 30);
  79. #endif
  80.   }
  81.  
  82.   LOG(INFO) << "Start running the calculator graph.";
  83.   ASSIGN_OR_RETURN(mediapipe::OutputStreamPoller poller,
  84.                    graph.AddOutputStreamPoller(kOutputStream));
  85.   ASSIGN_OR_RETURN(mediapipe::OutputStreamPoller poller_detection,
  86.                    graph.AddOutputStreamPoller(kDetectionsStream));
  87.   MP_RETURN_IF_ERROR(graph.StartRun({}));
  88.  
  89.   LOG(INFO) << "Start grabbing and processing frames.";
  90.   bool grab_frames = true;
  91.   while (grab_frames) {
  92.     // Capture opencv camera or video frame.
  93.     cv::Mat camera_frame_raw;
  94.     capture >> camera_frame_raw;
  95.     if (camera_frame_raw.empty()) {
  96.       if (!load_video) {
  97.         LOG(INFO) << "Ignore empty frames from camera.";
  98.         continue;
  99.       }
  100.       LOG(INFO) << "Empty frame, end of video reached.";
  101.       break;
  102.     }
  103.     cv::Mat camera_frame;
  104.     cv::cvtColor(camera_frame_raw, camera_frame, cv::COLOR_BGR2RGB);
  105.     if (!load_video) {
  106.       cv::flip(camera_frame, camera_frame, /*flipcode=HORIZONTAL*/ 1);
  107.     }
  108.  
  109.     // Wrap Mat into an ImageFrame.
  110.     auto input_frame = absl::make_unique<mediapipe::ImageFrame>(
  111.         mediapipe::ImageFormat::SRGB, camera_frame.cols, camera_frame.rows,
  112.         mediapipe::ImageFrame::kDefaultAlignmentBoundary);
  113.     cv::Mat input_frame_mat = mediapipe::formats::MatView(input_frame.get());
  114.     camera_frame.copyTo(input_frame_mat);
  115.  
  116.     // Send image packet into the graph.
  117.     size_t frame_timestamp_us =
  118.         (double)cv::getTickCount() / (double)cv::getTickFrequency() * 1e6;
  119.     MP_RETURN_IF_ERROR(graph.AddPacketToInputStream(
  120.         kInputStream, mediapipe::Adopt(input_frame.release())
  121.                           .At(mediapipe::Timestamp(frame_timestamp_us))));
  122.  
  123.     // Get the graph result packet, or stop if that fails.
  124.     mediapipe::Packet packet;
  125.     if (!poller.Next(&packet)) break;
  126.     auto& output_frame = packet.Get<mediapipe::ImageFrame>();
  127.    
  128.     mediapipe::Packet detection_packet;
  129.     if (!poller_detection.Next(&detection_packet)) break;
  130.     auto& output_landmarks = detection_packet.Get<std::vector<::mediapipe::NormalizedLandmarkList>>();
  131.     for (const ::mediapipe::NormalizedLandmarkList& normalizedlandmarkList : output_landmarks) {
  132.         LOG(INFO) << normalizedlandmarkList.DebugString();
  133.     }
  134.  
  135.     // Convert back to opencv for display or saving.
  136.     cv::Mat output_frame_mat = mediapipe::formats::MatView(&output_frame);
  137.     cv::cvtColor(output_frame_mat, output_frame_mat, cv::COLOR_RGB2BGR);
  138.     if (save_video) {
  139.       if (!writer.isOpened()) {
  140.         LOG(INFO) << "Prepare video writer.";
  141.         writer.open(absl::GetFlag(FLAGS_output_video_path),
  142.                     mediapipe::fourcc('a', 'v', 'c', '1'),  // .mp4
  143.                     capture.get(cv::CAP_PROP_FPS), output_frame_mat.size());
  144.         RET_CHECK(writer.isOpened());
  145.       }
  146.       writer.write(output_frame_mat);
  147.     } else {
  148.       cv::imshow(kWindowName, output_frame_mat);
  149.       // Press any key to exit.
  150.       const int pressed_key = cv::waitKey(5);
  151.       if (pressed_key >= 0 && pressed_key != 255) grab_frames = false;
  152.     }
  153.   }
  154.  
  155.   LOG(INFO) << "Shutting down.";
  156.   if (writer.isOpened()) writer.release();
  157.   MP_RETURN_IF_ERROR(graph.CloseInputStream(kInputStream));
  158.   return graph.WaitUntilDone();
  159. }
  160.  
  161. int main(int argc, char** argv) {
  162.   google::InitGoogleLogging(argv[0]);
  163.   absl::ParseCommandLine(argc, argv);
  164.   absl::Status run_status = RunMPPGraph();
  165.   if (!run_status.ok()) {
  166.     LOG(ERROR) << "Failed to run the graph: " << run_status.message();
  167.     return EXIT_FAILURE;
  168.   } else {
  169.     LOG(INFO) << "Success!";
  170.   }
  171.   return EXIT_SUCCESS;
  172. }
  173.  
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement