detector.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 openvino model xml file");
  27. DEFINE_string(cfg_file, "", "Path of PaddleX model yaml file");
  28. DEFINE_string(image, "", "Path of test image file");
  29. DEFINE_string(image_list, "", "Path of test image list file");
  30. DEFINE_int32(thread_num, 1, "num of thread to infer");
  31. DEFINE_string(save_dir, "", "Path to save visualized image");
  32. DEFINE_int32(batch_size, 1, "Batch size of infering");
  33. DEFINE_double(threshold,
  34. 0.5,
  35. "The minimum scores of target boxes which are shown");
  36. int main(int argc, char** argv) {
  37. google::ParseCommandLineFlags(&argc, &argv, true);
  38. if (FLAGS_model_dir == "") {
  39. std::cerr << "--model_dir need to be defined" << std::endl;
  40. return -1;
  41. }
  42. if (FLAGS_cfg_file == "") {
  43. std::cerr << "--cfg_file need to be defined" << std::endl;
  44. return -1;
  45. }
  46. if (FLAGS_image == "" & FLAGS_image_list == "") {
  47. std::cerr << "--image or --image_list need to be defined" << std::endl;
  48. return -1;
  49. }
  50. // load model
  51. PaddleX::Model model;
  52. model.Init(FLAGS_model_dir, FLAGS_cfg_file, FLAGS_thread_num);
  53. int imgs = 1;
  54. auto colormap = PaddleX::GenerateColorMap(model.labels.size());
  55. // predict
  56. if (FLAGS_image_list != "") {
  57. std::ifstream inf(FLAGS_image_list);
  58. if (!inf) {
  59. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  60. return -1;
  61. }
  62. std::string image_path;
  63. while (getline(inf, image_path)) {
  64. PaddleX::DetResult result;
  65. cv::Mat im = cv::imread(image_path, 1);
  66. model.predict(im, &result);
  67. if (FLAGS_save_dir != "") {
  68. cv::Mat vis_img = PaddleX::Visualize(
  69. im, result, model.labels, colormap, FLAGS_threshold);
  70. std::string save_path =
  71. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_image);
  72. cv::imwrite(save_path, vis_img);
  73. std::cout << "Visualized output saved as " << save_path << std::endl;
  74. }
  75. }
  76. } else {
  77. PaddleX::DetResult result;
  78. cv::Mat im = cv::imread(FLAGS_image, 1);
  79. model.predict(im, &result);
  80. for (int i = 0; i < result.boxes.size(); ++i) {
  81. std::cout << "image file: " << FLAGS_image << std::endl;
  82. std::cout << ", predict label: " << result.boxes[i].category
  83. << ", label_id:" << result.boxes[i].category_id
  84. << ", score: " << result.boxes[i].score
  85. << ", box(xmin, ymin, w, h):(" << result.boxes[i].coordinate[0]
  86. << ", " << result.boxes[i].coordinate[1] << ", "
  87. << result.boxes[i].coordinate[2] << ", "
  88. << result.boxes[i].coordinate[3] << ")" << std::endl;
  89. }
  90. if (FLAGS_save_dir != "") {
  91. // visualize
  92. cv::Mat vis_img = PaddleX::Visualize(
  93. im, result, model.labels, colormap, FLAGS_threshold);
  94. std::string save_path =
  95. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_image);
  96. cv::imwrite(save_path, vis_img);
  97. result.clear();
  98. std::cout << "Visualized output saved as " << save_path << std::endl;
  99. }
  100. }
  101. return 0;
  102. }