segmenter.cpp 3.0 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_dir, "", "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. int main(int argc, char** argv) {
  30. google::ParseCommandLineFlags(&argc, &argv, true);
  31. if (FLAGS_model_dir == "") {
  32. std::cerr << "--model_dir need to be defined" << std::endl;
  33. return -1;
  34. }
  35. if (FLAGS_cfg_dir == "") {
  36. std::cerr << "--cfg_dir need to be defined" << std::endl;
  37. return -1;
  38. }
  39. if (FLAGS_image == "" & FLAGS_image_list == "") {
  40. std::cerr << "--image or --image_list need to be defined" << std::endl;
  41. return -1;
  42. }
  43. //
  44. PaddleX::Model model;
  45. model.Init(FLAGS_model_dir, FLAGS_cfg_dir, FLAGS_device);
  46. int imgs = 1;
  47. auto colormap = PaddleX::GenerateColorMap(model.labels.size());
  48. if (FLAGS_image_list != "") {
  49. std::ifstream inf(FLAGS_image_list);
  50. if (!inf) {
  51. std::cerr << "Fail to open file " << FLAGS_image_list <<std::endl;
  52. return -1;
  53. }
  54. std::string image_path;
  55. while (getline(inf, image_path)) {
  56. PaddleX::SegResult result;
  57. cv::Mat im = cv::imread(image_path, 1);
  58. model.predict(im, &result);
  59. if(FLAGS_save_dir != ""){
  60. cv::Mat vis_img = PaddleX::Visualize(im, result, model.labels, colormap);
  61. std::string save_path =
  62. PaddleX::generate_save_path(FLAGS_save_dir, image_path);
  63. cv::imwrite(save_path, vis_img);
  64. std::cout << "Visualized output saved as " << save_path << std::endl;
  65. }
  66. }
  67. }else{
  68. PaddleX::SegResult result;
  69. cv::Mat im = cv::imread(FLAGS_image, 1);
  70. std::cout << "predict start" << std::endl;
  71. model.predict(im, &result);
  72. std::cout << "predict done" << std::endl;
  73. if(FLAGS_save_dir != ""){
  74. cv::Mat vis_img = PaddleX::Visualize(im, result, model.labels, colormap);
  75. std::string save_path =
  76. PaddleX::generate_save_path(FLAGS_save_dir, FLAGS_image);
  77. cv::imwrite(save_path, vis_img);
  78. std::cout << "Visualized` output saved as " << save_path << std::endl;
  79. }
  80. result.clear();
  81. }
  82. return 0;
  83. }