-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
499 lines (338 loc) · 11.2 KB
/
main.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include<highgui.h>
#include<cxcore.h>
#include <opencv2/opencv.hpp>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include<unistd.h>
#include<iostream>
#include<math.h>
#define pi 3.1415
// The key code to be sent.
#define KEYCODE_1 XK_Right
#define KEYCODE_2 XK_Left
#define KEYCODE_3 XK_Up
#define KEYCODE_4 XK_Down
//Max value of HSV trackbar
#define val_max_Hue 179
#define val_max_Saturation 255
#define val_max_Value 255
# define max_size 31 //for median blur
using namespace cv;
using namespace std;
struct position
{
int X;
int Y;
}present,previous,first,last;
class idle
{
bool status;
short int frame_cnt;
const short int max_frame_cnt = 1;
public:
idle()
{
status = 1;//1 means idle
frame_cnt=-1;
}
void change_status()
{
if(status==0)
status = 1;
else
status = 0;
}
bool get_status()
{return status;}
void inc_frame_cnt()
{
++frame_cnt;
}
short int get_frame_cnt()
{
return frame_cnt;
}
short int get_max_frame_cnt()
{
return max_frame_cnt;
}
void reset_frame_cnt()
{
frame_cnt = -1;
}
}idle_ob;
float displacement(position p1,position p2)//to find displacement b/w 2 points
{
return sqrt( pow(p1.X - p2.X,2) + pow(p1.Y - p2.Y,2) );;
}
float mod(float x)
{
if( x>= 0)
return x;
else
return -x;
}
class sweep_gesture
{
//bool status;
float len_displacement;
const float min_horizontal_sweep_len = 100;
const float min_veertical_sweep_len = 100;
const float max_deviation_angle = pi/6;
public:
int detect_sweep(position first_pos,position last_pos)
{
len_displacement = displacement(first_pos,last_pos);
if(len_displacement >= min_horizontal_sweep_len )//sweep detected
{
if( ( mod(first_pos.X - last_pos.X) >= len_displacement*cos(max_deviation_angle) ) && ( mod(first_pos.Y - last_pos.Y) <= len_displacement*sin(max_deviation_angle)) )//horizontal sweep detected
{
if(first_pos.X < last_pos.X) //web camera captures stores the mirror image
{
return 1;//right to leftt
}
else
{
return 2;// left to right
}
}
}
else if(len_displacement >= min_veertical_sweep_len)
{
if( (mod(first_pos.Y - last_pos.Y) >= len_displacement*cos(max_deviation_angle)) && ( mod(first_pos.X - last_pos.X) <= len_displacement*sin(max_deviation_angle) ) ) //vertical sweep detected
{
if(first_pos.Y > last_pos.Y)//its a reverse case here
{
return 3;//bottom to up
}
else
{
return 4;//up to bottom
}
}
}
return -1;//no valid sweep detected
}
}gesture_ob;
XKeyEvent createKeyEvent(Display *display, Window &win,
Window &winRoot, bool press,
int keycode, int modifiers)
{
XKeyEvent event;
event.display = display;
event.window = win;
event.root = winRoot;
event.subwindow = None;
event.time = CurrentTime;
event.x = 1;
event.y = 1;
event.x_root = 1;
event.y_root = 1;
event.same_screen = False;
event.keycode = XKeysymToKeycode(display, keycode);
event.state = modifiers;
if(press)
event.type = KeyPress;
else
event.type = KeyRelease;
return event;
}
int erode_size,dilate_size,erode_struct,dilate_struct,eli1,eli2;
Mat frame,target_frame,hsv_frame,struct_element,t1,t2,modified_frame,shape_frame,temp,face_cover_frame;
int value = 1;
int size_matrix;
int main()
{int x,y,c,delay_value,k,i;
int cnt=1;
// Obtain the X11 display.
Display *display = XOpenDisplay(0);
if(display == NULL)
return -1;
// Get the root window for the current display.
Window winRoot = XDefaultRootWindow(display);
// Find the window which has the current keyboard focus.
Window winFocus;
int revert;
XKeyEvent event;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector<vector<Point> > result;
previous.X = -1;
previous.Y = -1;
VideoCapture cap(0);
if(cap.isOpened()==0)
{cout<<"error!! \ncan't capture video!!!";
return -1;
}
int in_min_Hue = 0;
int in_max_Hue = 179;
int in_min_Saturation = 0;
int in_max_Saturation = 255;
int in_min_Value = 0;
int in_max_Value = 255;
namedWindow("webcamera",0);
namedWindow("target frame",0);
namedWindow("modified frame",0);
namedWindow("shape frame");
//for median blur
createTrackbar("size of matrix for median blur","target frame",&value,max_size);
//for detecting object
createTrackbar("Min Hue","target frame",&in_min_Hue,val_max_Hue);
createTrackbar("Max Hue","target frame",&in_max_Hue,val_max_Hue);
createTrackbar("Min Saturation","target frame",&in_min_Saturation,val_max_Saturation);
createTrackbar("Max Saturation","target frame",&in_max_Saturation,val_max_Saturation);
createTrackbar("Min Value","target frame",&in_min_Value,val_max_Value);
createTrackbar("Max Value","target frame",&in_max_Value,val_max_Value);
//erosion track bar
createTrackbar("erosion: element\n0-Rect\n1-cross\n2-ellipse","target frame",&erode_struct,2);
createTrackbar("erosion: size","target frame",&erode_size,21);
//dilation track bar
createTrackbar("dilation:element\n0-Rect\n1-cross\n2-ellipse","target frame",&dilate_struct,2);
createTrackbar("dilation:size","target frame",&dilate_size,21);
cap.read(temp);//we are reading a temporary frame from the video stream to detect its size and type
shape_frame = Mat::zeros(temp.size(),temp.type());
Mat contour_detect = shape_frame;
while(1)
{
bool frame_success = cap.read(frame);
if(frame_success == 0)
{
cout<<"error!! \n can't read frame from video stream";
}
switch(erode_struct)
{
case 0: eli1=MORPH_RECT;
break;
case 1: eli1=MORPH_CROSS;
break;
case 2: eli1=MORPH_ELLIPSE;
break;
}
switch(dilate_struct)
{
case 0: eli2=MORPH_RECT;
break;
case 1: eli2=MORPH_CROSS;
break;
case 2: eli2=MORPH_ELLIPSE;
break;
}
size_matrix= 2* value + 1;//the size of the matrix should be odd
medianBlur(frame,target_frame,size_matrix);
medianBlur(frame,frame,size_matrix);
cvtColor(frame,hsv_frame,COLOR_BGR2HSV);
inRange(hsv_frame,Scalar(in_min_Hue,in_min_Saturation,in_min_Value),Scalar(in_max_Hue,in_max_Saturation,in_max_Value),target_frame);
t1 = getStructuringElement(eli1,Size(2*erode_size + 1,2*erode_size + 1 ));
t2 = getStructuringElement(eli2,Size(2*dilate_size + 1,2*dilate_size + 1 ));
//morphological closing
dilate(target_frame,target_frame,t2);
//erode(target_frame,target_frame,t1);
//calculating moments
Moments moments_ob = moments(target_frame);
double area = moments_ob.m00;
double m01 = moments_ob.m01;
double m10 = moments_ob.m10;
present.X = (m10/area);
present.Y = (m01/area);
if(present.X > 0 && present.Y > 0)
{
if(displacement(present,previous) > 14 )
{
arrowedLine(shape_frame,Point(previous.X,previous.Y),Point(present.X,present.Y),Scalar(255,255,255),8);
if(idle_ob.get_status()== 1)//if idle
{
first=previous;//storing the initial point of the line
idle_ob.change_status();
idle_ob.inc_frame_cnt();
}
else//not idle
{
idle_ob.reset_frame_cnt();
}
previous.X = present.X;
previous.Y = present.Y;
}
else
{
if(idle_ob.get_status() == 0)//if not idle
{
if(idle_ob.get_frame_cnt() < idle_ob.get_max_frame_cnt())
{
idle_ob.inc_frame_cnt();
}
else//frame_cnt = max_frame
{
last=present;
i=0;
/*detecting sweep gestures*/
//this logic has been designed keeping in mind just the sweep gesture
//requires modifiacation to include other gestures
switch(gesture_ob.detect_sweep(first,last))
{
case 1://right to left sweep
cout<<"rt 2 left: "<<KEYCODE_1<<endl;
XGetInputFocus(display, &winFocus, &revert);
// Send a fake key press event to the window.
event = createKeyEvent(display, winFocus, winRoot, true, KEYCODE_1, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
// Send a fake key release event to the window.
event = createKeyEvent(display, winFocus, winRoot, false, KEYCODE_1, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
XSync(display,true);
break;
case 2://left to right sweep
cout<<"left 2 right: "<<KEYCODE_2<<endl;
XGetInputFocus(display, &winFocus, &revert);
// Send a fake key press event to the window.
event = createKeyEvent(display, winFocus, winRoot, true, KEYCODE_2, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
// Send a fake key release event to the window.
event = createKeyEvent(display, winFocus, winRoot, false, KEYCODE_2, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
XSync(display,true);
break;
case 3://bottom to up sweep
cout<<"bottom 2 up\n";
XGetInputFocus(display, &winFocus, &revert);
XSync(display,true);
// Send a fake key press event to the window.
event = createKeyEvent(display, winFocus, winRoot, true, KEYCODE_3, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
// Send a fake key release event to the window.
event = createKeyEvent(display, winFocus, winRoot, false, KEYCODE_3, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
XSync(display,true);
break;
case 4://up to bottom sweep
cout<<"up 2 bottom\n";
XGetInputFocus(display, &winFocus, &revert);
XSync(display,true);
// Send a fake key press event to the window.
event = createKeyEvent(display, winFocus, winRoot, true, KEYCODE_4, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
// Send a fake key release event to the window.
event = createKeyEvent(display, winFocus, winRoot, false, KEYCODE_4, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
XSync(display,true);
break;
default:cout<<"no gesture\n";
}
idle_ob.change_status();
idle_ob.reset_frame_cnt();
shape_frame=Mat::zeros(temp.size(),temp.type());//clear shape frame
}
}
}
}
imshow("shape frame",shape_frame);
imshow("webcamera",frame);
//imshow("hsv frame",hsv_frame);
imshow("target frame",target_frame);
//imshow("erosion frame",erode_frame);
//imshow("dilation frame",dilate_frame);
imshow("modified frame",target_frame);
// cnt=(cnt+1)%10;
waitKey( 5);
}
return 0;
}