segmenter.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <fstream>
  17. #include <iostream>
  18. #include <string>
  19. #include <vector>
  20. #include <utility>
  21. #include "include/paddlex/paddlex.h"
  22. #include "include/paddlex/visualize.h"
  23. DEFINE_string(model_dir, "", "Path of openvino model xml file");
  24. DEFINE_string(cfg_file, "", "Path of PaddleX model yaml file");
  25. DEFINE_string(image, "", "Path of test image file");
  26. DEFINE_string(image_list, "", "Path of test image list file");
  27. DEFINE_string(save_dir, "", "Path to save visualized image");
  28. DEFINE_int32(batch_size, 1, "Batch size of infering");
  29. DEFINE_int32(thread_num, 1, "num of thread to infer");
  30. int main(int argc, char** argv) {
  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_cfg_file == "") {
  37. std::cerr << "--cfg_file need to be defined" << std::endl;
  38. return -1;
  39. }
  40. if (FLAGS_image == "" & FLAGS_image_list == "") {
  41. std::cerr << "--image or --image_list need to be defined" << std::endl;
  42. return -1;
  43. }
  44. //
  45. std::cout << "init start" << std::endl;
  46. PaddleX::Model model;
  47. model.Init(FLAGS_model_dir, FLAGS_cfg_file, FLAGS_thread_num);
  48. std::cout << "init done" << std::endl;
  49. int imgs = 1;
  50. auto colormap = PaddleX::GenerateColorMap(model.labels.size());
  51. if (FLAGS_image_list != "") {
  52. std::ifstream inf(FLAGS_image_list);
  53. if (!inf) {
  54. std::cerr << "Fail to open file " << FLAGS_image_list <<std::endl;
  55. return -1;
  56. }
  57. std::string image_path;
  58. while (getline(inf, image_path)) {
  59. PaddleX::SegResult result;
  60. cv::Mat im = cv::imread(image_path, 1);
  61. model.predict(im, &result);
  62. if (FLAGS_save_dir != "") {
  63. cv::Mat vis_img = PaddleX::Visualize(im, result, model.labels, colormap);
  64. std::string save_path =
  65. PaddleX::generate_save_path(FLAGS_save_dir, image_path);
  66. cv::imwrite(save_path, vis_img);
  67. std::cout << "Visualized output saved as " << save_path << std::endl;
  68. }
  69. }
  70. } else {
  71. PaddleX::SegResult result;
  72. cv::Mat im = cv::imread(FLAGS_image, 1);
  73. model.predict(im, &result);
  74. if (FLAGS_save_dir != "") {
  75. cv::Mat vis_img = PaddleX::Visualize(im, result, model.labels, colormap);
  76. std::string save_path =
  77. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_image);
  78. cv::imwrite(save_path, vis_img);
  79. std::cout << "Visualized` output saved as " << save_path << std::endl;
  80. }
  81. result.clear();
  82. }
  83. return 0;
  84. }