segmenter.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <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(device, "CPU", "Device name");
  28. DEFINE_string(save_dir, "", "Path to save visualized image");
  29. DEFINE_int32(batch_size, 1, "Batch size of infering");
  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. // load model
  45. PaddleX::Model model;
  46. model.Init(FLAGS_model_dir, FLAGS_cfg_file, FLAGS_device);
  47. int imgs = 1;
  48. auto colormap = PaddleX::GenerateColorMap(model.labels.size());
  49. if (FLAGS_image_list != "") {
  50. std::ifstream inf(FLAGS_image_list);
  51. if (!inf) {
  52. std::cerr << "Fail to open file " << FLAGS_image_list <<std::endl;
  53. return -1;
  54. }
  55. std::string image_path;
  56. while (getline(inf, image_path)) {
  57. PaddleX::SegResult result;
  58. cv::Mat im = cv::imread(image_path, 1);
  59. model.predict(im, &result);
  60. if (FLAGS_save_dir != "") {
  61. cv::Mat vis_img = PaddleX::Visualize(im, result, model.labels, colormap);
  62. std::string save_path =
  63. PaddleX::generate_save_path(FLAGS_save_dir, image_path);
  64. cv::imwrite(save_path, vis_img);
  65. std::cout << "Visualized output saved as " << save_path << std::endl;
  66. }
  67. }
  68. } else {
  69. PaddleX::SegResult result;
  70. cv::Mat im = cv::imread(FLAGS_image, 1);
  71. model.predict(im, &result);
  72. if (FLAGS_save_dir != "") {
  73. cv::Mat vis_img = PaddleX::Visualize(im, result, model.labels, colormap);
  74. std::string save_path =
  75. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_image);
  76. cv::imwrite(save_path, vis_img);
  77. std::cout << "Visualized` output saved as " << save_path << std::endl;
  78. }
  79. result.clear();
  80. }
  81. return 0;
  82. }