human_segmenter.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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_string(image, "", "Path of test image file");
  36. DEFINE_bool(use_camera, false, "Infering with Camera");
  37. DEFINE_int32(camera_id, 0, "Camera id");
  38. DEFINE_string(video_path, "", "Path of input video");
  39. DEFINE_bool(show_result, false, "show the result of each frame with a window");
  40. DEFINE_bool(save_result, true, "save the result of each frame to a video");
  41. DEFINE_string(save_dir, "output", "Path to save visualized image");
  42. int main(int argc, char** argv) {
  43. // Parsing command-line
  44. google::ParseCommandLineFlags(&argc, &argv, true);
  45. if (FLAGS_model_dir == "") {
  46. std::cerr << "--model_dir need to be defined" << std::endl;
  47. return -1;
  48. }
  49. if (FLAGS_image == "" & FLAGS_video_path == ""
  50. & FLAGS_use_camera == false) {
  51. std::cerr << "--image or --video_path or --use_camera need to be defined"
  52. << std::endl;
  53. return -1;
  54. }
  55. // Load model
  56. PaddleX::Model model;
  57. model.Init(FLAGS_model_dir,
  58. FLAGS_use_gpu,
  59. FLAGS_use_trt,
  60. FLAGS_gpu_id,
  61. FLAGS_key);
  62. if (FLAGS_use_camera || FLAGS_video_path != "") {
  63. // Open video
  64. cv::VideoCapture capture;
  65. if (FLAGS_use_camera) {
  66. capture.open(FLAGS_camera_id);
  67. if (!capture.isOpened()) {
  68. std::cout << "Can not open the camera "
  69. << FLAGS_camera_id << "."
  70. << std::endl;
  71. return -1;
  72. }
  73. } else {
  74. capture.open(FLAGS_video_path);
  75. if (!capture.isOpened()) {
  76. std::cout << "Can not open the video "
  77. << FLAGS_video_path << "."
  78. << std::endl;
  79. return -1;
  80. }
  81. }
  82. // Create a VideoWriter
  83. cv::VideoWriter video_out;
  84. std::string video_out_path;
  85. if (FLAGS_save_result) {
  86. // Get video information: resolution, fps
  87. int video_width = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
  88. int video_height =
  89. static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT));
  90. int video_fps = static_cast<int>(capture.get(CV_CAP_PROP_FPS));
  91. int video_fourcc;
  92. if (FLAGS_use_camera) {
  93. video_fourcc = 828601953;
  94. } else {
  95. video_fourcc = static_cast<int>(capture.get(CV_CAP_PROP_FOURCC));
  96. }
  97. if (FLAGS_use_camera) {
  98. time_t now = time(0);
  99. video_out_path =
  100. PaddleX::generate_save_path(FLAGS_save_dir,
  101. std::to_string(now) + ".mp4");
  102. } else {
  103. video_out_path =
  104. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_video_path);
  105. }
  106. video_out.open(video_out_path.c_str(),
  107. video_fourcc,
  108. video_fps,
  109. cv::Size(video_width, video_height),
  110. true);
  111. if (!video_out.isOpened()) {
  112. std::cout << "Create video writer failed!" << std::endl;
  113. return -1;
  114. }
  115. }
  116. PaddleX::SegResult result;
  117. cv::Mat frame;
  118. int key;
  119. while (capture.read(frame)) {
  120. if (FLAGS_show_result || FLAGS_use_camera) {
  121. key = cv::waitKey(1);
  122. // When pressing `ESC`, then exit program and result video is saved
  123. if (key == 27) {
  124. break;
  125. }
  126. } else if (frame.empty()) {
  127. break;
  128. }
  129. // Begin to predict
  130. model.predict(frame, &result);
  131. // Visualize results
  132. std::vector<uint8_t> label_map(result.label_map.data.begin(),
  133. result.label_map.data.end());
  134. cv::Mat mask(result.label_map.shape[0],
  135. result.label_map.shape[1],
  136. CV_8UC1,
  137. label_map.data());
  138. int rows = result.label_map.shape[0];
  139. int cols = result.label_map.shape[1];
  140. cv::Mat vis_img = frame.clone();
  141. for (int i = 0; i < rows; i++) {
  142. for (int j = 0; j < cols; j++) {
  143. int category_id = static_cast<int>(mask.at<uchar>(i, j));
  144. if (category_id == 0) {
  145. vis_img.at<cv::Vec3b>(i, j)[0] = 255;
  146. vis_img.at<cv::Vec3b>(i, j)[1] = 255;
  147. vis_img.at<cv::Vec3b>(i, j)[2] = 255;
  148. }
  149. }
  150. }
  151. if (FLAGS_show_result || FLAGS_use_camera) {
  152. cv::imshow("human_seg", vis_img);
  153. }
  154. if (FLAGS_save_result) {
  155. video_out.write(vis_img);
  156. }
  157. result.clear();
  158. }
  159. capture.release();
  160. if (FLAGS_save_result) {
  161. video_out.release();
  162. std::cout << "Visualized output saved as " << video_out_path << std::endl;
  163. }
  164. if (FLAGS_show_result || FLAGS_use_camera) {
  165. cv::destroyAllWindows();
  166. }
  167. } else {
  168. PaddleX::SegResult result;
  169. cv::Mat im = cv::imread(FLAGS_image, 1);
  170. model.predict(im, &result);
  171. // Visualize results
  172. std::vector<uint8_t> label_map(result.label_map.data.begin(),
  173. result.label_map.data.end());
  174. cv::Mat mask(result.label_map.shape[0],
  175. result.label_map.shape[1],
  176. CV_8UC1,
  177. label_map.data());
  178. int rows = result.label_map.shape[0];
  179. int cols = result.label_map.shape[1];
  180. cv::Mat vis_img = im.clone();
  181. for (int i = 0; i < rows; i++) {
  182. for (int j = 0; j < cols; j++) {
  183. int category_id = static_cast<int>(mask.at<uchar>(i, j));
  184. if (category_id == 0) {
  185. vis_img.at<cv::Vec3b>(i, j)[0] = 255;
  186. vis_img.at<cv::Vec3b>(i, j)[1] = 255;
  187. vis_img.at<cv::Vec3b>(i, j)[2] = 255;
  188. }
  189. }
  190. }
  191. std::string save_path =
  192. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_image);
  193. cv::imwrite(save_path, vis_img);
  194. result.clear();
  195. std::cout << "Visualized output saved as " << save_path << std::endl;
  196. }
  197. return 0;
  198. }