-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
162 lines (137 loc) · 5.58 KB
/
main.cpp
File metadata and controls
162 lines (137 loc) · 5.58 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
// Author: Dong Nguyen, Parth Gupta, Ka Hin Choi
// Purpose: use opencv to detect human face, eyes, smile on video stream
// and get human emotion happy or sad base on collected faces
// finally, add filter base on human emotion
// Prefix: local computer camera is not available or no face detected
// Postfix: add filter tear below human eye when detect emotion sad
// add rainbow on mouth when detect happy
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main(int argc, const char *argv[])
{
Mat img;
Mat imgGray;
Mat teardrop = imread("teardrop.png", IMREAD_UNCHANGED);
Mat rainbowsmile = imread("rainbowsmile.png", IMREAD_UNCHANGED);
string haarCascadePath = "haarcascade_frontalface_default.xml";
string smileCascadePath = "haarcascade_smile.xml";
string eyeCascadePath = "haarcascade_eye.xml";
CascadeClassifier faceCascade;
CascadeClassifier smileCascade;
CascadeClassifier eyeCascade;
// loading cascade
smileCascade.load(smileCascadePath);
faceCascade.load(haarCascadePath);
eyeCascade.load(eyeCascadePath);
VideoCapture video(1);
// check if video and cascade works
if (!video.isOpened())
{
cerr << "Error: Could not open camera." << endl;
return -1;
}
if (faceCascade.empty())
{
cerr << "Error: Could not load face detector." << endl;
return -1;
}
if (smileCascade.empty())
{
cerr << "Error: Could not load smile detector." << endl;
return -1;
}
if (eyeCascade.empty())
{
cerr << "Error: Could not load eye detector." << endl;
return -1;
}
// open stream video
while (true)
{
if (!video.read(img))
{
cerr << "Error: Could not read frame from camera." << endl;
video.release();
break;
}
vector<Rect> faces;
cvtColor(img, imgGray, COLOR_BGR2GRAY);
faceCascade.detectMultiScale(imgGray, faces, 1.3, 5);
// detecting faces
for (Rect &face : faces)
{
rectangle(img, face, Scalar(50, 50, 255), 3);
putText(img, "Face", face.tl() - Point2i(-2, 5), FONT_HERSHEY_SIMPLEX, 1, Scalar(50, 50, 255), 2);
Mat faceROI = imgGray(face);
vector<Rect> eyes;
eyeCascade.detectMultiScale(faceROI, eyes, 1.1, 20, 0 | CASCADE_SCALE_IMAGE, Size(5, 5));
vector<Rect> smiles;
smileCascade.detectMultiScale(faceROI, smiles, 1.1, 50, 0 | CASCADE_SCALE_IMAGE, Size(80, 80));
// detect eyes
for (Rect &eye : eyes)
{
rectangle(img, face.tl() + eye.tl(), face.tl() + eye.br(), Scalar(255, 50, 50), 2);
putText(img, "Eye", face.tl() + eye.tl() - Point2i(-2, 5), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 50, 50), 2);
if (smiles.size() <= 0)
{
// Resize teardrop image to fit the size of the detected eye
Mat resizedTeardrop;
resize(teardrop, resizedTeardrop, eye.size());
// Get the region of interest (ROI) for the teardrop
Mat teardropROI(resizedTeardrop.rows, resizedTeardrop.cols, CV_8UC4, Scalar(0, 0, 0, 0));
resizedTeardrop(Rect(0, 0, resizedTeardrop.cols, resizedTeardrop.rows)).copyTo(teardropROI);
// Calculate the position to overlay the teardrop
Point teardropPosition = face.tl() + eye.tl() + Point(0, eye.height / 1.5);
// Overlay the teardrop onto the original image
for (int y = 0; y < teardropROI.rows; ++y)
{
for (int x = 0; x < teardropROI.cols; ++x)
{
Vec4b teardropPixel = teardropROI.at<Vec4b>(y, x);
if (teardropPixel[3] > 0) // Check the alpha channel
{
img.at<Vec3b>(teardropPosition.y + y, teardropPosition.x + x)[0] = teardropPixel[0];
img.at<Vec3b>(teardropPosition.y + y, teardropPosition.x + x)[1] = teardropPixel[1];
img.at<Vec3b>(teardropPosition.y + y, teardropPosition.x + x)[2] = teardropPixel[2];
}
}
}
}
}
for (Rect &smile : smiles)
{
rectangle(img, face.tl() + smile.tl(), face.tl() + smile.br(), Scalar(50, 255, 50), 2);
putText(img, "Smile", face.tl() + smile.tl() - Point2i(-2, 5), FONT_HERSHEY_SIMPLEX, 1, Scalar(50, 255, 50), 2);
// Resize rainbowsmile image to fit the width of the detected smile
int resizedRainbowsmileWidth = smile.width;
int resizedRainbowsmileHeight = static_cast<int>((static_cast<float>(resizedRainbowsmileWidth) / rainbowsmile.cols) * rainbowsmile.rows);
Mat resizedRainbowsmile;
resize(rainbowsmile, resizedRainbowsmile, Size(resizedRainbowsmileWidth, resizedRainbowsmileHeight));
// Calculate the position to overlay the rainbowsmile at the center of the mouth
Point rainbowsmilePosition = face.tl() + smile.tl() + Point(smile.width / 2 - resizedRainbowsmile.cols / 2, (smile.height / 2 - resizedRainbowsmile.rows / 2) / 1.25);
// Overlay the rainbowsmile onto the original image with transparency
for (int y = 0; y < resizedRainbowsmile.rows; ++y)
{
for (int x = 0; x < resizedRainbowsmile.cols; ++x)
{
Vec4b rainbowsmilePixel = resizedRainbowsmile.at<Vec4b>(y, x);
if (rainbowsmilePixel[3] > 0) // Check the alpha channel
{
Vec3b &imgPixel = img.at<Vec3b>(rainbowsmilePosition.y + y, rainbowsmilePosition.x + x);
imgPixel[0] = static_cast<uchar>((rainbowsmilePixel[3] / 255.0) * rainbowsmilePixel[0] + (1.0 - rainbowsmilePixel[3] / 255.0) * imgPixel[0]);
imgPixel[1] = static_cast<uchar>((rainbowsmilePixel[3] / 255.0) * rainbowsmilePixel[1] + (1.0 - rainbowsmilePixel[3] / 255.0) * imgPixel[1]);
imgPixel[2] = static_cast<uchar>((rainbowsmilePixel[3] / 255.0) * rainbowsmilePixel[2] + (1.0 - rainbowsmilePixel[3] / 255.0) * imgPixel[2]);
}
}
}
}
}
imshow("Image", img);
if (waitKey(50) == 27) // Break the loop if the user presses the 'Esc' key (ASCII 27)
break;
}
video.release();
return 0;
}