classifier.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 <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_bool(use_gpu, false, "Infering with GPU or CPU");
  22. DEFINE_bool(use_trt, false, "Infering with TensorRT");
  23. DEFINE_int32(gpu_id, 0, "GPU card id");
  24. DEFINE_string(key, "", "key of encryption");
  25. DEFINE_string(image, "", "Path of test image file");
  26. DEFINE_string(image_list, "", "Path of test image list file");
  27. int main(int argc, char** argv) {
  28. // Parsing command-line
  29. google::ParseCommandLineFlags(&argc, &argv, true);
  30. if (FLAGS_model_dir == "") {
  31. std::cerr << "--model_dir need to be defined" << std::endl;
  32. return -1;
  33. }
  34. if (FLAGS_image == "" & FLAGS_image_list == "") {
  35. std::cerr << "--image or --image_list need to be defined" << std::endl;
  36. return -1;
  37. }
  38. // 加载模型
  39. PaddleX::Model model;
  40. model.Init(FLAGS_model_dir, FLAGS_use_gpu, FLAGS_use_trt, FLAGS_gpu_id, FLAGS_key);
  41. // 进行预测
  42. if (FLAGS_image_list != "") {
  43. std::ifstream inf(FLAGS_image_list);
  44. if (!inf) {
  45. std::cerr << "Fail to open file " << FLAGS_image_list << std::endl;
  46. return -1;
  47. }
  48. std::string image_path;
  49. while (getline(inf, image_path)) {
  50. PaddleX::ClsResult result;
  51. cv::Mat im = cv::imread(image_path, 1);
  52. model.predict(im, &result);
  53. std::cout << "Predict label: " << result.category
  54. << ", label_id:" << result.category_id
  55. << ", score: " << result.score << std::endl;
  56. }
  57. } else {
  58. PaddleX::ClsResult result;
  59. cv::Mat im = cv::imread(FLAGS_image, 1);
  60. model.predict(im, &result);
  61. std::cout << "Predict label: " << result.category
  62. << ", label_id:" << result.category_id
  63. << ", score: " << result.score << std::endl;
  64. }
  65. return 0;
  66. }