a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
1
2
3
4
5
6
7
8
9
|
#include "FFNvDecoderManager.h"
#include <iostream>
#include <thread>
#include <chrono>
#include "check_tool.h"
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
10
11
12
|
#include "cuda_kernels.h"
#include "NvJpegEncoder.h"
|
61501775
Hu Chunming
添加showFrame
|
13
14
|
#include "opencv2\opencv.hpp"
|
69bdb744
Hu Chunming
改用onnx模型
|
15
|
#include "DogPoseDetectorOnnx.h"
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
16
17
|
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
18
|
using namespace std;
|
61501775
Hu Chunming
添加showFrame
|
19
|
using namespace cv;
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
20
21
22
23
24
25
|
unsigned char *pHwRgb[2] = {nullptr, nullptr};
int sum1 = 0;
int sum2 = 0;
|
69bdb744
Hu Chunming
改用onnx模型
|
26
|
DogPoseDetectorOnnx poseDetector;
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
27
|
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
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
|
mutex m_mutex;
void saveFrame(AVFrame * gpuFrame, string file_name) {
std::lock_guard<std::mutex> l(m_mutex);
unsigned char *pHwData = nullptr;
cudaError_t cudaStatus = cudaMalloc((void **)&pHwData, 3 * gpuFrame->width * gpuFrame->height * sizeof(unsigned char));
cuda_common::setColorSpace(ITU709, 0);
cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0], (CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], pHwData, gpuFrame->width, gpuFrame->height);
cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
cout << "CUDAToBGR failed !!!" << endl;
return;
}
string path = file_name + ".jpg";
saveJpeg(path.c_str(), pHwData, gpuFrame->width, gpuFrame->height, nullptr); // 验证 CUDAToRGB
cudaDeviceSynchronize();
cudaFree(pHwData);
pHwData = nullptr;
}
|
69bdb744
Hu Chunming
改用onnx模型
|
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
|
static void showResult(unsigned char *pGpuBgr, int src_width, int src_height, std::vector<DogPoseResult> vec_result) {
int buf_size = 3 * src_width * src_height;
unsigned char* pBuf = new unsigned char[buf_size];
cudaMemcpy(pBuf, pGpuBgr, buf_size * sizeof(unsigned char), cudaMemcpyDeviceToHost);
cv::Mat image(src_height, src_width, CV_8UC3, pBuf);
for (size_t i = 0; i < vec_result.size(); i++)
{
DogPoseResult poseResult = vec_result[i];
std::cout << poseResult.x << std::endl;
std::cout << poseResult.y << std::endl;
std::cout << poseResult.width << std::endl;
std::cout << poseResult.height << std::endl;
std::cout << poseResult.confidence << std::endl;
std::cout << poseResult.classId << std::endl;
std::cout << poseResult.className << std::endl;
cv::Rect position_boxe;
position_boxe.x = poseResult.x;
position_boxe.y = poseResult.y;
position_boxe.width = poseResult.width;
position_boxe.height = poseResult.height;
cv::rectangle(image, position_boxe, cv::Scalar(0, 0, 255), 2, 8);
cv::rectangle(image, cv::Point(position_boxe.x, position_boxe.y - 20), cv::Point(position_boxe.x, position_boxe.y), cv::Scalar(0, 255, 255), -1);
cv::putText(image, poseResult.className, cv::Point(position_boxe.x, position_boxe.y - 10), cv::FONT_HERSHEY_SIMPLEX, .5, cv::Scalar(0, 0, 0));
}
cv::namedWindow("show", cv::WINDOW_NORMAL);
cv::imwrite("result.jpg", image);
cv::imshow("show", image);
cv::waitKey(1);
delete pBuf;
pBuf = nullptr;
}
|
61501775
Hu Chunming
添加showFrame
|
89
|
mutex m_mutex_show;
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
90
91
|
unsigned char *pShowData = nullptr;
|
61501775
Hu Chunming
添加showFrame
|
92
93
94
|
void showFrame(AVFrame * gpuFrame) {
std::lock_guard<std::mutex> l(m_mutex_show);
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
95
96
97
98
99
100
|
cudaError_t cudaStatus = cudaSuccess;
if (pShowData == nullptr)
{
cudaError_t cudaStatus = cudaMalloc((void **)&pShowData, 3 * gpuFrame->width * gpuFrame->height * sizeof(unsigned char));
}
|
61501775
Hu Chunming
添加showFrame
|
101
|
cuda_common::setColorSpace(ITU709, 0);
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
102
|
cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0], (CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], pShowData, gpuFrame->width, gpuFrame->height);
|
61501775
Hu Chunming
添加showFrame
|
103
104
105
106
107
|
if (cudaStatus != cudaSuccess) {
cout << "CUDAToBGR failed !!!" << endl;
return;
}
|
61501775
Hu Chunming
添加showFrame
|
108
109
110
111
|
int channel = 3;
int width = gpuFrame->width;
int height = gpuFrame->height;
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
112
|
if (pShowData != nullptr && channel > 0 && width > 0 && height > 0) {
|
69bdb744
Hu Chunming
改用onnx模型
|
113
114
115
|
std::vector<DogPoseResult> vec_result = poseDetector.detect(pShowData, width, height);
showResult(pShowData, width, height, vec_result);
|
61501775
Hu Chunming
添加showFrame
|
116
|
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
117
118
|
//int nSize = channel * height * width;
//unsigned char* cpu_data = new unsigned char[nSize];
|
61501775
Hu Chunming
添加showFrame
|
119
|
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
120
121
|
//cudaMemcpy(cpu_data, pShowData, nSize * sizeof(unsigned char), cudaMemcpyDeviceToHost);
//cudaDeviceSynchronize();
|
61501775
Hu Chunming
添加showFrame
|
122
|
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
123
124
125
|
//cv::Mat img_(height, width, CV_8UC3, cpu_data);
//imshow("show", img_);
//waitKey(1);
|
61501775
Hu Chunming
添加showFrame
|
126
|
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
127
128
|
//delete[] cpu_data;
//cpu_data = nullptr;
|
61501775
Hu Chunming
添加showFrame
|
129
|
|
69bdb744
Hu Chunming
改用onnx模型
|
130
131
|
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
132
|
}
|
61501775
Hu Chunming
添加showFrame
|
133
134
|
}
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/**
* 注意: gpuFrame 在解码器设置的显卡上,后续操作要十分注意这一点,尤其是多线程情况
* */
void postDecoded(const void * userPtr, AVFrame * gpuFrame){
FFNvDecoder* decoder = (FFNvDecoder*)userPtr;
if (decoder!= nullptr)
{
// cout << "decode name: " << decoder->getName() << endl;
// const char* gpu_pixfmt = av_get_pix_fmt_name((AVPixelFormat)gpuFrame->format);
// cout << "pixfmt: " << gpu_pixfmt << endl;
// cout << "keyframe: " << gpuFrame->key_frame << " width: " << gpuFrame->width << " height: "<< gpuFrame->height << endl;
// cout << "decode successed ✿✿ヽ(°▽°)ノ✿ " << endl;
if (gpuFrame->format == AV_PIX_FMT_CUDA)
{
// cout << "gpuid = " << atoi(decoder->m_cfg.gpuid.c_str()) << endl;
cudaSetDevice(atoi(decoder->m_cfg.gpuid.c_str()));
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
154
|
//saveFrame(gpuFrame, decoder->getName());
|
61501775
Hu Chunming
添加showFrame
|
155
|
showFrame(gpuFrame);
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
}
}
}
long long start_time = 0;
long long end_time = 0;
bool count_flag = false;
int count_num = 0;
int count_std = 100;
static long long get_cur_time(){
// 获取操作系统当前时间点(精确到微秒)
chrono::time_point<chrono::system_clock, chrono::milliseconds> tpMicro
= chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());
// (微秒精度的)时间点 => (微秒精度的)时间戳
return tpMicro.time_since_epoch().count();
}
|
61501775
Hu Chunming
添加showFrame
|
174
|
static int suming = 0;
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
unsigned char *pHwData = nullptr;
void postDecoded0(const void * userPtr, AVFrame * gpuFrame){
// std::this_thread::sleep_for(std::chrono::milliseconds(30000));
FFNvDecoder* decoder = (FFNvDecoder*)userPtr;
if (decoder!= nullptr)
{
// cout << "decode name: " << decoder->getName() << endl;
if (decoder->getName() == "dec")
{
if (! count_flag)
{
count_flag = true;
count_num = 0;
end_time = start_time = get_cur_time();
}
count_num++;
|
61501775
Hu Chunming
添加showFrame
|
193
|
suming ++ ;
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
194
195
196
197
198
199
200
201
202
203
|
if (count_num >= count_std)
{
// end_time = get_cur_time();
// long time_using = end_time - start_time;
// double time_per_frame = double(time_using)/count_std ;
// cout << count_std << "帧用时:" << time_using << "ms 每帧用时:" << time_per_frame << "ms" << endl;
cout << "keyframe: " << gpuFrame->key_frame << " width: " << gpuFrame->width << " height: "<< gpuFrame->height << endl;
count_flag = false;
}
|
61501775
Hu Chunming
添加showFrame
|
204
|
cout << "帧数:" << suming << endl;
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
205
206
207
208
209
210
211
212
213
214
215
216
217
|
}
}
}
void decode_finished_cbk(const void* userPtr){
cout << "decode_finish timestamp: " << get_cur_time() << endl;
}
// string test_uri = "rtmp://192.168.10.56:1935/objecteye/1";
// string test_uri = "/home/cmhu/data/output_800x480.mp4";
// string test_uri = "/home/cmhu/data/output_1920x1080.mp4";
// string test_uri = "rtsp://176.10.0.2:8554/stream";
// string test_uri = "/mnt/f/fiss/test_data/h265.mp4";
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
218
|
//string test_uri = "rtsp://176.10.0.4:8554/stream";
|
69bdb744
Hu Chunming
改用onnx模型
|
219
|
string test_uri = "f://data/5min.mp4";
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
void createDecode(int index){
FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
MgrDecConfig config;
config.name = "dec" + to_string(index);
config.cfg.uri = test_uri;
config.cfg.post_decoded_cbk = postDecoded;
config.cfg.decode_finished_cbk = decode_finished_cbk;
config.cfg.force_tcp = true;
if (index % 2 == 0)
{
config.cfg.gpuid = "0";
}
else
{
config.cfg.gpuid = "0";
}
FFNvDecoder* decoder = pDecManager->createDecoder(config);
if (!decoder)
{
return ;
}
pDecManager->setUserPtr(config.name, decoder);
pDecManager->startDecodeByName(config.name);
}
void logFF(void *, int level, const char *fmt, va_list ap)
{
vfprintf(stdout, fmt, ap);
}
int main(int argc, char* argv[]) {
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
256
|
printf("start \n");
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
257
258
259
260
|
//if (argc != 3) {
// fprintf(stderr, "./xxx uri gpu_id\n");
// return -1;
//}
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
261
|
|
69bdb744
Hu Chunming
改用onnx模型
|
262
|
//char* uri = argv[1];
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
263
|
char* gpuid = "0";//argv[2];
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
264
265
266
|
cout << av_version_info() << endl;
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
267
268
269
270
|
poseDetector.init();
//namedWindow("show", WINDOW_NORMAL);
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
//evalQuality(uri, gpuid);
// av_log_set_callback(&logFF);
FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
// int count = 99;
// for (size_t i = 0; i < count ; i++)
// {
// createDecode(i);
// }
MgrDecConfig config;
config.name = "dec";
|
69bdb744
Hu Chunming
改用onnx模型
|
286
|
config.cfg.uri = test_uri;
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
config.cfg.post_decoded_cbk = postDecoded;
config.cfg.decode_finished_cbk = decode_finished_cbk;
config.cfg.force_tcp = true;
config.cfg.gpuid = "0";
FFNvDecoder* dec2 = pDecManager->createDecoder(config);
if (!dec2)
{
return 1;
}
pDecManager->setUserPtr(config.name, dec2);
// pDecManager->setDecKeyframe(config.name, true);
pDecManager->startDecodeByName(config.name);
int w,h;
pDecManager->getResolution(config.name, w,h);
printf( "%s : %dx%d\n", config.name.c_str() , w,h );
|
07639e75
Hu Chunming
实现狗狗姿态检测
|
304
305
306
307
308
309
310
311
312
313
314
315
316
|
//thread* m_thread = new thread([](void* arg)
// {
// while (true)
// {
// std::this_thread::sleep_for(std::chrono::milliseconds(5000));
// FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
// int count = pDecManager->count();
// cout << "当前运行路数: " << pDecManager->count() << endl;
// }
// return (void*)0;
// }
//, nullptr);
|
a359bb4f
Hu Chunming
调通可正常运行 -- 初版
|
317
318
319
320
321
322
323
324
|
while (getchar() != 'q');
cout << "总共帧数:" << sum << endl;
pDecManager->closeAllDecoder();
}
|