pipeline.h 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//   Copyright (c) 2021 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.

15 16
#ifndef DEPLOY_PPTRACKING_CPP_INCLUDE_PIPELINE_H_
#define DEPLOY_PPTRACKING_CPP_INCLUDE_PIPELINE_H_
17

18 19
#include <glog/logging.h>

20 21 22
#include <math.h>
#include <sys/types.h>
#include <algorithm>
23
#include <iostream>
24
#include <numeric>
25 26 27 28 29 30 31 32 33 34 35
#include <string>
#include <vector>

#ifdef _WIN32
#include <direct.h>
#include <io.h>
#elif LINUX
#include <stdarg.h>
#include <sys/stat.h>
#endif

W
wangguanzhong 已提交
36 37
#include "include/jde_predictor.h"
#include "include/sde_predictor.h"
38 39 40 41 42 43

namespace PaddleDetection {

class Pipeline {
 public:
  explicit Pipeline(const std::string& device,
44 45 46 47 48 49 50
                    const double threshold,
                    const std::string& output_dir,
                    const std::string& run_mode = "fluid",
                    const int gpu_id = 0,
                    const bool use_mkldnn = false,
                    const int cpu_threads = 1,
                    const bool trt_calib_mode = false,
51
                    const bool do_entrance_counting = false,
52 53 54
                    const bool save_result = false,
                    const std::string& scene = "pedestrian",
                    const bool tiny_obj = false,
55
                    const bool is_mtmct = false,
56 57 58 59
                    const int secs_interval = 10,
                    const std::string track_model_dir = "",
                    const std::string det_model_dir = "",
                    const std::string reid_model_dir = "") {
60 61 62 63 64 65 66 67 68 69
    std::vector<std::string> input;
    this->input_ = input;
    this->device_ = device;
    this->threshold_ = threshold;
    this->output_dir_ = output_dir;
    this->run_mode_ = run_mode;
    this->gpu_id_ = gpu_id;
    this->use_mkldnn_ = use_mkldnn;
    this->cpu_threads_ = cpu_threads;
    this->trt_calib_mode_ = trt_calib_mode;
70 71
    this->do_entrance_counting_ = do_entrance_counting;
    this->secs_interval_ = secs_interval_;
72
    this->save_result_ = save_result;
73 74 75 76 77 78
    SelectModel(scene,
                tiny_obj,
                is_mtmct,
                track_model_dir,
                det_model_dir,
                reid_model_dir);
W
wangguanzhong 已提交
79
    InitPredictor();
80 81 82
  }

  // Set input, it must execute before Run()
83
  void SetInput(const std::string& input_video);
W
wangguanzhong 已提交
84
  void ClearInput();
85

W
wangguanzhong 已提交
86
  // Run pipeline in video
87
  void Run();
W
wangguanzhong 已提交
88 89
  void PredictMOT(const std::string& video_path);
  void PredictMTMCT(const std::vector<std::string> video_inputs);
90

W
wangguanzhong 已提交
91
  // Run pipeline in stream
92 93
  void RunMOTStream(const cv::Mat img,
                    const int frame_id,
94 95
                    const int video_fps,
                    const Rect entrance,
96 97
                    cv::Mat out_img,
                    std::vector<std::string>* records,
98 99
                    std::set<int>* count_set,
                    std::set<int>* interval_count_set,
100
                    std::vector<int>* in_count_list,
101 102 103
                    std::vector<int>* out_count_list,
                    std::map<int, std::vector<float>>* prev_center,
                    std::vector<std::string>* flow_records);
104 105 106 107
  void RunMTMCTStream(const std::vector<cv::Mat> imgs,
                      std::vector<std::string>* records);

  void PrintBenchmarkLog(const std::vector<double> det_time, const int img_num);
108 109

 private:
W
wangguanzhong 已提交
110
  // Select model according to scenes, it must execute before Run()
111 112
  void SelectModel(const std::string& scene = "pedestrian",
                   const bool tiny_obj = false,
113 114 115 116
                   const bool is_mtmct = false,
                   const std::string track_model_dir = "",
                   const std::string det_model_dir = "",
                   const std::string reid_model_dir = "");
W
wangguanzhong 已提交
117 118 119 120 121
  void InitPredictor();

  std::shared_ptr<PaddleDetection::JDEPredictor> jde_sct_;
  std::shared_ptr<PaddleDetection::SDEPredictor> sde_sct_;

122
  std::vector<std::string> input_;
W
wangguanzhong 已提交
123
  std::vector<cv::Mat> stream_;
124 125 126 127 128 129 130 131 132 133 134
  std::string device_;
  double threshold_;
  std::string output_dir_;
  std::string track_model_dir_;
  std::string det_model_dir_;
  std::string reid_model_dir_;
  std::string run_mode_ = "fluid";
  int gpu_id_ = 0;
  bool use_mkldnn_ = false;
  int cpu_threads_ = 1;
  bool trt_calib_mode_ = false;
135
  bool do_entrance_counting_ = false;
136
  bool save_result_ = false;
137
  int secs_interval_ = 10;
138 139
};

140 141
}  // namespace PaddleDetection

142
#endif  // DEPLOY_PPTRACKING_CPP_INCLUDE_PIPELINE_H_