-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgesture.cpp
377 lines (325 loc) · 10.2 KB
/
gesture.cpp
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "gesture.hpp"
double gesture::get_avg_fps()
{
return avg_fps;
}
cv::Rect gesture::get_fist()
{
return fist[0];
}
cv::Rect gesture::get_palm()
{
return palm[0];
}
bool gesture::parse_config(char * path, sys_config & config)
{
std::ifstream ifs;
Json::Reader reader;
Json::Value root;
Json::Value libApis;
ifs.open(path, std::ios::in | std::ios::binary);
if (ifs.is_open() == false) {
log_error("Open file failed!\n");
return false;
}
if (!reader.parse(ifs, root, false)) {
log_error("Read file failed!\n");
ifs.close();
return false;
}
libApis = root["APIs"];
//memset(&config, 0, sizeof(sys_config));
config.palm_path = root["palm_classifier_path"].asString();
config.fist_path = root["fist_classifier_path"].asString();
config.palm_detect_neighbor_num = libApis[0]["palm detect neighbor number"].asInt();
config.palm_detect_size = libApis[0]["palm detect rectangle size"].asInt();
config.fist_detect_neighbor_num = libApis[0]["fist detect neighbor number"].asInt();
config.fist_detect_size = libApis[0]["fist detect rectangle size"].asInt();
config.iter_time = libApis[0]["test iteration time"].asInt();
config.click_judge_time = libApis[0]["click judge time"].asDouble();
config.confirm_time = libApis[0]["draw rectangle delay"].asDouble();
config.anti_shake_cnt = libApis[0]["anti-shake frame count"].asInt();
log_printf("---------------Config Json file loaded---------------\n");
log_printf("Palm classifier path: %s\n",config.palm_path.data());
log_printf("Fist classifier path: %s\n",config.fist_path.data());
log_printf("Palm detect neighbor number: %d\n",config.palm_detect_neighbor_num);
log_printf("Palm detect rectangle size: %d\n",config.palm_detect_size);
log_printf("Fist detect neighbor number: %d\n",config.fist_detect_neighbor_num);
log_printf("Fist detect rectangle size: %d\n",config.fist_detect_size);
log_printf("Test iteration time: %d\n",config.iter_time);
log_printf("-----------------------------------------------------\n");
ifs.close();
return true;
}
void gesture::read_config()
{
if(parse_config((char *)CONFIG_FILENAME, config))
{
pdetect_num = config.palm_detect_neighbor_num;
pdetect_rec = config.palm_detect_size;
fdetect_num = config.fist_detect_neighbor_num;
fdetect_rec = config.fist_detect_size;
T = config.iter_time;
m_CLICK_JUDGE = config.click_judge_time;
m_TIME_CONFIRMING_JUDGE = config.confirm_time;
m_ANTI_SHAKING_CNT = config.anti_shake_cnt;
fist_path = config.fist_path;
palm_path = config.palm_path;
Fist.load(fist_path);
Palm.load(palm_path);
}
else
{
log_error("config read error! All parameters are set to default value.\n");
pdetect_num = 7;
pdetect_rec = 90;
fdetect_num = 7;
fdetect_rec = 80;
T = 5;
m_CLICK_JUDGE = 0.5;
m_TIME_CONFIRMING_JUDGE = 0.2;
m_ANTI_SHAKING_CNT = 4;
fist_path = "palm_v4.xml";
palm_path = "fist_v3.xml";
Fist.load(fist_path);
Palm.load(palm_path);
}
}
void gesture::detect(cv::Mat& img)
{
img.copyTo(cam_img);
split(cam_img, imageRGB);
for (int i = 0; i < 3; i++)
{
equalizeHist(imageRGB[i], imageRGB[i]);
}
merge(imageRGB, 3, cam_img);
//int64 start = cv::getTickCount();
//s = cv::getTickCount();
t.start();
s = t.gettimegap();
Palm.detectMultiScale(cam_img,palm,1.1,pdetect_num,0|CV_HAAR_SCALE_IMAGE,cv::Size(pdetect_rec,pdetect_rec));
Fist.detectMultiScale(cam_img,fist,1.1,fdetect_num,0|CV_HAAR_SCALE_IMAGE,cv::Size(fdetect_rec,fdetect_rec));
//time_detect += (cv::getTickCount() - s) / cv::getTickFrequency();
t.pause();
time_detect += (t.gettimegap() - s)/1000;
t.recovery();
for (unsigned int i = 0; i < palm.size();i++){
x1i = palm[i].x;
y1i = palm[i].y;
x2i = x1i + palm[i].width;
y2i = y1i + palm[i].height;
for (unsigned int j = 0; j < fist.size();j++){
iou = 0.0;
x1j = fist[j].x;
y1j = fist[j].y;
x2j = x1j + fist[j].width;
y2j = y1j + fist[j].height;
areai = (x2i-x1i+1)*(y2i-y1i+1);
areaj = (x2j-x1j+1)*(y2j-y1j+1);
xx1 = max(x1i, x1j);
yy1 = max(y1i, y1j);
xx2 = min(x2i, x2j);
yy2 = min(y2i, y2j);
h = max(0.0, yy2-yy1+1);
w = max(0.0, xx2-xx1+1);
intersection = w * h;
iou = max(iou,(intersection / (areai + areaj - intersection)));
//printf("iou is: %f\n",iou);
if (iou>=0.5)
{
std::vector<cv::Rect>::iterator pnlist = fist.begin();
advance(pnlist,j);
if(pnlist!=fist.end())
{
fist.erase(pnlist);
j-=1;
}
}
}
}
for (unsigned int i = 0; i < palm.size();i++){
cv::rectangle(img, palm[i],cv::Scalar(0,0,255),4);
cv::putText(img,"Palm",cv::Point(palm[i].x,palm[i].y-10),cv::FONT_HERSHEY_SIMPLEX,0.8,cv::Scalar(0,0,255),2,1);
}
//palm_tlist.push_back((cv::getTickCount() - s) / cv::getTickFrequency());
for (unsigned int i = 0; i < fist.size();i++){
cv::rectangle(img, fist[i],cv::Scalar(0,255,0),4);
cv::putText(img,"Fist",cv::Point(fist[i].x,fist[i].y-10),cv::FONT_HERSHEY_SIMPLEX,0.8,cv::Scalar(0,255,0),2,1);
}
//fist_tlist.push_back((cv::getTickCount() - s) / cv::getTickFrequency());
/*
if(is_palm())
add(G_PALM, &(palm[0]));
else if(is_fist())
add(G_FIST, &(fist[0]));
frame_cnt++;
time_past += (cv::getTickCount() - start) / cv::getTickFrequency();
if(palm_tlist.size()>=100){
time_palm = 0;
list<double>::iterator pi;
for (pi = palm_tlist.begin(); pi != palm_tlist.end(); ++pi)
time_palm+=*pi;
printf("average time_palm per 100 frames:%f ms\n", time_palm/100*1000);
palm_tlist.clear();
}
if(fist_tlist.size()>=100){
time_fist = 0;
list<double>::iterator fi;
for (fi = fist_tlist.begin(); fi != fist_tlist.end(); ++fi)
time_fist+=*fi;
printf("average time_fist per 100 frames:%f ms\n", time_fist/100*1000);
fist_tlist.clear();
}
*/
t.pause();
time_past+=(t.gettimegap()-s)/1000;
frame_cnt++;
if(time_past/1000 >= T)
{
avg_fps = (double)frame_cnt / time_past * 1000;
log_printf("average fps:%3.2f, detection time:%f ms in %d second\n", avg_fps, time_detect/frame_cnt, T);
frame_cnt = 0;
time_past = 0;
time_detect = 0;
}
t.recovery();
}
bool gesture::is_fist()
{
return fist.size() >= 1;
}
bool gesture::is_palm()
{
return palm.size() >= 1;
}
/*
bool gesture::is_select_start()
{
return G_FIRST_CORD == m_state_cord;
}
cv::Point* gesture::get_select_lt()
{
return &(e_gesture.point_lt);
}
cv::Point* gesture::get_select_rb()
{
return &(e_gesture.point_rb);
}
cv::Point gesture::find_gesture(G_TYPE dst_type)
{
list<gesture_rec>::reverse_iterator it;
for(it=l_gesture.rbegin();it!=l_gesture.rend();it++)
{
if(it->type == dst_type)
return it->center;
}
return {0,0};
}
bool gesture::anti_shaking_state(G_TYPE s)
{
if(l_gesture.size()<m_ANTI_SHAKING_CNT)
return false;
list<gesture_rec>::reverse_iterator it;
for(unsigned int i=0;i<m_ANTI_SHAKING_CNT;i++)
{
it = l_gesture.rbegin();
if(it->type != s)
return false;
it++;
}
return true;
}
bool gesture::is_select_confirmed()
{
double cur_time = get_cur_time();
if(G_SELECT_CONFIRMING == m_state_cord &&
cur_time - m_time_confirming > m_TIME_CONFIRMING_JUDGE)
return 1;
return 0;
}
void gesture::clear_select()
{
m_state_cord = G_NONE;
}
cv::Point* gesture::get_palm_center()
{
list<gesture_rec>::reverse_iterator it = l_gesture.rbegin();
if(G_PALM == it->type)
return &(it->center);
else
return NULL;
}
double gesture::get_cur_time()
{
return (cv::getTickCount() - m_tick_s) / cv::getTickFrequency();
}
void gesture::add(G_TYPE type, cv::Rect* rect)
{
gesture_rec rec;
rec.type = type;
rec.time = get_cur_time();
rec.rect = *rect;
rec.center.x = rect->x + rect->width/2;
rec.center.y = rect->y + rect->height/2;
l_gesture.push_back(rec);
if (l_gesture.size() > G_Q_SIZE)
{
l_gesture.pop_front();
}
update_state_g();
}
STATE_G gesture::update_state_g()
{
G_TYPE type;
double time=1000;
list<gesture_rec>::reverse_iterator it = l_gesture.rbegin();
type = it->type;
if(l_gesture.size()>=2)
{
time = it->time;
it++;
time -= it->time;
}
if(S_NONE == m_state_g && G_PALM == type && anti_shaking_state(G_PALM))
{
m_state_g = S_PALM;
}
else if(S_PALM == m_state_g && G_FIST == type && anti_shaking_state(G_FIST))
{
if(time < m_CLICK_JUDGE)
m_state_g = S_CLICK;
else
m_state_g = S_NONE;
}
else if(S_CLICK == m_state_g)
m_state_g = S_NONE;
if(S_CLICK == m_state_g)
{
l_gesture.rbegin()->is_click = (m_state_g == S_CLICK);
}
update_state_cord();
return m_state_g;
}
void gesture::update_state_cord()
{
if(G_NONE == m_state_cord && S_CLICK == m_state_g)
{
e_gesture.point_lt = l_gesture.rbegin()->center;
m_state_cord = G_FIRST_CORD;
}
else if(G_FIRST_CORD == m_state_cord && S_CLICK == m_state_g)
{
e_gesture.point_rb = l_gesture.rbegin()->center;
e_gesture.rect_select.x = e_gesture.point_lt.x;
e_gesture.rect_select.y = e_gesture.point_lt.y;
e_gesture.rect_select.width = e_gesture.point_rb.x - e_gesture.point_lt.x;
e_gesture.rect_select.height = e_gesture.point_rb.y - e_gesture.point_lt.y;
m_state_cord = G_SELECT_CONFIRMING;
m_time_confirming = l_gesture.rbegin()->time;
}
else if(G_SELECT_CONFIRMING == m_state_cord)
{
}
}
*/