FaceDetModule.cpp
3.92 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
134
135
136
137
138
139
140
141
142
143
144
145
#include "FaceDetModule.h"
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/imgproc.hpp>
#include<opencv2/core/core.hpp>
#include <curand.h>
#include "CropImg.h"
auto sy_process_param_check = [](void *handle, sy_img* img, int batchsize)->bool {
if (handle == nullptr)
{
printf("error: face det handle nullptr!");
return FAILED;
}
if (batchsize == 0)
{
printf("error: face det batchsize zore!");
return FAILED;
}
for (int i = 0; i < batchsize; i++)
{
if (img[i].data_ == nullptr || img[i].h_ == 0 || img[i].w_ == 0 || img[i].c_ == 0)
{
printf("error: face det image nullptr!");
return FAILED;
}
}
};
int face_det_module::face_det_module_init(int gpuid, char* auth_license)
{
m_face_det_handle = nullptr;
fd_param param;
param.thresld = 0.6;
param.gpuid = 0;
param.mode = DEVICE_GPU;
param.log = SY_CONFIG_OPEN;
param.facial_fea_point_config = SY_CONFIG_OPEN; //�Ƿ������ؼ�����
param.pose_config = SY_CONFIG_OPEN; //�Ƿ�������̬��
param.quality_config = SY_CONFIG_OPEN; //�Ƿ������������
param.score_config = SY_CONFIG_OPEN; //�Ƿ������������Ŷ� //SY_CONFIG_OPEN SY_CONFIG_CLOSE
param.max_result_count = 100;
param.auth_license = "sy_va_sub_sdk_2023";
// param.max_batch_size_detect = 10;
// param.max_batch_size_ldmk = 10;
// param.max_batch_size_pose = 10;
// param.max_batch_size_score = 10;
// param.max_batch_size_blurglass = 10;
// //param.max_batch_size_hat = 4;
// param.max_batch_size_occlusion = 10;
param.serialize_file = "./serialize_file/FD";
int flagFD = fd_init(&m_face_det_handle, param);
if(flagFD != 0)
{
printf("fd_init failed\n");
return FAILED;
}
//return SUCCESS;
const int batchsize = 20;
fd_result result[batchsize];
for (int fd_i = 0; fd_i < batchsize; fd_i++)
{
result[fd_i].info = new fd_info[10]; //�ڴ����ⲿ����
}
cv::Mat img[batchsize];
sy_img syimg[batchsize];
for (int i = 0; i < batchsize; i++)
{
cv::Mat tmp_img(224, 112, CV_8UC3);
img[i] = tmp_img.clone();
syimg[i].set_data(img[i].cols, img[i].rows, img[i].channels(), img[i].data);
}
int ret = 0;
ret = fd_detect_batch(m_face_det_handle, syimg, SY_FORMAT_BGR888, batchsize, result);
printf("end fd init\n");
return SUCCESS;
}
int face_det_module::face_det_module_process(sy_img* img, int batchsize, fd_result *face_det_result, sy_point* original_size)
{
if (sy_process_param_check(m_face_det_handle, img, batchsize) == FAILED)
return FAILED;
sy_img *cpu_img = new sy_img[batchsize];
for (int b = 0; b < batchsize; b++) {
cpu_img[b].c_ = 3;
cpu_img[b].w_ = original_size[b].x_;
cpu_img[b].h_ = original_size[b].y_;
cudaMalloc(&cpu_img[b].data_, sizeof(uchar)*cpu_img[b].c_*cpu_img[b].w_*cpu_img[b].h_);
cudacommon::ResizeImgGpu_int8(img[b].data_, img[b].w_ , img[b].h_, cpu_img[b].data_, cpu_img[b].w_, cpu_img[b].h_);
}
for (int ii = 0; ii < batchsize; ii++)
{
face_det_result[ii].count = 0;
for (int c = 0; c < 10; c++)
{
memset((void*)&face_det_result[ii].info[c], 0, sizeof(fd_info));
}
}
int ret = fd_detect_batch(m_face_det_handle, cpu_img, SY_FORMAT_BGR888, batchsize, face_det_result);
for (int b = 0; b < batchsize; b++)
{
int real_count = 0;
for (int c = 0; c < face_det_result[b].count; c++)
{
if (face_det_result[b].info[c].score >= 0.5)
{
memcpy((void*)&face_det_result[b].info[real_count], (void*)&face_det_result[b].info[c], sizeof(fd_info));
real_count++;
}
}
face_det_result[b].count = real_count;
}
for (int b = 0; b < batchsize; b++) {
if(cpu_img[b].data_) {
cudaFree(cpu_img[b].data_);
}
}
if (cpu_img) delete[] cpu_img;
return ret;
}
void face_det_module::face_det_module_release()
{
fd_release(&m_face_det_handle);
}