forked from shouxieai/tensorRT_Pro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_fall_recognize.cpp
More file actions
133 lines (110 loc) · 4.54 KB
/
app_fall_recognize.cpp
File metadata and controls
133 lines (110 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <string.h>
#include <opencv2/opencv.hpp>
#include <builder/trt_builder.hpp>
#include <infer/trt_infer.hpp>
#include <common/ilogger.hpp>
#include "app_yolo/yolo.hpp"
#include "app_alphapose/alpha_pose.hpp"
#include "app_fall_gcn/fall_gcn.hpp"
#include "tools/zmq_remote_show.hpp"
#include "tools/deepsort.hpp"
using namespace cv;
using namespace std;
bool requires(const char* name);
static bool compile_models(){
TRT::set_device(0);
const char* onnx_files[]{"yolox_m", "sppe", "fall_bp"};
for(auto& name : onnx_files){
if(not requires(name))
return false;
string onnx_file = iLogger::format("%s.onnx", name);
string model_file = iLogger::format("%s.FP32.trtmodel", name);
int test_batch_size = 1;
if(not iLogger::exists(model_file)){
bool ok = TRT::compile(
TRT::Mode::FP32, // FP32、FP16、INT8
test_batch_size, // max batch size
onnx_file, // source
model_file // save to
);
if(!ok) return false;
}
}
return true;
}
int app_fall_recognize(){
cv::setNumThreads(0);
INFO("===================== test alphapose FP32 ==================================");
if(!compile_models())
return 0;
auto pose_model_file = "sppe.FP32.trtmodel";
auto detector_model_file = "yolox_m.FP32.trtmodel";
auto gcn_model_file = "fall_bp.FP32.trtmodel";
auto pose_model = AlphaPose::create_infer(pose_model_file, 0);
auto detector_model = Yolo::create_infer(detector_model_file, Yolo::Type::X, 0, 0.4f);
auto gcn_model = FallGCN::create_infer(gcn_model_file, 0);
Mat image;
VideoCapture cap("exp/fall_video.mp4");
INFO("Video fps=%d, Width=%d, Height=%d",
(int)cap.get(cv::CAP_PROP_FPS),
(int)cap.get(cv::CAP_PROP_FRAME_WIDTH),
(int)cap.get(cv::CAP_PROP_FRAME_HEIGHT)
);
//auto remote_show = create_zmq_remote_show();
INFO("Use tools/show.py to remote show");
auto config = DeepSORT::TrackerConfig();
config.set_initiate_state({
0.1, 0.1, 0.1, 0.1,
0.2, 0.2, 1, 0.2
});
config.set_per_frame_motion({
0.1, 0.1, 0.1, 0.1,
0.2, 0.2, 1, 0.2
});
auto tracker = DeepSORT::create_tracker(config);
// VideoWriter writer("fall_video.result.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'),
// 30,
// Size(cap.get(cv::CAP_PROP_FRAME_WIDTH), cap.get(cv::CAP_PROP_FRAME_HEIGHT))
// );
// if(!writer.isOpened()){
// INFOE("Writer failed.");
// return 0;
// }
while(cap.read(image)){
auto objects = detector_model->commit(image).get();
vector<DeepSORT::Box> boxes;
for(int i = 0; i < objects.size(); ++i){
auto& obj = objects[i];
if(obj.class_label != 0) continue;
boxes.emplace_back(std::move(DeepSORT::convert_to_box(obj)));
}
tracker->update(boxes);
auto final_objects = tracker->get_objects();
for(int i = 0; i < final_objects.size(); ++i){
auto& person = final_objects[i];
if(person->time_since_update() == 0 && person->state() == DeepSORT::State::Confirmed){
Rect box = DeepSORT::convert_box_to_rect(person->last_position());
auto keys = pose_model->commit(make_tuple(image, box)).get();
auto statev = gcn_model->commit(make_tuple(keys, box)).get();
FallGCN::FallState state = get<0>(statev);
float confidence = get<1>(statev);
const char* label_name = FallGCN::state_name(state);
rectangle(image, DeepSORT::convert_box_to_rect(person->predict_box()), Scalar(0, 255, 0), 1);
rectangle(image, box, Scalar(0, 255, 255), 1);
auto line = person->trace_line();
for(int j = 0; j < (int)line.size() - 1; ++j){
auto& p = line[j];
auto& np = line[j + 1];
cv::line(image, p, np, Scalar(255, 128, 60), 2, 16);
}
putText(image, iLogger::format("%d. [%s] %.2f %%", person->id(), label_name, confidence * 100), box.tl(), 0, 1, Scalar(0, 255, 0), 2, 16);
//INFO("Predict is [%s], %.2f %%", label_name, confidence * 100);
}
}
//remote_show->post(image);
//writer.write(image);
}
INFO("Done");
return 0;
}