meter_reader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <iostream>
  19. #include <vector>
  20. #include <utility>
  21. #include <limits>
  22. #include <opencv2/opencv.hpp>
  23. #include <opencv2/highgui.hpp>
  24. #include <opencv2/core/core.hpp>
  25. #include "meter_reader/global.h"
  26. #include "meter_reader/postprocess.h"
  27. #include "include/paddlex/paddlex.h"
  28. #include "include/paddlex/visualize.h"
  29. using namespace std::chrono; // NOLINT
  30. DEFINE_string(det_model_dir, "", "Path of detection inference model");
  31. DEFINE_string(seg_model_dir, "", "Path of segmentation inference model");
  32. DEFINE_bool(use_gpu, false, "Infering with GPU or CPU");
  33. DEFINE_bool(use_trt, false, "Infering with TensorRT");
  34. DEFINE_bool(use_camera, false, "Infering with Camera");
  35. DEFINE_bool(use_erode, true, "Eroding predicted label map");
  36. DEFINE_int32(gpu_id, 0, "GPU card id");
  37. DEFINE_int32(camera_id, 0, "Camera id");
  38. DEFINE_int32(thread_num,
  39. omp_get_num_procs(),
  40. "Number of preprocessing threads");
  41. DEFINE_int32(erode_kernel, true, "Eroding kernel size");
  42. DEFINE_int32(seg_batch_size, 2, "Batch size of segmentation infering");
  43. DEFINE_string(det_key, "", "Detector key of encryption");
  44. DEFINE_string(seg_key, "", "Segmenter model key of encryption");
  45. DEFINE_string(image, "", "Path of test image file");
  46. DEFINE_string(image_list, "", "Path of test image list file");
  47. DEFINE_string(save_dir, "output", "Path to save visualized image");
  48. DEFINE_double(score_threshold, 0.5,
  49. "Detected bbox whose score is lower than this threshlod is filtered");
  50. void predict(const cv::Mat &input_image, PaddleX::Model *det_model,
  51. PaddleX::Model *seg_model, const std::string save_dir,
  52. const std::string image_path, const bool use_erode,
  53. const int erode_kernel, const int thread_num,
  54. const int seg_batch_size, const double threshold) {
  55. PaddleX::DetResult det_result;
  56. det_model->predict(input_image, &det_result);
  57. PaddleX::DetResult filter_result;
  58. int num_bboxes = det_result.boxes.size();
  59. for (int i = 0; i < num_bboxes; ++i) {
  60. double score = det_result.boxes[i].score;
  61. if (score > threshold || score == threshold) {
  62. PaddleX::Box box;
  63. box.category_id = det_result.boxes[i].category_id;
  64. box.category = det_result.boxes[i].category;
  65. box.score = det_result.boxes[i].score;
  66. box.coordinate = det_result.boxes[i].coordinate;
  67. filter_result.boxes.push_back(std::move(box));
  68. }
  69. }
  70. int meter_num = filter_result.boxes.size();
  71. if (!meter_num) {
  72. std::cout << "Don't find any meter." << std::endl;
  73. return;
  74. }
  75. std::vector<std::vector<int64_t>> seg_result(meter_num);
  76. for (int i = 0; i < meter_num; i += seg_batch_size) {
  77. int im_vec_size =
  78. std::min(static_cast<int>(meter_num), i + seg_batch_size);
  79. std::vector<cv::Mat> meters_image(im_vec_size - i);
  80. int batch_thread_num = std::min(thread_num, im_vec_size - i);
  81. #pragma omp parallel for num_threads(batch_thread_num)
  82. for (int j = i; j < im_vec_size; ++j) {
  83. int left = static_cast<int>(filter_result.boxes[j].coordinate[0]);
  84. int top = static_cast<int>(filter_result.boxes[j].coordinate[1]);
  85. int width = static_cast<int>(filter_result.boxes[j].coordinate[2]);
  86. int height = static_cast<int>(filter_result.boxes[j].coordinate[3]);
  87. int right = left + width - 1;
  88. int bottom = top + height - 1;
  89. cv::Mat sub_image = input_image(
  90. cv::Range(top, bottom + 1), cv::Range(left, right + 1));
  91. float scale_x =
  92. static_cast<float>(METER_SHAPE[0]) / static_cast<float>(sub_image.cols);
  93. float scale_y =
  94. static_cast<float>(METER_SHAPE[1]) / static_cast<float>(sub_image.rows);
  95. cv::resize(sub_image,
  96. sub_image,
  97. cv::Size(),
  98. scale_x,
  99. scale_y,
  100. cv::INTER_LINEAR);
  101. meters_image[j - i] = std::move(sub_image);
  102. }
  103. std::vector<PaddleX::SegResult> batch_result(im_vec_size - i);
  104. seg_model->predict(meters_image, &batch_result, batch_thread_num);
  105. #pragma omp parallel for num_threads(batch_thread_num)
  106. for (int j = i; j < im_vec_size; ++j) {
  107. if (use_erode) {
  108. cv::Mat kernel(4, 4, CV_8U, cv::Scalar(1));
  109. std::vector<uint8_t> label_map(
  110. batch_result[j - i].label_map.data.begin(),
  111. batch_result[j - i].label_map.data.end());
  112. cv::Mat mask(batch_result[j - i].label_map.shape[0],
  113. batch_result[j - i].label_map.shape[1],
  114. CV_8UC1,
  115. label_map.data());
  116. cv::erode(mask, mask, kernel);
  117. std::vector<int64_t> map;
  118. if (mask.isContinuous()) {
  119. map.assign(mask.data, mask.data + mask.total() * mask.channels());
  120. } else {
  121. for (int r = 0; r < mask.rows; r++) {
  122. map.insert(map.end(),
  123. mask.ptr<int64_t>(r),
  124. mask.ptr<int64_t>(r) + mask.cols * mask.channels());
  125. }
  126. }
  127. seg_result[j] = std::move(map);
  128. } else {
  129. seg_result[j] = std::move(batch_result[j - i].label_map.data);
  130. }
  131. }
  132. }
  133. std::vector<READ_RESULT> read_results(meter_num);
  134. int all_thread_num = std::min(thread_num, meter_num);
  135. read_process(seg_result, &read_results, all_thread_num);
  136. cv::Mat output_image = input_image.clone();
  137. for (int i = 0; i < meter_num; i++) {
  138. float result = 0;;
  139. if (read_results[i].scale_num > TYPE_THRESHOLD) {
  140. result = read_results[i].scales * meter_config[0].scale_value;
  141. } else {
  142. result = read_results[i].scales * meter_config[1].scale_value;
  143. }
  144. std::cout << "-- Meter " << i
  145. << " -- result: " << result
  146. << " --" << std::endl;
  147. int lx = static_cast<int>(filter_result.boxes[i].coordinate[0]);
  148. int ly = static_cast<int>(filter_result.boxes[i].coordinate[1]);
  149. int w = static_cast<int>(filter_result.boxes[i].coordinate[2]);
  150. int h = static_cast<int>(filter_result.boxes[i].coordinate[3]);
  151. cv::Rect bounding_box = cv::Rect(lx, ly, w, h) &
  152. cv::Rect(0, 0, output_image.cols, output_image.rows);
  153. if (w > 0 && h > 0) {
  154. cv::Scalar color = cv::Scalar(237, 189, 101);
  155. cv::rectangle(output_image, bounding_box, color);
  156. cv::rectangle(output_image,
  157. cv::Point2d(lx, ly),
  158. cv::Point2d(lx + w, ly - 30),
  159. color, -1);
  160. std::string class_name = "Meter";
  161. cv::putText(output_image,
  162. class_name + " " + std::to_string(result),
  163. cv::Point2d(lx, ly-5),
  164. cv::FONT_HERSHEY_SIMPLEX,
  165. 1, cv::Scalar(255, 255, 255), 2);
  166. }
  167. }
  168. cv::Mat result_image;
  169. cv::Size resize_size(RESULT_SHAPE[0], RESULT_SHAPE[1]);
  170. cv::resize(output_image, result_image, resize_size, 0, 0, cv::INTER_LINEAR);
  171. std::string save_path = PaddleX::generate_save_path(save_dir, image_path);
  172. cv::imwrite(save_path, result_image);
  173. return;
  174. }
  175. int main(int argc, char **argv) {
  176. google::ParseCommandLineFlags(&argc, &argv, true);
  177. if (FLAGS_det_model_dir == "") {
  178. std::cerr << "--det_model_dir need to be defined" << std::endl;
  179. return -1;
  180. }
  181. if (FLAGS_seg_model_dir == "") {
  182. std::cerr << "--seg_model_dir need to be defined" << std::endl;
  183. return -1;
  184. }
  185. if (FLAGS_image == "" & FLAGS_image_list == "" & FLAGS_use_camera == false) {
  186. std::cerr << "--image or --image_list need to be defined "
  187. << "when the camera is not been used" << std::endl;
  188. return -1;
  189. }
  190. // Load model
  191. PaddleX::Model det_model;
  192. det_model.Init(FLAGS_det_model_dir, FLAGS_use_gpu, FLAGS_use_trt,
  193. FLAGS_gpu_id, FLAGS_det_key);
  194. PaddleX::Model seg_model;
  195. seg_model.Init(FLAGS_seg_model_dir, FLAGS_use_gpu, FLAGS_use_trt,
  196. FLAGS_gpu_id, FLAGS_seg_key);
  197. double total_running_time_s = 0.0;
  198. double total_imread_time_s = 0.0;
  199. int imgs = 1;
  200. if (FLAGS_use_camera) {
  201. cv::VideoCapture cap(FLAGS_camera_id);
  202. cap.set(CV_CAP_PROP_FRAME_WIDTH, IMAGE_SHAPE[0]);
  203. cap.set(CV_CAP_PROP_FRAME_HEIGHT, IMAGE_SHAPE[1]);
  204. if (!cap.isOpened()) {
  205. std::cout << "Open the camera unsuccessfully." << std::endl;
  206. return -1;
  207. }
  208. std::cout << "Open the camera successfully." << std::endl;
  209. while (1) {
  210. auto start = system_clock::now();
  211. cv::Mat im;
  212. cap >> im;
  213. auto imread_end = system_clock::now();
  214. std::cout << "-------------------------" << std::endl;
  215. std::cout << "Got a camera image." << std::endl;
  216. std::string ext_name = ".jpg";
  217. predict(im, &det_model, &seg_model, FLAGS_save_dir,
  218. std::to_string(imgs) + ext_name, FLAGS_use_erode,
  219. FLAGS_erode_kernel, FLAGS_thread_num,
  220. FLAGS_seg_batch_size, FLAGS_score_threshold);
  221. imgs++;
  222. auto imread_duration = duration_cast<microseconds>(imread_end - start);
  223. total_imread_time_s += static_cast<double>(imread_duration.count()) *
  224. microseconds::period::num /
  225. microseconds::period::den;
  226. auto end = system_clock::now();
  227. auto duration = duration_cast<microseconds>(end - start);
  228. total_running_time_s += static_cast<double>(duration.count()) *
  229. microseconds::period::num /
  230. microseconds::period::den;
  231. }
  232. cap.release();
  233. cv::destroyAllWindows();
  234. } else {
  235. if (FLAGS_image_list != "") {
  236. std::ifstream inf(FLAGS_image_list);
  237. if (!inf) {
  238. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  239. return -1;
  240. }
  241. std::string image_path;
  242. while (getline(inf, image_path)) {
  243. auto start = system_clock::now();
  244. cv::Mat im = cv::imread(image_path, 1);
  245. imgs++;
  246. auto imread_end = system_clock::now();
  247. predict(im, &det_model, &seg_model, FLAGS_save_dir,
  248. image_path, FLAGS_use_erode, FLAGS_erode_kernel,
  249. FLAGS_thread_num, FLAGS_seg_batch_size,
  250. FLAGS_score_threshold);
  251. auto imread_duration = duration_cast<microseconds>(imread_end - start);
  252. total_imread_time_s += static_cast<double>(imread_duration.count()) *
  253. microseconds::period::num /
  254. microseconds::period::den;
  255. auto end = system_clock::now();
  256. auto duration = duration_cast<microseconds>(end - start);
  257. total_running_time_s += static_cast<double>(duration.count()) *
  258. microseconds::period::num /
  259. microseconds::period::den;
  260. }
  261. } else {
  262. auto start = system_clock::now();
  263. cv::Mat im = cv::imread(FLAGS_image, 1);
  264. auto imread_end = system_clock::now();
  265. predict(im, &det_model, &seg_model, FLAGS_save_dir,
  266. FLAGS_image, FLAGS_use_erode, FLAGS_erode_kernel,
  267. FLAGS_thread_num, FLAGS_seg_batch_size,
  268. FLAGS_score_threshold);
  269. auto imread_duration = duration_cast<microseconds>(imread_end - start);
  270. total_imread_time_s += static_cast<double>(imread_duration.count()) *
  271. microseconds::period::num /
  272. microseconds::period::den;
  273. auto end = system_clock::now();
  274. auto duration = duration_cast<microseconds>(end - start);
  275. total_running_time_s += static_cast<double>(duration.count()) *
  276. microseconds::period::num /
  277. microseconds::period::den;
  278. }
  279. }
  280. std::cout << "Total running time: " << total_running_time_s
  281. << " s, average running time: " << total_running_time_s / imgs
  282. << " s/img, total read img time: " << total_imread_time_s
  283. << " s, average read time: " << total_imread_time_s / imgs
  284. << " s/img" << std::endl;
  285. return 0;
  286. }