detector.cpp 3.9 KB

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