detector.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. using namespace std::chrono; // NOLINT
  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_bool(use_mkl, true, "Infering with MKL");
  30. DEFINE_int32(mkl_thread_num,
  31. omp_get_num_procs(),
  32. "Number of mkl threads");
  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_string(image_list, "", "Path of test image list file");
  37. DEFINE_string(save_dir, "output", "Path to save visualized image");
  38. DEFINE_int32(batch_size, 1, "Batch size of infering");
  39. DEFINE_double(threshold,
  40. 0.5,
  41. "The minimum scores of target boxes which are shown");
  42. DEFINE_int32(thread_num,
  43. omp_get_num_procs(),
  44. "Number of preprocessing threads");
  45. int main(int argc, char** argv) {
  46. // Parsing command-line
  47. google::ParseCommandLineFlags(&argc, &argv, true);
  48. if (FLAGS_model_dir == "") {
  49. std::cerr << "--model_dir need to be defined" << std::endl;
  50. return -1;
  51. }
  52. if (FLAGS_image == "" & FLAGS_image_list == "") {
  53. std::cerr << "--image or --image_list need to be defined" << std::endl;
  54. return -1;
  55. }
  56. // Load model
  57. PaddleX::Model model;
  58. model.Init(FLAGS_model_dir,
  59. FLAGS_use_gpu,
  60. FLAGS_use_trt,
  61. FLAGS_use_mkl,
  62. FLAGS_mkl_thread_num,
  63. FLAGS_gpu_id,
  64. FLAGS_key);
  65. int imgs = 1;
  66. std::string save_dir = "output";
  67. // Predict
  68. if (FLAGS_image_list != "") {
  69. std::ifstream inf(FLAGS_image_list);
  70. if (!inf) {
  71. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  72. return -1;
  73. }
  74. std::string image_path;
  75. std::vector<std::string> image_paths;
  76. while (getline(inf, image_path)) {
  77. image_paths.push_back(image_path);
  78. }
  79. imgs = image_paths.size();
  80. for (int i = 0; i < image_paths.size(); i += FLAGS_batch_size) {
  81. int im_vec_size =
  82. std::min(static_cast<int>(image_paths.size()), i + FLAGS_batch_size);
  83. std::vector<cv::Mat> im_vec(im_vec_size - i);
  84. std::vector<PaddleX::DetResult> results(im_vec_size - i,
  85. PaddleX::DetResult());
  86. int thread_num = std::min(FLAGS_thread_num, im_vec_size - i);
  87. #pragma omp parallel for num_threads(thread_num)
  88. for (int j = i; j < im_vec_size; ++j) {
  89. im_vec[j - i] = std::move(cv::imread(image_paths[j], 1));
  90. }
  91. model.predict(im_vec, &results, thread_num);
  92. // Output predicted bounding boxes
  93. for (int j = 0; j < im_vec_size - i; ++j) {
  94. for (int k = 0; k < results[j].boxes.size(); ++k) {
  95. std::cout << "image file: " << image_paths[i + j] << ", ";
  96. std::cout << "predict label: " << results[j].boxes[k].category
  97. << ", label_id:" << results[j].boxes[k].category_id
  98. << ", score: " << results[j].boxes[k].score
  99. << ", box(xmin, ymin, w, h):("
  100. << results[j].boxes[k].coordinate[0] << ", "
  101. << results[j].boxes[k].coordinate[1] << ", "
  102. << results[j].boxes[k].coordinate[2] << ", "
  103. << results[j].boxes[k].coordinate[3] << ")" << std::endl;
  104. }
  105. }
  106. // Visualize results
  107. for (int j = 0; j < im_vec_size - i; ++j) {
  108. cv::Mat vis_img = PaddleX::Visualize(
  109. im_vec[j], results[j], model.labels, FLAGS_threshold);
  110. std::string save_path =
  111. PaddleX::generate_save_path(FLAGS_save_dir, image_paths[i + j]);
  112. cv::imwrite(save_path, vis_img);
  113. std::cout << "Visualized output saved as " << save_path << std::endl;
  114. }
  115. }
  116. } else {
  117. PaddleX::DetResult result;
  118. cv::Mat im = cv::imread(FLAGS_image, 1);
  119. model.predict(im, &result);
  120. // Output predicted bounding boxes
  121. for (int i = 0; i < result.boxes.size(); ++i) {
  122. std::cout << "image file: " << FLAGS_image << std::endl;
  123. std::cout << ", predict label: " << result.boxes[i].category
  124. << ", label_id:" << result.boxes[i].category_id
  125. << ", score: " << result.boxes[i].score
  126. << ", box(xmin, ymin, w, h):(" << result.boxes[i].coordinate[0]
  127. << ", " << result.boxes[i].coordinate[1] << ", "
  128. << result.boxes[i].coordinate[2] << ", "
  129. << result.boxes[i].coordinate[3] << ")" << std::endl;
  130. }
  131. // Visualize results
  132. cv::Mat vis_img =
  133. PaddleX::Visualize(im, result, model.labels, FLAGS_threshold);
  134. std::string save_path =
  135. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_image);
  136. cv::imwrite(save_path, vis_img);
  137. result.clear();
  138. std::cout << "Visualized output saved as " << save_path << std::endl;
  139. }
  140. return 0;
  141. }