video_detector.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_int32(gpu_id, 0, "GPU card id");
  33. DEFINE_bool(use_camera, false, "Infering with Camera");
  34. DEFINE_int32(camera_id, 0, "Camera id");
  35. DEFINE_string(video_path, "", "Path of input video");
  36. DEFINE_bool(show_result, false, "show the result of each frame with a window");
  37. DEFINE_bool(save_result, true, "save the result of each frame to a video");
  38. DEFINE_string(key, "", "key of encryption");
  39. DEFINE_string(save_dir, "output", "Path to save visualized image");
  40. DEFINE_double(threshold,
  41. 0.5,
  42. "The minimum scores of target boxes which are shown");
  43. int main(int argc, char** argv) {
  44. // 解析命令行参数
  45. google::ParseCommandLineFlags(&argc, &argv, true);
  46. if (FLAGS_model_dir == "") {
  47. std::cerr << "--model_dir need to be defined" << std::endl;
  48. return -1;
  49. }
  50. if (FLAGS_video_path == "" & FLAGS_use_camera == false) {
  51. std::cerr << "--video_path or --use_camera need to be defined" << std::endl;
  52. return -1;
  53. }
  54. // 加载模型
  55. PaddleX::Model model;
  56. model.Init(FLAGS_model_dir,
  57. FLAGS_use_gpu,
  58. FLAGS_use_trt,
  59. FLAGS_gpu_id,
  60. FLAGS_key);
  61. // 打开视频流
  62. cv::VideoCapture capture;
  63. if (FLAGS_use_camera) {
  64. capture.open(FLAGS_camera_id);
  65. if (!capture.isOpened()) {
  66. std::cout << "Can not open the camera "
  67. << FLAGS_camera_id << "."
  68. << std::endl;
  69. return -1;
  70. }
  71. } else {
  72. capture.open(FLAGS_video_path);
  73. if (!capture.isOpened()) {
  74. std::cout << "Can not open the video "
  75. << FLAGS_video_path << "."
  76. << std::endl;
  77. return -1;
  78. }
  79. }
  80. // 创建VideoWriter
  81. cv::VideoWriter video_out;
  82. std::string video_out_path;
  83. if (FLAGS_save_result) {
  84. // 获取视频流信息: 分辨率, 帧率
  85. int video_width = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
  86. int video_height = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT));
  87. int video_fps = static_cast<int>(capture.get(CV_CAP_PROP_FPS));
  88. int video_fourcc;
  89. if (FLAGS_use_camera) {
  90. video_fourcc = 828601953;
  91. } else {
  92. video_fourcc = static_cast<int>(capture.get(CV_CAP_PROP_FOURCC));
  93. }
  94. if (FLAGS_use_camera) {
  95. time_t now = time(0);
  96. video_out_path =
  97. PaddleX::generate_save_path(FLAGS_save_dir,
  98. std::to_string(now) + ".mp4");
  99. } else {
  100. video_out_path =
  101. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_video_path);
  102. }
  103. video_out.open(video_out_path.c_str(),
  104. video_fourcc,
  105. video_fps,
  106. cv::Size(video_width, video_height),
  107. true);
  108. if (!video_out.isOpened()) {
  109. std::cout << "Create video writer failed!" << std::endl;
  110. return -1;
  111. }
  112. }
  113. PaddleX::DetResult result;
  114. cv::Mat frame;
  115. int key;
  116. while (capture.read(frame)) {
  117. if (FLAGS_show_result || FLAGS_use_camera) {
  118. key = cv::waitKey(1);
  119. // 按下ESC退出整个程序,保存视频文件到磁盘
  120. if (key == 27) {
  121. break;
  122. }
  123. } else if (frame.empty()) {
  124. break;
  125. }
  126. model.predict(frame, &result);
  127. // 可视化
  128. cv::Mat vis_img =
  129. PaddleX::Visualize(frame, result, model.labels, FLAGS_threshold);
  130. if (FLAGS_show_result || FLAGS_use_camera) {
  131. cv::imshow("video_detector", vis_img);
  132. }
  133. if (FLAGS_save_result) {
  134. video_out.write(vis_img);
  135. }
  136. result.clear();
  137. }
  138. capture.release();
  139. if (FLAGS_save_result) {
  140. std::cout << "Visualized output saved as " << video_out_path << std::endl;
  141. video_out.release();
  142. }
  143. if (FLAGS_show_result || FLAGS_use_camera) {
  144. cv::destroyAllWindows();
  145. }
  146. return 0;
  147. }