main.cpp 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include <chrono>
#include <iomanip>
#include <iostream>
L
littletomatodonkey 已提交
21
#include <opencv2/core/utils/filesystem.hpp>
22 23 24 25 26 27 28 29
#include <ostream>
#include <vector>

#include <cstring>
#include <fstream>
#include <numeric>

#include <include/cls.h>
L
littletomatodonkey 已提交
30
#include <include/cls_config.h>
31 32 33 34 35 36 37 38 39 40 41 42

using namespace std;
using namespace cv;
using namespace PaddleClas;

int main(int argc, char **argv) {
  if (argc < 3) {
    std::cerr << "[ERROR] usage: " << argv[0]
              << " configure_filepath image_path\n";
    exit(1);
  }

L
littletomatodonkey 已提交
43
  ClsConfig config(argv[1]);
44 45 46

  config.PrintConfigInfo();

L
littletomatodonkey 已提交
47
  std::string path(argv[2]);
48

L
littletomatodonkey 已提交
49 50 51 52 53 54 55 56 57 58 59 60
  std::vector<std::string> img_files_list;
  if (cv::utils::fs::isDirectory(path)) {
    std::vector<cv::String> filenames;
    cv::glob(path, filenames);
    for (auto f : filenames) {
      img_files_list.push_back(f);
    }
  } else {
    img_files_list.push_back(path);
  }

  std::cout << "img_file_list length: " << img_files_list.size() << std::endl;
61

62 63 64
  Classifier classifier(config.cls_model_path, config.cls_params_path,
                        config.use_gpu, config.gpu_id, config.gpu_mem,
                        config.cpu_math_library_num_threads, config.use_mkldnn,
L
littletomatodonkey 已提交
65
                        config.use_tensorrt, config.use_fp16,
66
                        config.resize_short_size, config.crop_size);
67

L
littletomatodonkey 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  double elapsed_time = 0.0;
  int warmup_iter = img_files_list.size() > 5 ? 5 : 0;
  for (int idx = 0; idx < img_files_list.size(); ++idx) {
    std::string img_path = img_files_list[idx];
    cv::Mat srcimg = cv::imread(img_path, cv::IMREAD_COLOR);
    cv::cvtColor(srcimg, srcimg, cv::COLOR_BGR2RGB);

    auto start = std::chrono::system_clock::now();
    classifier.Run(srcimg);
    auto end = std::chrono::system_clock::now();
    auto duration =
        std::chrono::duration_cast<std::chrono::microseconds>(end - start);
    double curr_time = double(duration.count()) *
                       std::chrono::microseconds::period::num /
                       std::chrono::microseconds::period::den;
    if (idx >= warmup_iter) {
      elapsed_time += curr_time;
L
littletomatodonkey 已提交
85 86 87 88 89 90
      std::cout << "Current image path: " << img_path << std::endl;
      std::cout << "Current time cost: " << curr_time << " s, "
                << "average time cost in all: "
                << elapsed_time / (idx + 1 - warmup_iter) << " s." << std::endl;
    } else {
      std::cout << "Current time cost: " << curr_time << " s." << std::endl;
L
littletomatodonkey 已提交
91 92
    }
  }
93 94 95

  return 0;
}