Skip to content

Commit

Permalink
[MOT] add cpplint & refine format in pptracking (PaddlePaddle#4501)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrywgz authored Nov 8, 2021
1 parent 305928b commit 531bca9
Show file tree
Hide file tree
Showing 22 changed files with 1,680 additions and 1,636 deletions.
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@
files: \.(md|yml)$
- id: remove-tabs
files: \.(md|yml)$
- repo: local
hooks:
- id: clang-format-with-version-check
name: clang-format
description: Format files with ClangFormat.
entry: bash ./.travis/codestyle/clang_format.hook -i
language: system
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto)$

- repo: local
hooks:
- id: cpplint-cpp-source
name: cpplint
description: Check C++ code style using cpplint.py.
entry: bash ./.travis/codestyle/cpplint_pre_commit.hook
language: system
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx)$
15 changes: 15 additions & 0 deletions .travis/codestyle/clang_format.hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e

readonly VERSION="3.8"

version=$(clang-format -version)

if ! [[ $version == *"$VERSION"* ]]; then
echo "clang-format version check failed."
echo "a version contains '$VERSION' is needed, but get '$version'"
echo "you can install the right version, and make an soft-link to '\$PATH' env"
exit -1
fi

clang-format $@
27 changes: 27 additions & 0 deletions .travis/codestyle/cpplint_pre_commit.hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

TOTAL_ERRORS=0
if [[ ! $TRAVIS_BRANCH ]]; then
# install cpplint on local machine.
if [[ ! $(which cpplint) ]]; then
pip install cpplint
fi
# diff files on local machine.
files=$(git diff --cached --name-status | awk '$1 != "D" {print $2}')
else
# diff files between PR and latest commit on Travis CI.
branch_ref=$(git rev-parse "$TRAVIS_BRANCH")
head_ref=$(git rev-parse HEAD)
files=$(git diff --name-status $branch_ref $head_ref | awk '$1 != "D" {print $2}')
fi
# The trick to remove deleted files: https://stackoverflow.com/a/2413151
for file in $files; do
if [[ $file =~ ^(patches/.*) ]]; then
continue;
else
cpplint --filter=-readability/fn_size,-build/include_what_you_use,-build/c++11 $file;
TOTAL_ERRORS=$(expr $TOTAL_ERRORS + $?);
fi
done

exit $TOTAL_ERRORS
8 changes: 3 additions & 5 deletions deploy/pptracking/include/config_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
#pragma once

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <string>
#include <vector>

#include "yaml-cpp/yaml.h"

Expand Down Expand Up @@ -47,8 +47,7 @@ class ConfigPaser {
mode_ = config["mode"].as<std::string>();
} else {
std::cerr << "Please set mode, "
<< "support value : fluid/trt_fp16/trt_fp32."
<< std::endl;
<< "support value : fluid/trt_fp16/trt_fp32." << std::endl;
return false;
}

Expand Down Expand Up @@ -136,4 +135,3 @@ class ConfigPaser {
};

} // namespace PaddleDetection

47 changes: 22 additions & 25 deletions deploy/pptracking/include/jde_predictor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,37 @@

#pragma once

#include <string>
#include <vector>
#include <ctime>
#include <memory>
#include <string>
#include <utility>
#include <ctime>
#include <vector>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include "paddle_inference_api.h" // NOLINT
#include "paddle_inference_api.h" // NOLINT

#include "include/preprocess_op.h"
#include "include/config_parser.h"
#include "include/preprocess_op.h"
#include "include/utils.h"

using namespace paddle_infer;
using namespace paddle_infer; // NOLINT

namespace PaddleDetection {

class JDEPredictor {
public:
explicit JDEPredictor(const std::string& device="CPU",
const std::string& model_dir="",
const double threshold=-1.,
const std::string& run_mode="fluid",
const int gpu_id=0,
const bool use_mkldnn=false,
const int cpu_threads=1,
bool trt_calib_mode=false,
const int min_box_area=200) {
explicit JDEPredictor(const std::string& device = "CPU",
const std::string& model_dir = "",
const double threshold = -1.,
const std::string& run_mode = "fluid",
const int gpu_id = 0,
const bool use_mkldnn = false,
const int cpu_threads = 1,
bool trt_calib_mode = false,
const int min_box_area = 200) {
this->device_ = device;
this->gpu_id_ = gpu_id;
this->use_mkldnn_ = use_mkldnn;
Expand All @@ -60,15 +60,14 @@ class JDEPredictor {
}

// Load Paddle inference model
void LoadModel(
const std::string& model_dir,
const std::string& run_mode = "fluid");
void LoadModel(const std::string& model_dir,
const std::string& run_mode = "fluid");

// Run predictor
void Predict(const std::vector<cv::Mat> imgs,
const double threshold = 0.5,
MOTResult* result = nullptr,
std::vector<double>* times = nullptr);
const double threshold = 0.5,
MOTResult* result = nullptr,
std::vector<double>* times = nullptr);

private:
std::string device_ = "CPU";
Expand All @@ -82,9 +81,7 @@ class JDEPredictor {
// Preprocess image and copy data to input buffer
void Preprocess(const cv::Mat& image_mat);
// Postprocess result
void Postprocess(
const cv::Mat dets, const cv::Mat emb,
MOTResult* result);
void Postprocess(const cv::Mat dets, const cv::Mat emb, MOTResult* result);

std::shared_ptr<Predictor> predictor_;
Preprocessor preprocessor_;
Expand Down
36 changes: 24 additions & 12 deletions deploy/pptracking/include/lapjv.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
// Ths copyright of gatagat/lap is as follows:
// MIT License

#ifndef LAPJV_H
#define LAPJV_H

#ifndef DEPLOY_PPTRACKING_INCLUDE_LAPJV_H_
#define DEPLOY_PPTRACKING_INCLUDE_LAPJV_H_
#define LARGE 1000000

#if !defined TRUE
Expand All @@ -29,9 +28,21 @@
#define FALSE 0
#endif

#define NEW(x, t, n) if ((x = (t *)malloc(sizeof(t) * (n))) == 0) {return -1;}
#define FREE(x) if (x != 0) { free(x); x = 0; }
#define SWAP_INDICES(a, b) { int_t _temp_index = a; a = b; b = _temp_index; }
#define NEW(x, t, n) \
if ((x = reinterpret_cast<t *>(malloc(sizeof(t) * (n)))) == 0) { \
return -1; \
}
#define FREE(x) \
if (x != 0) { \
free(x); \
x = 0; \
}
#define SWAP_INDICES(a, b) \
{ \
int_t _temp_index = a; \
a = b; \
b = _temp_index; \
}
#include <opencv2/opencv.hpp>

namespace PaddleDetection {
Expand All @@ -42,11 +53,12 @@ typedef double cost_t;
typedef char boolean;
typedef enum fp_t { FP_1 = 1, FP_2 = 2, FP_DYNAMIC = 3 } fp_t;

int lapjv_internal(
const cv::Mat &cost, const bool extend_cost, const float cost_limit,
int *x, int *y);

} // namespace PaddleDetection
int lapjv_internal(const cv::Mat &cost,
const bool extend_cost,
const float cost_limit,
int *x,
int *y);

#endif // LAPJV_H
} // namespace PaddleDetection

#endif // DEPLOY_PPTRACKING_INCLUDE_LAPJV_H_
65 changes: 37 additions & 28 deletions deploy/pptracking/include/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef DEPLOY_PPTRACKING_INCLUDE_PIPELINE_H_
#define DEPLOY_PPTRACKING_INCLUDE_PIPELINE_H_

#include <glog/logging.h>

#include <math.h>
#include <sys/types.h>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#include <numeric>
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>
#include <algorithm>

#ifdef _WIN32
#include <direct.h>
Expand All @@ -39,18 +41,18 @@ namespace PaddleDetection {
class Pipeline {
public:
explicit Pipeline(const std::string& device,
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,
const bool count=false,
const bool save_result=false,
const std::string& scene="pedestrian",
const bool tiny_obj=false,
const bool is_mtmct=false) {
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,
const bool count = false,
const bool save_result = false,
const std::string& scene = "pedestrian",
const bool tiny_obj = false,
const bool is_mtmct = false) {
std::vector<std::string> input;
this->input_ = input;
this->device_ = device;
Expand All @@ -67,10 +69,8 @@ class Pipeline {
InitPredictor();
}



// Set input, it must execute before Run()
void SetInput(std::string& input_video);
void SetInput(const std::string& input_video);
void ClearInput();

// Run pipeline in video
Expand All @@ -79,16 +79,23 @@ class Pipeline {
void PredictMTMCT(const std::vector<std::string> video_inputs);

// Run pipeline in stream
void RunMOTStream(const cv::Mat img, const int frame_id, cv::Mat& out_img, std::vector<std::string>& records, std::vector<int>& count_list, std::vector<int>& in_count_list, std::vector<int>& out_count_list);
void RunMTMCTStream(const std::vector<cv::Mat> imgs, std::vector<std::string>& records);

void PrintBenchmarkLog(std::vector<double> det_time, int img_num);
void RunMOTStream(const cv::Mat img,
const int frame_id,
cv::Mat out_img,
std::vector<std::string>* records,
std::vector<int>* count_list,
std::vector<int>* in_count_list,
std::vector<int>* out_count_list);
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);

private:
// Select model according to scenes, it must execute before Run()
void SelectModel(const std::string& scene="pedestrian",
const bool tiny_obj=false,
const bool is_mtmct=false);
void SelectModel(const std::string& scene = "pedestrian",
const bool tiny_obj = false,
const bool is_mtmct = false);
void InitPredictor();

std::shared_ptr<PaddleDetection::JDEPredictor> jde_sct_;
Expand All @@ -111,4 +118,6 @@ class Pipeline {
bool save_result_ = false;
};

} // namespace PaddleDetection
} // namespace PaddleDetection

#endif // DEPLOY_PPTRACKING_INCLUDE_PIPELINE_H_
26 changes: 15 additions & 11 deletions deploy/pptracking/include/postprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

#pragma once

#include <string>
#include <vector>
#include <ctime>
#include <memory>
#include <string>
#include <utility>
#include <ctime>
#include <vector>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include "include/utils.h"

Expand All @@ -33,16 +33,20 @@ cv::Scalar GetColor(int idx);

// Visualize Tracking Results
cv::Mat VisualizeTrackResult(const cv::Mat& img,
const MOTResult& results,
const float fps, const int frame_id);
const MOTResult& results,
const float fps,
const int frame_id);

// Pedestrian/Vehicle Counting
void FlowStatistic(const MOTResult& results, const int frame_id,
std::vector<int>* count_list,
std::vector<int>* in_count_list,
void FlowStatistic(const MOTResult& results,
const int frame_id,
std::vector<int>* count_list,
std::vector<int>* in_count_list,
std::vector<int>* out_count_list);

// Save Tracking Results
void SaveMOTResult(const MOTResult& results, const int frame_id, std::vector<std::string>& records);
void SaveMOTResult(const MOTResult& results,
const int frame_id,
std::vector<std::string>* records);

} // namespace PaddleDetection
} // namespace PaddleDetection
Loading

0 comments on commit 531bca9

Please sign in to comment.