-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
97 lines (79 loc) · 2.41 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
#include "opencv2/opencv.hpp"
#include "handTracker.h"
#include<windows.h>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
Mat frame;
Rect trackBox;
Point prePoint, curPoint;
VideoCapture capture;
bool gotTrackBox = false;
int interCount = 0;
int continue_fist = 0;
capture.open(0);
if (!capture.isOpened())
{
cout << "No camera!\n" << endl;
return -1;
}
HandTracker hand_tracker;
while (1)
{
/************ Get hand which need to be tracked ************/
while (!gotTrackBox)
{
capture >> frame;
if (frame.empty())
return -1;
if (hand_tracker.init(frame, trackBox))
{
gotTrackBox = true;
prePoint = Point(trackBox.x + 0.5 * trackBox.width, trackBox.y + 0.5 * trackBox.height);
}
imshow("handTracker", frame);
if (waitKey(2) == 27)
return -1;
}
capture >> frame;
double t = (double)cvGetTickCount();
/****************** Tracking hand **************************/
if (!hand_tracker.processFrame(frame, trackBox))
gotTrackBox = false;
/****************** Control the mouse *********************/
curPoint = Point(trackBox.x + 0.5 * trackBox.width, trackBox.y + 0.5 * trackBox.height);
int dx = curPoint.x - prePoint.x;
int dy = curPoint.y - prePoint.y;
float k = 1.5;
mouse_event(MOUSEEVENTF_MOVE, -k * dx, k * dy, 0, 0);
prePoint = curPoint;
// When you made a fist, means you click left button of mouse
// To avoid successive active within short time, when you had actived a single
// we will ingnore the next 10 frames
interCount++;
if (interCount > 10 && hand_tracker.detectFist(frame, trackBox))
{
// To avoid detecting error, need to successive detect three times successfully
continue_fist++;
if (continue_fist > 3)
{
interCount = 0;
cout << "YOU active a single click command!" << endl;
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
}
else
continue_fist = 0;
/******************* Show image ****************************/
rectangle(frame, trackBox, Scalar(0, 0, 255), 3);
imshow("handTracker", frame);
/******************* Show cost time ************************/
t = (double)cvGetTickCount() - t;
cout << "cost time: " << t / ((double)cvGetTickFrequency()*1000.) << endl;
if (cvWaitKey(3) == 27)
break;
}
return 0;
}