/
main.cpp
386 lines (353 loc) · 10.3 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
//openCV include directories
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
//Requied t run on raspberryPi
#include "facerec.hpp"
//rapberrypicam_cv include directories
#include "RaspiCamCV.h"
//regular includes needed to compile
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
//additional includes so we can send commands to serial
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
//includes to allow for threading
#include <thread>
using namespace std;
using namespace cv;
double happiness;
bool keepConnected;
void sendAction()
{
int fd = open("/dev/ttyACM0", O_RDWR); //needed for the serial write
if (fd == -1)
{
perror("/dev/ttyACM0");
cout<<"Failed to connect to Arduino. Please connect Arduino.";
// return 1;
}
//Set up the serial port to allow for writing to it.
struct termios tios;
tcgetattr(fd, &tios);
// disable flow control and all that, and ignore break and parity errors
tios.c_iflag = IGNBRK | IGNPAR;
tios.c_oflag = 0;
tios.c_lflag = 0;
cfsetspeed(&tios, B115200);
tcsetattr(fd, TCSAFLUSH, &tios);
// the serial port has a brief glitch once we turn it on which generates a
// start bit; sleep for 1ms to let it settle
usleep(1000);
while (!keepConnected)
{
//Need to tell user to connect keepon up and check we can connect
cout<<"Is Keepon Connected(Y/N)?: ";
string input;
getline(cin, input);
if (input.find("Y") != std::string::npos)
{
cout<<"Perfoming test."<<endl;
char msg[] = "MOVE PON UP;";
write(fd, msg, strlen(msg));
strcpy(msg, "MOVE PON DOWN;");
write(fd, msg, strlen(msg));
cout<<"Was the test a success(Y/N)?:"<<endl;
getline(cin, input);
if (input.find("Y") != std::string::npos)
keepConnected = true;
}
cout<<"Keepon needs to be connected please connect.";
}
while (keepConnected == true)
{
//Sleep for 20seconds just to prevent loads of reapted emotions
//sleep(20);
if ((happiness > -10) && (happiness < 11))
{
char mst[] = ("MOVE TILT 0;");
write(fd, mst, strlen(mst));
strcpy(mst, "MOVE PAN 0;");
write(fd, mst, strlen(mst));
}
if ((happiness > 10) && (happiness < 21))
{
char mst[] = ("MOVE SIDE LEFT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE RIGHT;");
write(fd, mst, strlen(mst));
strcpy(mst,"SOUND PLAY 40;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE LEFT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE RIGHT;");
write(fd, mst, strlen(mst));
strcpy(mst,"SOUND PLAY 40;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE LEFT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE RIGHT;");
write(fd, mst, strlen(mst));
strcpy(mst,"SOUND PLAY 40;");
write(fd, mst, strlen(mst));
}
if ((happiness > 20) && (happiness < 31))
{
char mst[] = ("MOVE SIDE LEFT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE RIGHT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON UP;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON DOWN;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE LEFT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE RIGHT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON UP;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON DOWN;");
write(fd, mst, strlen(mst));
}
if ((happiness > 30) && (happiness < 41))
{
char mst[] = ("MOVE SIDE LEFT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE RIGHT;");
write(fd, mst, strlen(mst));
strcpy(mst,"SOUND PLAY 58;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE LEFT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE RIGHT;");
write(fd, mst, strlen(mst));
strcpy(mst,"SOUND PLAY 58;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON UP;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON DOWN;");
write(fd, mst, strlen(mst));
strcpy(mst,"SOUND PLAY 58;");
write(fd, mst, strlen(mst));
}
if ((happiness > 40) && (happiness < 51))
{
char mst[] = ("MOVE LEFT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE RIGHT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON UP;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON DOWN;");
write(fd, mst, strlen(mst));
strcpy(mst,"SOUND PLAY 48;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PAN 50;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE LEFT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE RIGHT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON UP;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON DOWN;");
write(fd, mst, strlen(mst));
strcpy(mst,"SOUND PLAY 48;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PAN -50;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE LEFT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE RIGHT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON UP;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON DOWN;");
write(fd, mst, strlen(mst));
strcpy(mst,"SOUND PLAY 48;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PAN 0;");
write(fd, mst, strlen(mst));
}
if ((happiness > -20) && (happiness < -11))
{
char mst[] = ("MOVE TILT -100;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PAN -25;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PAN 25;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PAN 0;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON UP;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PON DOWN;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE TILT 100;");
write(fd, mst, strlen(mst));
strcpy(mst,"SOUND PLAY 23;");
write(fd, mst, strlen(mst));
}
if ((happiness > -30) && (happiness < -21))
{
char mst[] = ("SOUND PLAY 23;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE TILT 100;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PAN 25;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE TILT -100;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE TILT 0;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PAN -25;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE TILT -100;");
write(fd, mst, strlen(mst));
strcpy(mst,"SOUND PLAY 23;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE TILT 0;");
write(fd, mst, strlen(mst));
}
if ((happiness > -40) && (happiness < -31))
{
char mst[] = ("MOVE SIDE LEFT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE RIGHT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE LEFT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE SIDE RIGHT;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE TILT -100;");
write(fd, mst, strlen(mst));
strcpy(mst,"SOUND PLAY 23;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE TILT 0;");
write(fd, mst, strlen(mst));
}
if ((happiness > -50) && (happiness < -41))
{
char mst[] = ("SOUND PLAY 23;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE TILT 100;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PAN 25;");
write(fd, mst, strlen(mst));
strcpy(mst,"MOVE PAN -25;");
write(fd, mst, strlen(mst));
}
}
//return 1;
}
void faceDetect()
{
//create classifier and load a cascade file into it, to allow for face detection.
CascadeClassifier face_cascade;
face_cascade.load("haarcascade_frontalface_alt.xml");
if (face_cascade.empty() == true)
{cout<<"empty";}
//set VideoCapture variable and link it to camera.
//CvCapture *captureDevice = cvCreateCameraCapture(0);
RaspiCamCvCapture *captureDevice = raspiCamCvCreateCameraCapture(0);
//check to see if capture device is "real" and alert user if it isn't.
if (!captureDevice)
{
cout<<"Capture device not found please fix"<<endl;
// return -1;
}
//create some matricies to be used in the program.
Mat captureFrame;
Mat greyscaleFrame;
//need a temporary varibale to ensure we dont keep detecting faces and spike values wayyy to high.
//boolean to check if someone was in the last frame
int counter = 0;
bool lastScene = false;
//Make window for the output to be displayed in.
cvNamedWindow("outputCapture",1);
while (!keepConnected)
{
//cout<<"Waiting..."<<endl;
}
//loop to continuously find a face
while (keepConnected)
{
//Check if counter exceeds 100, if it does set it to 0
if (counter > 100)
counter = 0;
//get a new frame
//captureFrame = cvQueryFrame(captureDevice);
captureFrame = raspiCamCvQueryFrame(captureDevice);
//convert frame to greyscale and equalize
cvtColor(captureFrame, greyscaleFrame, CV_BGR2GRAY);
equalizeHist(greyscaleFrame, greyscaleFrame);
//Hold faces
vector<Rect> faces;
face_cascade.detectMultiScale(greyscaleFrame, faces, 1.1, 3, CV_HAAR_SCALE_IMAGE, Size(30,30));
//Here we output what the camera sees
imshow("outputCapture", captureFrame);
//Drawing rectangles around the faces
for(int i = 0; i < faces.size(); i++)
{
Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
Point pt2(faces[i].x, faces[i].y);
rectangle(captureFrame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
}
//check for faces and change value of happiness dependent of if there is a face.
if (faces.size() > 0)
{
if (lastScene == false && counter == 0)
{
happiness += 10.0;
counter++;
}
else
{
happiness += 3.0;
counter++;
}
cout<<"Faces: "<<faces.size()<<endl;
if (happiness > 50.0)
happiness = 50.0;
lastScene = true;
}
else
{
lastScene = false;
happiness -= 0.5;
cout<<"Faces: "<<faces.size()<<endl;
if (happiness < -50.0)
happiness = -50.0;
}
if (waitKey(5) == 27)
{
cout<<"Process ended"<<endl;
break;
}
}
//return 1;
}
//Main of program argc is the number of arguments while argv is the arguments
//said argument are normally locations of files
int main(int argc, char* argv[])
{
//initalize variables
happiness = 0; //Keepon's happiness rating
keepConnected = false;
thread camera(faceDetect); //Start the faceDetection thread
thread action(sendAction); //start the Keepon Control thread
//wait till threads have completed.
camera.join();
action.join();
return 0;
}