-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFaceDetector.cpp
More file actions
executable file
·178 lines (150 loc) · 5.39 KB
/
Copy pathFaceDetector.cpp
File metadata and controls
executable file
·178 lines (150 loc) · 5.39 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
#include "FaceDetector.hpp"
/* declaration for function in giflib_to_opencv.cpp to convert
gif images to a image file that can be used in opencv */
cv::Mat imread_gif(const std::string& filename, const int flags);
int FaceDetector::loadCascade()
{
if (!faceHaarCascade_.load(HAAR_CASCADE_PATH))
{
std::cerr << "ERROR: Failed to load Haar Cascade from " << HAAR_CASCADE_PATH << std::endl;
return -1; // failure
}
return 0; // success
}
int FaceDetector::loadImage(std::string imagePath)
{
imagePath_ = imagePath;
std::string fileExtension;
if (imagePath.find_last_of(".") != std::string::npos)
{
fileExtension = imagePath.substr(imagePath.find_last_of(".")+1);
for(unsigned int i = 0; i < fileExtension.length(); ++i)
{
fileExtension[i] = tolower(fileExtension[i]);
}
}
// use correct read function based on file extension
if ((fileExtension == "jpg") || (fileExtension == "jpeg"))
{
image_ = cv::imread(imagePath);
}
else if (fileExtension == "png")
{
image_ = cv::imread(imagePath);
}
else if (fileExtension == "gif")
{
/* use gif_lib to load gif file */
int error = 0;
GifFileType* gifFile = DGifOpenFileName(imagePath.c_str(), &error); // open gif
if (!gifFile || (error != 0))
{
std::cerr << "ERROR: Could not open gif - " << GifErrorString(error) << std::endl;
return -2; // failure
}
if (DGifSlurp(gifFile) != GIF_OK) // read gif contents into memory structure
{
std::cerr << "ERROR: Could not load gif - " << GifErrorString(error) << std::endl;
return -2; // failure
}
const int width = gifFile->Image.Width;
const int height = gifFile->Image.Height;
image_ = cv::Mat(cv::Size(width, height), CV_8UC3);
GifColorType *colorMap = gifFile->SColorMap->Colors;
SavedImage *img = &gifFile->SavedImages[0];
auto *ptr = image_.ptr<cv::Vec3b>();
for (int i = 0; i < width * height; i++, ptr++) {
const GifColorType &color = colorMap[img->RasterBits[i]];
*ptr = cv::Vec3b(color.Red, color.Green, color.Blue);
}
cvtColor(image_, image_, CV_RGB2BGR);
DGifCloseFile(gifFile, &error);
}
else
{
std::cerr << "ERROR: Unrecognized file extension on " << imagePath << std::endl;
return -1; // failure
}
// verify that the image was loaded
if (image_.empty())
{
std::cerr << "ERROR: Failed to load image file " << imagePath << std::endl;
return -2; // failure
}
return 0; // success!
}
int FaceDetector::detectFaces()
{
if (image_.empty())
{
std::cerr << "ERROR: Cannot detect faces; image_ not loaded" << std::endl;
return -1; // failure
}
// convert image to grayscale
cv::Mat imageGrayScale;
cv::cvtColor(image_, imageGrayScale, cv::COLOR_BGR2GRAY);
cv::equalizeHist(imageGrayScale, imageGrayScale);
// use haar cascade to detect faces
faceHaarCascade_.detectMultiScale(imageGrayScale, faces_, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(250, 250));
return faces_.size(); // number of faces detected
}
void FaceDetector::displayImage()
{
if (image_.empty())
{
std::cerr << "ERROR: Cannot display image_; image_ not loaded" << std::endl;
return; // failure
}
for (int i = 0; i < faces_.size(); i++)
{
cv::Point center(faces_[i].x + faces_[i].width / 2, faces_[i].y + faces_[i].height / 2);
cv::ellipse(image_, center, cv::Size(faces_[i].width / 2, faces_[i].height / 2), 0, 0, 360, cv::Scalar(0, 255, 0), 10, 8, 0);
}
// scale image to fit within 1280x720 window
shrinkImage(1280, 720);
// create a window for display
cv::namedWindow("Face Detector", cv::WINDOW_AUTOSIZE);
cv::imshow("Face Detector", image_);
cv::waitKey(0);
}
void FaceDetector::saveJSON()
{
if (imagePath_.empty())
{
std::cerr << "ERROR: Cannot save JSON; imagePath_ not available" << std::endl;
return; // failure
}
Json::Value result;
Json::StreamWriterBuilder builder;
std::ofstream outFile;
// populate the JSON fields
result["countFaces"] = faces_.size();
char absolutePath [PATH_MAX + 1];
result["imageLocation"] = realpath(imagePath_.c_str(), absolutePath);
// create output file in same directory as image
std::string outFileName = imagePath_ + ".json";
outFile.open(outFileName);
builder.newStreamWriter()->write(result, &outFile);
outFile.close();
std::cout << "Saved " << outFileName << std::endl;
}
void FaceDetector::shrinkImage(int maxWidth, int maxHeight)
{
if (image_.empty())
{
std::cerr << "ERROR: Cannot scale image_; image_ not loaded" << std::endl;
return; // failure
}
// scale image_ to fit within maxWidth x maxHeight
cv::Size s = image_.size();
if ((s.width/s.height) > (maxWidth/maxHeight)) // scale based on width
{
cv::resize(image_, image_,
cv::Size(maxWidth, (s.height * maxWidth / s.width)));
}
else // scale based on height
{
cv::resize(image_, image_,
cv::Size((s.width * maxHeight / s.height), maxHeight));
}
}