-
Notifications
You must be signed in to change notification settings - Fork 0
/
faceDetection.cpp
113 lines (76 loc) · 2.62 KB
/
faceDetection.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
//after detect face --> detect small
// Aldebaran includes.
#include <alproxies/alvideodeviceproxy.h>
#include <alvision/alimage.h>
#include <alvision/alvisiondefinitions.h>
#include <alerror/alerror.h>
#include <alproxies/alanimatedspeechproxy.h>
// Opencv includes.
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <string>
#include <vector>
using namespace AL;
using namespace cv;
void detectFace(const std::string& robotIp)
{
ALVideoDeviceProxy camProxy("192.168.1.104", 9559);
AL::ALAnimatedSpeechProxy animtts ("192.168.1.104", 9559);
const std::string clientName = camProxy.subscribe("test", kQVGA, kBGRColorSpace, 30);
const std::string fn_haar = "/home/youmna/devTools/opencv-3.0.0-rc1/data/haarcascades/haarcascade_frontalface_alt2.xml";
const std::string sm_haar = "/home/youmna/devTools/opencv-3.0.0-rc1/data/haarcascades/haarcascade_smile.xml";
cv::Mat imgHeader = cv::Mat(cv::Size(320, 240), CV_8UC3);
cv::CascadeClassifier haar_cascade;
haar_cascade.load(fn_haar);
cv::CascadeClassifier haar_cascade2;
haar_cascade2.load(sm_haar);
cv::namedWindow("images");
while ((char) cv::waitKey(30) != 27)
{
ALValue img = camProxy.getImageRemote(clientName);
imgHeader.data = (uchar*) img[6].GetBinary();
Mat gray;
cvtColor(imgHeader, gray, CV_BGR2GRAY);
std::vector< Rect_<int> > faces;
haar_cascade.detectMultiScale(gray, faces);
for(int i = 0; i < faces.size(); i++) {
Rect face_i = faces[i];
Mat face = gray(face_i); // crop the image with smile
std::vector< Rect_<int> > smiles;
haar_cascade2.detectMultiScale(face, smiles);
if(smiles.size() > 0){
// std::string textToSay = "\\rspd=10\\ ^start(animations/Stand/Gestures/Hey_1)Hello Human ^wait(animations/Stand/Gestures/Hey_1)";
// animtts.say(textToSay);
std::cout <<" yes smile yes hello" <<std::endl;
}
else{
std::cout <<" no smile no hello" <<std::endl;
}
rectangle(imgHeader, face_i, CV_RGB(0, 255,0), 1); //draw rectangle around the smile area
}
cv::imshow("images", imgHeader);
camProxy.releaseImage(clientName);
}
camProxy.unsubscribe(clientName);
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cerr << "Usage 'getimages robotIp'" << std::endl;
return 1;
}
const std::string robotIp(argv[1]);
try
{
detectFace(robotIp);
}
catch (const AL::ALError& e)
{
std::cerr << "Caught exception " << e.what() << std::endl;
}
return 0;
}