-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrack.cpp
More file actions
214 lines (187 loc) · 6.02 KB
/
track.cpp
File metadata and controls
214 lines (187 loc) · 6.02 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
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <cstdio>
#include "Eserial.h"
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay (Mat frame, int threshold);
vector<Rect> storeLeftEyePos(Mat rightFaceImage);
vector<Rect> storeLeftEyePos_open(Mat rightFaceImage);
/** Global variables */
//-- Note, either copy these two files from opencv/data/haarscascades to your current folder, or change these locations
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
String leftEyeCascade = "haarcascade_lefteye_2splits.xml";
String rightEyeCascade = "haarcascade_righteye_2splits.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
CascadeClassifier leftEyeDetector;
CascadeClassifier rightEyeDetector;
string window_name = "Captcha";
// Serial to Arduino global declarations
int arduino_command;
Eserial * arduino_com;
short MSBLSB = 0;
unsigned char MSB = 0;
unsigned char LSB = 0;
int open_eye = 0, close_eye = 0;
// Serial to Arduino global declarations
int main (int argc, const char **argv)
{
VideoCapture capture(0);
Mat frame;
char c = argv[2][0];
cout << c << endl;
/*
if (argc != 2)
{
cerr << "Usage: techbitarFaceDetection <serial device>" << endl;
exit(1);
}
*/
// serial to Arduino setup
arduino_com = new Eserial ();
if (arduino_com != 0)
{
//Standard Arduino uses /dev/ttyACM0 typically, Shrimping.it
//seems to use /dev/ttyUSB0 or /dev/ttyUSB1.
arduino_com->connect(argv[1], 57600, spNONE);
}
// serial to Arduino setup
//-- 1. Load the cascades
if (!face_cascade.load (face_cascade_name))
{
printf("--(!)Error loading\n");
return -1;
};
if (!eyes_cascade.load (eyes_cascade_name))
{
printf("--(!)Error loading\n");
return -1;
};
if (!leftEyeDetector.load (leftEyeCascade))
{
printf("--(!)Error loading\n");
return -1;
};
if (!rightEyeDetector.load (rightEyeCascade))
{
printf("--(!)Error loading\n");
return -1;
};
//-- 2. Read the video stream
//capture = cvCaptureFromCAM (-1);
if (capture.isOpened())
{
while (true)
{
capture.read(frame);
//-- 3. Apply the classifier to the frame
if(!frame.empty ())
{
detectAndDisplay (frame, c-'0');
}
else
{
printf (" --(!) No captured frame -- Break!");
break;
}
int c = waitKey (10);
if((char) c == 'c')
{
break;
}
}
}
// Serial to Arduino - shutdown
arduino_com->disconnect ();
delete arduino_com;
arduino_com = 0;
// Serial to Arduino - shutdown
return 0;
}
/**
* @function detectAndDisplay
*/
void detectAndDisplay (Mat frame, int threshold)
{
std::vector <Rect> faces;
Mat frame_gray;
cvtColor (frame, frame_gray, CV_BGR2GRAY);
equalizeHist (frame_gray, frame_gray); // To make uniform distributuon of the intensity.
//-- Detect faces and returns a list of rectangles
face_cascade.detectMultiScale (frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size (30, 30));
for (int i = 0; i < faces.size (); i++)
{
Point center (faces[i].x + faces[i].width * 0.5, faces[i].y + faces[i].height * 0.5);
ellipse (frame, center, Size (faces[i].width * 0.5, faces[i].height * 0.5), 0, 0, 360, Scalar (255, 0, 255), 2, 8, 0);
Mat faceROI = frame_gray (faces[i]);
std::vector <Rect> eyes;
std::vector <Rect> eyes1;
//-- In each face, detect eyes
//eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CASCADE_SCALE_IMAGE, Size(30, 30) );
//cout << eyes.size() << endl;
leftEyeDetector.detectMultiScale(faceROI, eyes, 1.1, 2, CASCADE_FIND_BIGGEST_OBJECT, Size(0, 0));
rightEyeDetector.detectMultiScale(faceROI, eyes1, 1.1, 2, CASCADE_FIND_BIGGEST_OBJECT, Size(0, 0));
for ( size_t j = 0; j < eyes.size(); j++ )
{
Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y + eyes[j].y + eyes[j].height/2 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, eye_center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
}
for ( size_t j = 0; j < eyes1.size(); j++ )
{
Point eye_center( faces[i].x + eyes1[j].x + eyes1[j].width/2, faces[i].y + eyes1[j].y + eyes1[j].height/2 );
int radius = cvRound( (eyes1[j].width + eyes1[j].height)*0.25 );
circle( frame, eye_center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
}
if (eyes.size() > 0)
{
// Now look for open eyes only
vector<Rect> eyesRightNew = storeLeftEyePos_open(faceROI);
if(open_eye > threshold && close_eye > threshold)
{
// cout << "X:" << faces[i].x << " y:" << faces[i].y << endl;
// send X,Y of face center to serial com port
// send X axis
// read least significant byte
LSB = faces[i].x & 0xff;
// read next significant byte
MSB = (faces[i].x >> 8) & 0xff;
arduino_com->sendChar (MSB);
arduino_com->sendChar (LSB);
//cout << "X MSB: " << hex << (int) MSB << ", X LSB: " << (int) LSB << endl;
// Send Y axis
LSB = faces[i].y & 0xff;
MSB = (faces[i].y >> 8) & 0xff;
arduino_com->sendChar (MSB);
arduino_com->sendChar (LSB);
//cout << "Y MSB: " << hex << (int) MSB << ", Y LSB: " << (int) LSB << endl;
// serial com port send
}
else
if (eyesRightNew.size() > 0) //Eye is open
{
cout << "OPEN" << endl;
open_eye++;
}
else //Eye is closed
{
cout << "close" << endl;
close_eye++;
}
}
}
//-- Show what you got
imshow (window_name, frame);
}
// Method for detecting open eyes in right half of face
vector<Rect> storeLeftEyePos_open(Mat rightFaceImage)
{
std::vector<Rect> eyes;
eyes_cascade.detectMultiScale(rightFaceImage, eyes, 1.1, 2, CASCADE_FIND_BIGGEST_OBJECT, Size(0, 0));
return eyes;
}