video_segmenter.cpp 4.7 KB

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