video_classifier.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_string(key, "", "key of encryption");
  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(save_dir, "output", "Path to save visualized image");
  40. int main(int argc, char** argv) {
  41. // Parsing command-line
  42. google::ParseCommandLineFlags(&argc, &argv, true);
  43. if (FLAGS_model_dir == "") {
  44. std::cerr << "--model_dir need to be defined" << std::endl;
  45. return -1;
  46. }
  47. if (FLAGS_video_path == "" & FLAGS_use_camera == false) {
  48. std::cerr << "--video_path or --use_camera need to be defined" << std::endl;
  49. return -1;
  50. }
  51. // 加载模型
  52. PaddleX::Model model;
  53. model.Init(FLAGS_model_dir,
  54. FLAGS_use_gpu,
  55. FLAGS_use_trt,
  56. FLAGS_gpu_id,
  57. FLAGS_key);
  58. // 打开视频流
  59. cv::VideoCapture capture;
  60. if (FLAGS_use_camera) {
  61. capture.open(FLAGS_camera_id);
  62. if (!capture.isOpened()) {
  63. std::cout << "Can not open the camera "
  64. << FLAGS_camera_id << "."
  65. << std::endl;
  66. return -1;
  67. }
  68. } else {
  69. capture.open(FLAGS_video_path);
  70. if (!capture.isOpened()) {
  71. std::cout << "Can not open the video "
  72. << FLAGS_video_path << "."
  73. << std::endl;
  74. return -1;
  75. }
  76. }
  77. // 创建VideoWriter
  78. cv::VideoWriter video_out;
  79. std::string video_out_path;
  80. if (FLAGS_save_result) {
  81. // 获取视频流信息: 分辨率, 帧率
  82. int video_width = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
  83. int video_height = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT));
  84. int video_fps = static_cast<int>(capture.get(CV_CAP_PROP_FPS));
  85. int video_fourcc;
  86. if (FLAGS_use_camera) {
  87. video_fourcc = 828601953;
  88. } else {
  89. video_fourcc = static_cast<int>(capture.get(CV_CAP_PROP_FOURCC));
  90. }
  91. if (FLAGS_use_camera) {
  92. time_t now = time(0);
  93. video_out_path =
  94. PaddleX::generate_save_path(FLAGS_save_dir,
  95. std::to_string(now) + ".mp4");
  96. } else {
  97. video_out_path =
  98. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_video_path);
  99. }
  100. video_out.open(video_out_path.c_str(),
  101. video_fourcc,
  102. video_fps,
  103. cv::Size(video_width, video_height),
  104. true);
  105. if (!video_out.isOpened()) {
  106. std::cout << "Create video writer failed!" << std::endl;
  107. return -1;
  108. }
  109. }
  110. PaddleX::ClsResult result;
  111. cv::Mat frame;
  112. int key;
  113. while (capture.read(frame)) {
  114. if (FLAGS_show_result || FLAGS_use_camera) {
  115. key = cv::waitKey(1);
  116. // 按下ESC退出整个程序,保存视频文件到磁盘
  117. if (key == 27) {
  118. break;
  119. }
  120. } else if (frame.empty()) {
  121. break;
  122. }
  123. // 开始预测
  124. model.predict(frame, &result);
  125. // 可视化
  126. cv::Mat vis_img = frame.clone();
  127. auto colormap = PaddleX::GenerateColorMap(model.labels.size());
  128. int c1 = colormap[3 * result.category_id + 0];
  129. int c2 = colormap[3 * result.category_id + 1];
  130. int c3 = colormap[3 * result.category_id + 2];
  131. cv::Scalar text_color = cv::Scalar(c1, c2, c3);
  132. std::string text = result.category;
  133. text += std::to_string(static_cast<int>(result.score * 100)) + "%";
  134. int font_face = cv::FONT_HERSHEY_SIMPLEX;
  135. double font_scale = 0.5f;
  136. float thickness = 0.5;
  137. cv::Size text_size =
  138. cv::getTextSize(text, font_face, font_scale, thickness, nullptr);
  139. cv::Point origin;
  140. origin.x = frame.cols / 2;
  141. origin.y = frame.rows / 2;
  142. cv::Rect text_back = cv::Rect(origin.x,
  143. origin.y - text_size.height,
  144. text_size.width,
  145. text_size.height);
  146. cv::rectangle(vis_img, text_back, text_color, -1);
  147. cv::putText(vis_img,
  148. text,
  149. origin,
  150. font_face,
  151. font_scale,
  152. cv::Scalar(255, 255, 255),
  153. thickness);
  154. if (FLAGS_show_result || FLAGS_use_camera) {
  155. cv::imshow("human_seg", vis_img);
  156. }
  157. if (FLAGS_save_result) {
  158. video_out.write(vis_img);
  159. }
  160. std::cout << "Predict label: " << result.category
  161. << ", label_id:" << result.category_id
  162. << ", score: " << result.score << std::endl;
  163. }
  164. capture.release();
  165. if (FLAGS_save_result) {
  166. video_out.release();
  167. std::cout << "Visualized output saved as " << video_out_path << std::endl;
  168. }
  169. if (FLAGS_show_result || FLAGS_use_camera) {
  170. cv::destroyAllWindows();
  171. }
  172. return 0;
  173. }