detector.cpp 6.5 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 <algorithm>
  16. #include <chrono>
  17. #include <fstream>
  18. #include <iostream>
  19. #include <string>
  20. #include <vector>
  21. #include <utility>
  22. #include <omp.h>
  23. #include "include/paddlex/paddlex.h"
  24. #include "include/paddlex/visualize.h"
  25. using namespace std::chrono;
  26. DEFINE_string(model_dir, "", "Path of inference model");
  27. DEFINE_bool(use_gpu, false, "Infering with GPU or CPU");
  28. DEFINE_bool(use_trt, false, "Infering with TensorRT");
  29. DEFINE_int32(gpu_id, 0, "GPU card id");
  30. DEFINE_string(key, "", "key of encryption");
  31. DEFINE_string(image, "", "Path of test image file");
  32. DEFINE_string(image_list, "", "Path of test image list file");
  33. DEFINE_string(save_dir, "output", "Path to save visualized image");
  34. DEFINE_int32(batch_size, 1, "Batch size of infering");
  35. DEFINE_double(threshold, 0.5, "The minimum scores of target boxes which are shown");
  36. DEFINE_int32(thread_num, omp_get_num_procs(), "Number of preprocessing threads");
  37. int main(int argc, char** argv) {
  38. // 解析命令行参数
  39. google::ParseCommandLineFlags(&argc, &argv, true);
  40. if (FLAGS_model_dir == "") {
  41. std::cerr << "--model_dir need to be defined" << std::endl;
  42. return -1;
  43. }
  44. if (FLAGS_image == "" & FLAGS_image_list == "") {
  45. std::cerr << "--image or --image_list need to be defined" << std::endl;
  46. return -1;
  47. }
  48. std::cout << "Thread num: " << FLAGS_thread_num << std::endl;
  49. // 加载模型
  50. PaddleX::Model model;
  51. model.Init(FLAGS_model_dir, FLAGS_use_gpu, FLAGS_use_trt, FLAGS_gpu_id, FLAGS_key, FLAGS_batch_size);
  52. double total_running_time_s = 0.0;
  53. double total_imread_time_s = 0.0;
  54. int imgs = 1;
  55. auto colormap = PaddleX::GenerateColorMap(model.labels.size());
  56. std::string save_dir = "output";
  57. // 进行预测
  58. if (FLAGS_image_list != "") {
  59. std::ifstream inf(FLAGS_image_list);
  60. if (!inf) {
  61. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  62. return -1;
  63. }
  64. std::string image_path;
  65. std::vector<std::string> image_paths;
  66. while (getline(inf, image_path)) {
  67. image_paths.push_back(image_path);
  68. }
  69. imgs = image_paths.size();
  70. for(int i = 0; i < image_paths.size(); i += FLAGS_batch_size) {
  71. auto start = system_clock::now();
  72. int im_vec_size = std::min((int)image_paths.size(), i + FLAGS_batch_size);
  73. std::vector<cv::Mat> im_vec(im_vec_size - i);
  74. std::vector<PaddleX::DetResult> results(im_vec_size - i, PaddleX::DetResult());
  75. int thread_num = std::min(FLAGS_thread_num, im_vec_size - i);
  76. #pragma omp parallel for num_threads(thread_num)
  77. for(int j = i; j < im_vec_size; ++j){
  78. im_vec[j - i] = std::move(cv::imread(image_paths[j], 1));
  79. }
  80. auto imread_end = system_clock::now();
  81. model.predict(im_vec, results, thread_num);
  82. auto imread_duration = duration_cast<microseconds>(imread_end - start);
  83. total_imread_time_s += double(imread_duration.count()) * microseconds::period::num / microseconds::period::den;
  84. auto end = system_clock::now();
  85. auto duration = duration_cast<microseconds>(end - start);
  86. total_running_time_s += double(duration.count()) * microseconds::period::num / microseconds::period::den;
  87. //输出结果目标框
  88. for(int j = 0; j < im_vec_size - i; ++j) {
  89. for(int k = 0; k < results[j].boxes.size(); ++k) {
  90. std::cout << "image file: " << image_paths[i + j] << ", ";// << std::endl;
  91. std::cout << "predict label: " << results[j].boxes[k].category
  92. << ", label_id:" << results[j].boxes[k].category_id
  93. << ", score: " << results[j].boxes[k].score << ", box(xmin, ymin, w, h):("
  94. << results[j].boxes[k].coordinate[0] << ", "
  95. << results[j].boxes[k].coordinate[1] << ", "
  96. << results[j].boxes[k].coordinate[2] << ", "
  97. << results[j].boxes[k].coordinate[3] << ")" << std::endl;
  98. }
  99. }
  100. // 可视化
  101. for(int j = 0; j < im_vec_size - i; ++j) {
  102. cv::Mat vis_img =
  103. PaddleX::Visualize(im_vec[j], results[j], model.labels, colormap, FLAGS_threshold);
  104. std::string save_path =
  105. PaddleX::generate_save_path(FLAGS_save_dir, image_paths[i + j]);
  106. cv::imwrite(save_path, vis_img);
  107. std::cout << "Visualized output saved as " << save_path << std::endl;
  108. }
  109. }
  110. } else {
  111. PaddleX::DetResult result;
  112. cv::Mat im = cv::imread(FLAGS_image, 1);
  113. model.predict(im, &result);
  114. for (int i = 0; i < result.boxes.size(); ++i) {
  115. std::cout << "image file: " << FLAGS_image << std::endl;
  116. std::cout << ", predict label: " << result.boxes[i].category
  117. << ", label_id:" << result.boxes[i].category_id
  118. << ", score: " << result.boxes[i].score << ", box(xmin, ymin, w, h):("
  119. << result.boxes[i].coordinate[0] << ", "
  120. << result.boxes[i].coordinate[1] << ", "
  121. << result.boxes[i].coordinate[2] << ", "
  122. << result.boxes[i].coordinate[3] << ")" << std::endl;
  123. }
  124. // 可视化
  125. cv::Mat vis_img =
  126. PaddleX::Visualize(im, result, model.labels, colormap, FLAGS_threshold);
  127. std::string save_path =
  128. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_image);
  129. cv::imwrite(save_path, vis_img);
  130. result.clear();
  131. std::cout << "Visualized output saved as " << save_path << std::endl;
  132. }
  133. std::cout << "Total running time: "
  134. << total_running_time_s
  135. << " s, average running time: "
  136. << total_running_time_s / imgs
  137. << " s/img, total read img time: "
  138. << total_imread_time_s
  139. << " s, average read img time: "
  140. << total_imread_time_s / imgs
  141. << " s, batch_size = "
  142. << FLAGS_batch_size
  143. << std::endl;
  144. return 0;
  145. }