FaceDetModule.cpp
5.59 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#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 fd_param_;
fd_param_.thresld = 0.7;
fd_param_.gpuid = gpuid;
fd_param_.mode = DEVICE_GPU;
fd_param_.log = SY_CONFIG_OPEN;
fd_param_.facial_fea_point_config = SY_CONFIG_OPEN;
fd_param_.pose_config = SY_CONFIG_OPEN;
fd_param_.quality_config = SY_CONFIG_OPEN;
fd_param_.score_config = SY_CONFIG_OPEN;
fd_param_.max_result_count = 100;
fd_param_.max_batch_size_detect = 10;
fd_param_.max_batch_size_ldmk = 10;
fd_param_.max_batch_size_pose = 10;
fd_param_.max_batch_size_score = 10;
fd_param_.max_batch_size_blurglass = 10;
fd_param_.max_batch_size_occlusion = 10;
fd_param_.serialize_file = "./serialize_file/FD";
fd_param_.auth_license = "sy_tsl_aiplatform_sdk_2021";
// fd_param param;
// param.thresld = 0.6;
// param.gpuid = gpuid;
// 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_tsl_aiplatform_sdk_2021";
// if (SUCCESS != fd_init(&m_face_det_handle, fd_param_, ""))
if (SUCCESS != fd_init(&m_face_det_handle, fd_param_)) // 220512
{
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)
{
//static int framcount = 0;
//printf("framcount: %d\n", framcount++);
if (sy_process_param_check(m_face_det_handle, img, batchsize) == FAILED)
return FAILED;
sy_img *cpu_img = new sy_img[batchsize];
//unsigned char** data = new unsigned char*[batchsize];
cv::Mat *human_image = new cv::Mat[batchsize];
cv::Mat *human_image_ori = new cv::Mat[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;
// cv::Mat tmp_img(img[b].h_, img[b].w_, CV_8UC3, cpu_img[b].data_);
for (int c = 0; c < face_det_result[b].count; c++)
{
if (face_det_result[b].info[c].score >= 0.5)
{
/*printf("score: %f\n", face_det_result[b].info[c].score);
cv::rectangle(tmp_img, cv::Rect(face_det_result[b].info[0].face_position.left_,
face_det_result[b].info[0].face_position.top_,
face_det_result[b].info[0].face_position.width_,
face_det_result[b].info[0].face_position.height_), cv::Scalar(25, 250, 25), 2, 8, 0);
cv::imshow("face", tmp_img);
cv::waitKey(0);*/
/*printf("face det: %d %d %d %d %d %d\n", face_det_result[b].info[0].face_position.left_,
face_det_result[b].info[0].face_position.top_,
face_det_result[b].info[0].face_position.width_,
face_det_result[b].info[0].face_position.height_,
face_det_result[b].info[0].facial_fea_point[0].x_,
face_det_result[b].info[0].facial_fea_point[0].y_);*/
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;
if (human_image) delete[] human_image;
if (human_image_ori) delete[] human_image_ori;
return ret;
}
void face_det_module::face_det_module_release()
{
fd_release(&m_face_det_handle);
}