classifier.cpp 2.6 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 <fstream>
  16. #include <iostream>
  17. #include <string>
  18. #include <vector>
  19. #include "include/paddlex/paddlex.h"
  20. DEFINE_string(model_dir, "", "Path of inference model");
  21. DEFINE_string(cfg_file, "", "Path of PaddelX model yml file");
  22. DEFINE_string(image, "", "Path of test image file");
  23. DEFINE_string(image_list, "", "Path of test image list file");
  24. DEFINE_int32(thread_num, 1, "num of thread to infer");
  25. int main(int argc, char** argv) {
  26. // Parsing command-line
  27. google::ParseCommandLineFlags(&argc, &argv, true);
  28. if (FLAGS_model_dir == "") {
  29. std::cerr << "--model_dir need to be defined" << std::endl;
  30. return -1;
  31. }
  32. if (FLAGS_cfg_file == "") {
  33. std::cerr << "--cfg_flie 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. // load model
  41. PaddleX::Model model;
  42. model.Init(FLAGS_model_dir, FLAGS_cfg_file, FLAGS_thread_num);
  43. std::cout << "init is done" << std::endl;
  44. // predict
  45. if (FLAGS_image_list != "") {
  46. std::ifstream inf(FLAGS_image_list);
  47. if (!inf) {
  48. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  49. return -1;
  50. }
  51. std::string image_path;
  52. while (getline(inf, image_path)) {
  53. PaddleX::ClsResult result;
  54. cv::Mat im = cv::imread(image_path, 1);
  55. model.predict(im, &result);
  56. std::cout << "Predict label: " << result.category
  57. << ", label_id:" << result.category_id
  58. << ", score: " << result.score << std::endl;
  59. }
  60. } else {
  61. PaddleX::ClsResult result;
  62. cv::Mat im = cv::imread(FLAGS_image, 1);
  63. model.predict(im, &result);
  64. std::cout << "Predict label: " << result.category
  65. << ", label_id:" << result.category_id
  66. << ", score: " << result.score << std::endl;
  67. }
  68. return 0;
  69. }