Skip to content

Instantly share code, notes, and snippets.

@toopay
Last active December 16, 2015 22:39
Show Gist options
  • Save toopay/5508315 to your computer and use it in GitHub Desktop.
Save toopay/5508315 to your computer and use it in GitHub Desktop.
C++ Face Detect
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
const char* keys =
{
"{i|input| |The source image}"
"{d|dir| |The resource directory}"
};
int main(int argc, const char** argv)
{
cv::CommandLineParser parser(argc, argv, keys);
std::string infile = parser.get<std::string>("input");
std::string dir = parser.get<std::string>("dir");
std::string imgdir = "img/temp";
std::string resdir = "res";
std::string outdir = dir + imgdir;
std::string cascade_xml = "/haarcascade_frontalface_alt.xml";
std::string cascade_file = dir + resdir + cascade_xml;
cv::CascadeClassifier cascade;
if (cascade_file.empty() || !cascade.load(cascade_file))
{
std::cout << cv::format("Error: cannot load cascade file! %s %s\n", outdir.c_str(),cascade_file.c_str());
return -1;
}
cv::Mat src = cv::imread(infile);
if (src.empty())
{
std::cout << cv::format("Error: cannot load source image!\n");
return -1;
}
cv::Mat gray;
cv::cvtColor(src, gray, CV_BGR2GRAY);
cv::equalizeHist(gray, gray);
std::vector<cv::Rect> faces;
cascade.detectMultiScale(gray, faces, 1.2, 3);
std::cout << cv::format("0, %s (%dx%d)\n", infile.c_str(), src.cols, src.rows);
cv::Mat src_copy = src.clone();
for (int i = 0; i < faces.size(); i++)
{
std::string outfile(cv::format("%s/face-%d.jpg", outdir.c_str(), i+1));
cv::Rect r = faces[i];
cv::rectangle(src, r, CV_RGB(0,255,0), 2);
cv::imwrite(outfile, src_copy(r));
cv::imwrite(infile, src);
std::cout << cv::format("%d, %s (%dx%d)\n", i+1, outfile.c_str(), r.width, r.height);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment