detector.cpp 4.2 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 <glog/logging.h>
  15. #include <fstream>
  16. #include <iostream>
  17. #include <string>
  18. #include <vector>
  19. #include "include/paddlex/paddlex.h"
  20. #include "include/paddlex/visualize.h"
  21. DEFINE_string(model_dir, "", "Path of inference model");
  22. DEFINE_bool(use_gpu, false, "Infering with GPU or CPU");
  23. DEFINE_bool(use_trt, false, "Infering with TensorRT");
  24. DEFINE_int32(gpu_id, 0, "GPU card id");
  25. DEFINE_string(key, "", "key of encryption");
  26. DEFINE_string(image, "", "Path of test image file");
  27. DEFINE_string(image_list, "", "Path of test image list file");
  28. DEFINE_string(save_dir, "output", "Path to save visualized image");
  29. int main(int argc, char** argv) {
  30. // 解析命令行参数
  31. google::ParseCommandLineFlags(&argc, &argv, true);
  32. if (FLAGS_model_dir == "") {
  33. std::cerr << "--model_dir need to be defined" << std::endl;
  34. return -1;
  35. }
  36. if (FLAGS_image == "" & FLAGS_image_list == "") {
  37. std::cerr << "--image or --image_list need to be defined" << std::endl;
  38. return -1;
  39. }
  40. // 加载模型
  41. PaddleX::Model model;
  42. model.Init(FLAGS_model_dir, FLAGS_use_gpu, FLAGS_use_trt, FLAGS_gpu_id, FLAGS_key);
  43. auto colormap = PaddleX::GenerateColorMap(model.labels.size());
  44. std::string save_dir = "output";
  45. // 进行预测
  46. if (FLAGS_image_list != "") {
  47. std::ifstream inf(FLAGS_image_list);
  48. if (!inf) {
  49. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  50. return -1;
  51. }
  52. std::string image_path;
  53. while (getline(inf, image_path)) {
  54. PaddleX::DetResult result;
  55. cv::Mat im = cv::imread(image_path, 1);
  56. model.predict(im, &result);
  57. for (int i = 0; i < result.boxes.size(); ++i) {
  58. std::cout << "image file: " << image_path
  59. << ", predict label: " << result.boxes[i].category
  60. << ", label_id:" << result.boxes[i].category_id
  61. << ", score: " << result.boxes[i].score << ", box(xmin, ymin, w, h):("
  62. << result.boxes[i].coordinate[0] << ", "
  63. << result.boxes[i].coordinate[1] << ", "
  64. << result.boxes[i].coordinate[2] << ", "
  65. << result.boxes[i].coordinate[3] << ")" << std::endl;
  66. }
  67. // 可视化
  68. cv::Mat vis_img =
  69. PaddleX::Visualize(im, result, model.labels, colormap, 0.5);
  70. std::string save_path =
  71. PaddleX::generate_save_path(FLAGS_save_dir, image_path);
  72. cv::imwrite(save_path, vis_img);
  73. result.clear();
  74. std::cout << "Visualized output saved as " << save_path << std::endl;
  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 << ", predict label: " << result.boxes[i].category
  82. << ", label_id:" << result.boxes[i].category_id
  83. << ", score: " << result.boxes[i].score << ", box(xmin, ymin, w, h):("
  84. << 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. // 可视化
  90. cv::Mat vis_img =
  91. PaddleX::Visualize(im, result, model.labels, colormap, 0.5);
  92. std::string save_path =
  93. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_image);
  94. cv::imwrite(save_path, vis_img);
  95. result.clear();
  96. std::cout << "Visualized output saved as " << save_path << std::endl;
  97. }
  98. return 0;
  99. }