meter_reader.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <string>
  16. #include <iostream>
  17. #include <vector>
  18. #include <utility>
  19. #include <limits>
  20. #include <opencv2/opencv.hpp>
  21. #include <opencv2/highgui.hpp>
  22. #include <opencv2/core/core.hpp>
  23. #include "pipeline/include/pipeline.h"
  24. #include "meter_reader/include/reader_postprocess.h"
  25. DEFINE_string(pipeline_cfg, "", "Path of pipeline config file");
  26. DEFINE_bool(use_erode, true, "Eroding predicted label map");
  27. DEFINE_int32(erode_kernel, 4, "Eroding kernel size");
  28. DEFINE_string(image, "", "Path of test image file");
  29. DEFINE_string(save_dir, "", "Path to save visualized results");
  30. int main(int argc, char **argv) {
  31. google::ParseCommandLineFlags(&argc, &argv, true);
  32. if (FLAGS_pipeline_cfg == "") {
  33. std::cerr << "--pipeline_cfg need to be defined" << std::endl;
  34. return -1;
  35. }
  36. if (FLAGS_image == "") {
  37. std::cerr << "--image need to be defined "
  38. << "when the camera is not been used" << std::endl;
  39. return -1;
  40. }
  41. std::vector<std::string> image_paths = {FLAGS_image};
  42. PaddleXPipeline::Pipeline pipeline;
  43. if (pipeline.Init(FLAGS_pipeline_cfg)) {
  44. pipeline.SetInput("src0", image_paths);
  45. pipeline.Run();
  46. std::vector<PaddleDeploy::Result> det_results;
  47. std::vector<PaddleDeploy::Result> seg_results;
  48. pipeline.GetOutput("sink0", &det_results);
  49. pipeline.GetOutput("sink1", &seg_results);
  50. // Do image erosion for the predicted label map of each meter
  51. std::vector<std::vector<uint8_t>> seg_label_maps;
  52. Erode(FLAGS_erode_kernel, seg_results, &seg_label_maps);
  53. // The postprocess are done to get the reading or each meter
  54. std::vector<float> readings;
  55. GetMeterReading(seg_label_maps, &readings);
  56. PrintMeterReading(readings);
  57. if (FLAGS_save_dir != "") {
  58. cv::Mat img = cv::imread(FLAGS_image);
  59. cv::Mat vis_img;
  60. Visualize(img, det_results[0], readings, &vis_img);
  61. std::string save_path;
  62. if (PaddleXPipeline::GenerateSavePath(
  63. FLAGS_save_dir, FLAGS_image, &save_path)) {
  64. cv::imwrite(save_path, vis_img);
  65. }
  66. }
  67. }
  68. return 0;
  69. }