video_detector.cpp 5.1 KB

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