video_segmenter.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  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. #include <glog/logging.h>
  15. #include <omp.h>
  16. #include <algorithm>
  17. #include <chrono> // NOLINT
  18. #include <fstream>
  19. #include <iostream>
  20. #include <string>
  21. #include <vector>
  22. #include <utility>
  23. #include <ctime>
  24. #include "include/paddlex/paddlex.h"
  25. #include "include/paddlex/visualize.h"
  26. #if defined(__arm__) || defined(__aarch64__)
  27. #include <opencv2/videoio/legacy/constants_c.h>
  28. #endif
  29. using namespace std::chrono; // NOLINT
  30. DEFINE_string(model_dir, "", "Path of inference model");
  31. DEFINE_bool(use_gpu, false, "Infering with GPU or CPU");
  32. DEFINE_bool(use_trt, false, "Infering with TensorRT");
  33. DEFINE_bool(use_mkl, true, "Infering with MKL");
  34. DEFINE_int32(gpu_id, 0, "GPU card id");
  35. DEFINE_string(key, "", "key of encryption");
  36. DEFINE_int32(mkl_thread_num,
  37. omp_get_num_procs(),
  38. "Number of mkl threads");
  39. DEFINE_bool(use_camera, false, "Infering with Camera");
  40. DEFINE_int32(camera_id, 0, "Camera id");
  41. DEFINE_string(video_path, "", "Path of input video");
  42. DEFINE_bool(show_result, false, "show the result of each frame with a window");
  43. DEFINE_bool(save_result, true, "save the result of each frame to a video");
  44. DEFINE_string(save_dir, "output", "Path to save visualized image");
  45. int main(int argc, char** argv) {
  46. // Parsing command-line
  47. google::ParseCommandLineFlags(&argc, &argv, true);
  48. if (FLAGS_model_dir == "") {
  49. std::cerr << "--model_dir need to be defined" << std::endl;
  50. return -1;
  51. }
  52. if (FLAGS_video_path == "" & FLAGS_use_camera == false) {
  53. std::cerr << "--video_path or --use_camera need to be defined" << std::endl;
  54. return -1;
  55. }
  56. // Load model
  57. PaddleX::Model model;
  58. model.Init(FLAGS_model_dir,
  59. FLAGS_use_gpu,
  60. FLAGS_use_trt,
  61. FLAGS_use_mkl,
  62. FLAGS_mkl_thread_num,
  63. FLAGS_gpu_id,
  64. FLAGS_key);
  65. // Open video
  66. cv::VideoCapture capture;
  67. if (FLAGS_use_camera) {
  68. capture.open(FLAGS_camera_id);
  69. if (!capture.isOpened()) {
  70. std::cout << "Can not open the camera "
  71. << FLAGS_camera_id << "."
  72. << std::endl;
  73. return -1;
  74. }
  75. } else {
  76. capture.open(FLAGS_video_path);
  77. if (!capture.isOpened()) {
  78. std::cout << "Can not open the video "
  79. << FLAGS_video_path << "."
  80. << std::endl;
  81. return -1;
  82. }
  83. }
  84. // Create a VideoWriter
  85. cv::VideoWriter video_out;
  86. std::string video_out_path;
  87. if (FLAGS_save_result) {
  88. // Get video information: resolution, fps
  89. int video_width = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
  90. int video_height = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT));
  91. int video_fps = static_cast<int>(capture.get(CV_CAP_PROP_FPS));
  92. int video_fourcc;
  93. if (FLAGS_use_camera) {
  94. video_fourcc = 828601953;
  95. } else {
  96. video_fourcc = CV_FOURCC('M', 'J', 'P', 'G');
  97. }
  98. if (FLAGS_use_camera) {
  99. time_t now = time(0);
  100. video_out_path =
  101. PaddleX::generate_save_path(FLAGS_save_dir,
  102. std::to_string(now) + ".mp4");
  103. } else {
  104. video_out_path =
  105. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_video_path);
  106. }
  107. video_out.open(video_out_path.c_str(),
  108. video_fourcc,
  109. video_fps,
  110. cv::Size(video_width, video_height),
  111. true);
  112. if (!video_out.isOpened()) {
  113. std::cout << "Create video writer failed!" << std::endl;
  114. return -1;
  115. }
  116. }
  117. PaddleX::SegResult result;
  118. cv::Mat frame;
  119. int key;
  120. while (capture.read(frame)) {
  121. if (FLAGS_show_result || FLAGS_use_camera) {
  122. key = cv::waitKey(1);
  123. // When pressing `ESC`, then exit program and result video is saved
  124. if (key == 27) {
  125. break;
  126. }
  127. } else if (frame.empty()) {
  128. break;
  129. }
  130. // Begin to predict
  131. model.predict(frame, &result);
  132. // Visualize results
  133. cv::Mat vis_img = PaddleX::Visualize(frame, result, model.labels);
  134. if (FLAGS_show_result || FLAGS_use_camera) {
  135. cv::imshow("video_segmenter", vis_img);
  136. }
  137. if (FLAGS_save_result) {
  138. video_out.write(vis_img);
  139. }
  140. result.clear();
  141. }
  142. capture.release();
  143. if (FLAGS_save_result) {
  144. video_out.release();
  145. std::cout << "Visualized output saved as " << video_out_path << std::endl;
  146. }
  147. if (FLAGS_show_result || FLAGS_use_camera) {
  148. cv::destroyAllWindows();
  149. }
  150. return 0;
  151. }