eval.cpp
10.9 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "retinaface.h"
#include "faceLandmarks.h"
#include "speakcls.h"
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include<algorithm>
#include<opencv2/opencv.hpp>
#include<stdio.h>
using namespace std;
using namespace cv;
float iou_compute(Bbox b1, Bbox b2)
{
float tmp_w=min(b1.xmax,b2.xmax) - max(b1.xmin, b2.xmin);
float tmp_h=min(b1.ymax, b2.ymax) - max(b1.ymin, b2.ymin);
float w = max(tmp_w, float(0));
float h = max(tmp_h, float(0));
return w*h / ((b1.xmax-b1.xmin)*(b1.ymax-b1.ymin) + (b2.xmax-b2.xmin)*(b2.ymax-b2.ymin) - w*h);
}
vector<vector<cv::Mat>> mouth_process(vector<vector<vector<vector<float>>>> batch_landmarks, vector<cv::Mat> batch_images){
int input_size=112;
vector<vector<cv::Mat>> align_mouths;
for(int i=0;i<batch_images.size();++i){
cv::Mat image = batch_images[i];
vector<cv::Mat> tmp_mouths;
for(int j=0;j<batch_landmarks[i].size();++j){
vector<float> mouth_xs;
vector<float> mouth_ys;
for(int k=84;k<int(104);++k){
float x_q = round(batch_landmarks[i][j][k][0]);
float y_q = round(batch_landmarks[i][j][k][1]);
mouth_xs.push_back(x_q);
mouth_ys.push_back(y_q);
}
float mouth_width=*max_element(mouth_xs.begin(),mouth_xs.end())-*min_element(mouth_xs.begin(),mouth_xs.end());
float mouth_height=*max_element(mouth_ys.begin(),mouth_ys.end())-*min_element(mouth_ys.begin(),mouth_ys.end());
int mouth_min_x=ceil(*min_element(mouth_xs.begin(),mouth_xs.end())-mouth_width*0.2);
int mouth_min_y=ceil(*min_element(mouth_ys.begin(),mouth_ys.end())-mouth_height*0.1);
int mouth_max_x=ceil(*max_element(mouth_xs.begin(),mouth_xs.end())+mouth_width*0.2);
int mouth_max_y=ceil(*max_element(mouth_ys.begin(),mouth_ys.end())+mouth_height*0.1);
mouth_min_x=mouth_min_x>0?mouth_min_x:0;
mouth_min_y=mouth_min_y>0?mouth_min_y:0;
cv::Rect mouth_rect = Rect(mouth_min_x,mouth_min_y,mouth_max_x-mouth_min_x,mouth_max_y-mouth_min_y);
cv::Mat mouth_crop = image(mouth_rect);
cv::Mat resize_mouth_crop;
cv::resize(mouth_crop,resize_mouth_crop,Size(input_size,input_size));
Point center=Point(input_size/2,input_size/2);
float dx = batch_landmarks[i][j][90][0]-batch_landmarks[i][j][84][0];
float dy = batch_landmarks[i][j][90][1]-batch_landmarks[i][j][84][1];
double angle = atan2(dy,dx)*180/float(M_PI);
cv::Mat rotate_matrix = cv::getRotationMatrix2D(center,double(angle),1);
cv::Mat rot_img;
cv::warpAffine(resize_mouth_crop,rot_img,rotate_matrix,Size(input_size,input_size));
tmp_mouths.push_back(rot_img);
}
align_mouths.push_back(tmp_mouths);
}
return align_mouths;
}
//视频/图像数据切片
//图像
void image_reader(string file_path,int segment_num,vector<Mat> &bgr_frames,vector<vector<int>> &indices){
int new_length = 1;
vector<String> image_files;
glob(file_path, image_files, false);
int total_frames_num = (int)image_files.size();
float tick = float(total_frames_num - new_length + 1) / float(segment_num);
vector<int> indice;
for(int x=0;x<segment_num;++x){
indice.push_back(int(tick / 2.0 + tick * x));
}
indices.push_back(indice);
for(auto im_file:image_files){
Mat bgr_img=cv::imread(im_file);
bgr_frames.push_back(bgr_img);
}
}
// void imageio_reader(string file_path,int split_num,int segment_num,vector<Mat> &bgr_frames,vector<vector<int>> &total_split_indices){
// int new_length = 1;
// VideoCapture capture(file_path);
// int frameH = (int)capture.get(CAP_PROP_FRAME_HEIGHT);
// int frameW = (int)capture.get(CAP_PROP_FRAME_WIDTH);
// int fps = (int)capture.get(CAP_PROP_FPS);
// int total_frame_num = (int)capture.get(CAP_PROP_FRAME_COUNT);
// int i = 0;
// vector<cv::Mat> rgb_frames;
// while(1)
// {
// cv::Mat img = cvQueryFrame(capture); //获取一帧图片
// rgb_frames.push_back(img);
// ++i;
// if(i == total_frame_num){
// break;
// }
// }
// int reg_times = int(total_frame_num/split_num);
// float tick = (split_num-new_length + 1)/float(segment_num);
// vector<int> indices;
// for(int j=0;j<segment_num;++j){
// indices.push_back(int(tick/2.0+tick*j));
// }
// vector<vector<int>> total_split_indices;
// for(int l=0;l<reg_times;++l){
// vector<int> current_indices;
// int coefficient = l*split_num;
// for(int index:indices){
// current_indices.push_back(index+coefficient);
// }
// total_split_indices.push_back(current_indices);
// }
// if(reg_times==0){
// tick = (int(total_frame_num)-new_length + 1)/float(segment_num);
// includes.clear();
// for(int j=0;j<segment_num;++j){
// indices.push_back(int(tick/2.0+tick*j));
// }
// total_split_indices.push_back(indices);
// }
// }
int recognition(string image_path){
//说话人识别
SpeakCls speak_cls=SpeakCls("/home/situ/qfs/sdk_project/speak_det_c/model/cls_speak_v0.2.2.mnn");
//人脸检测
RetinaFace face_det=RetinaFace("/home/situ/qfs/sdk_project/speak_det_c/model/det_face_retina_torch_1.4_v0.0.2.onnx");
//人脸关键点检测
FaceLandmarks landm_det= FaceLandmarks("/home/situ/qfs/sdk_project/speak_det_c/model/det_landmarks_106_v0.0.1.onnx");
vector<Mat> all_bgr_images;
vector<vector<int>> total_split_indices;
image_reader(image_path,10,all_bgr_images,total_split_indices);
// vector<json> all_results;
bool is_talk=false;
for(int im_i=0;im_i<total_split_indices.size();++im_i){
vector<vector<cv::Mat>> face_list;
vector<vector<Bbox>> bbox_list;
vector<cv::Mat> rgb_frames;
vector<cv::Mat> bgr_frames;
int tmp_rows,tmp_cols;
for(int im_j=0;im_j<total_split_indices[im_i].size();++im_j){
Mat tmp_img=all_bgr_images[total_split_indices[im_i][im_j]];
if(im_j !=0){
if(tmp_img.rows!=tmp_rows&&tmp_img.cols!=tmp_cols){
cv::resize(tmp_img,tmp_img,Size(int(tmp_img.cols),int(tmp_img.rows)));
}
}
tmp_rows=tmp_img.rows;
tmp_cols=tmp_img.cols;
Mat rgb_tmp_img;
cv::cvtColor(tmp_img,rgb_tmp_img,cv::COLOR_BGR2RGB);
bgr_frames.push_back(tmp_img);
rgb_frames.push_back(rgb_tmp_img);
}
for(auto bgr_frame:bgr_frames){
vector<Bbox> boxes=face_det.detect_image(bgr_frame);
vector<cv::Mat> tmp_face_areas;
vector<Bbox> tmp_bbox_list;
for(auto box:boxes){
tmp_bbox_list.push_back(box);
// cout<<box.xmin<<" "<<box.ymin<<" "<<box.xmax-box.xmin<<" "<<box.ymax-box.ymin<<endl;
Rect m_select = Rect(box.xmin,box.ymin,box.xmax-box.xmin,box.ymax-box.ymin);
cv::Mat face_area=bgr_frame(m_select);
tmp_face_areas.push_back(face_area);
// cv::imshow("123",face_area);
// cv::waitKey(0);
}
face_list.push_back(tmp_face_areas);
bbox_list.push_back(tmp_bbox_list);
}
vector<vector<vector<vector<float>>>> landms_list;
for(int i=0;i<face_list.size();++i){
vector<vector<vector<float>>> tmp_landm_list;
for(int j=0;j<face_list[i].size();++j){
vector<vector<float>> tmp_landms=landm_det.detect_image_landmarks(face_list[i][j]);
for(int k=0;k<tmp_landms.size();++k){
tmp_landms[k][0]=tmp_landms[k][0]+bbox_list[i][j].xmin;
tmp_landms[k][1]=tmp_landms[k][1]+bbox_list[i][j].ymin;
}
tmp_landm_list.push_back(tmp_landms);
}
landms_list.push_back(tmp_landm_list);
}
vector<vector<cv::Mat>> mouth_list=mouth_process(landms_list,rgb_frames);
vector<vector<Bbox>> last_bboxes=bbox_list;
vector<Bbox> first_bboxes = bbox_list[0];
vector<vector<Bbox>>::iterator k = last_bboxes.begin();
last_bboxes.erase(k);
vector<vector<Bbox>> all_track_bbox_list;
vector<vector<cv::Mat>> all_face_list,all_mouth_list;
for(int i=0;i<first_bboxes.size();++i){
Bbox first_bbox=first_bboxes[i];
vector<Bbox> track_bbox_list;
vector<cv::Mat> trace_face_list,trace_mouth_list;
track_bbox_list.push_back(first_bbox);
trace_face_list.push_back(face_list[0][i]);
trace_mouth_list.push_back(mouth_list[0][i]);
for(int j=0;j<last_bboxes.size();++j){
vector<Bbox> next_bboxes=last_bboxes[j];
for(int k=0;k<next_bboxes.size();++k){
Bbox next_bbox = next_bboxes[k];
float iou=iou_compute(first_bbox,next_bbox);
if(iou>=0.4){
track_bbox_list.push_back(next_bbox);
trace_face_list.push_back(face_list[j+1][k]);
trace_mouth_list.push_back(mouth_list[j+1][k]);
break;
}
}
}
all_track_bbox_list.push_back(track_bbox_list);
all_face_list.push_back(trace_face_list);
all_mouth_list.push_back(trace_mouth_list);
}
for(int j=0;j<all_mouth_list.size();j++){
vector<cv::Mat> select_mouth_list=all_mouth_list[j];
/**
* @brief 模型推理部分代码,返回result 0/1 ,其中1为说话,0为未说话
*
*/
bool result=speak_cls.detect_speak(select_mouth_list);
// bool result=true;
if(result){
is_talk=true;
// speak_duration = (split_indices[0], split_indices[-1])
// Mat speaker = all_face_list[j][0];
// speaker_str = cv::imencode('.jpg', speaker)[1].tostring()
// speaker_str = base64.b64encode(speaker_str).decode()
// int position = j
// json cur_output={
// "is_talk":true,
// "speak_duration":[str(speak_duration[0]), str(speak_duration[1])],
// "speaker":speaker_str,
// "position":position
// }
// all_results.push_back(cur_output);
cout<<is_talk<<endl;
}else{
cout<<is_talk<<endl;
}
}
return 0;
}
}
int main(){
string test_data_path="/data/speak/bank_test/speak/2327QUESTION_ANSWER";
recognition(test_data_path);
return 0;
}