JeVoisBase  1.21
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
FaceDetector.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
19#include <jevois/Debug/Log.H>
20
21// ####################################################################################################
22FaceDetector::FaceDetector(std::string const & instance) :
23 jevois::Component(instance)
24{ }
25
26// ####################################################################################################
29
30// ####################################################################################################
32{
33 std::string const & facename = facedetector::face_cascade::get();
34 if (facename.empty()) LFATAL("face_cascade parameter cannot be empty");
35 std::string const facefile = absolutePath(facename);
36 itsFaceCascade.reset(new cv::CascadeClassifier(facefile));
37 if (itsFaceCascade->empty()) LFATAL("Error loading face cascade file " << facefile);
38
39 std::string const & eyename = facedetector::eye_cascade::get();
40 if (eyename.empty() == false)
41 {
42 std::string const eyefile = absolutePath(eyename);
43 itsEyesCascade.reset(new cv::CascadeClassifier(eyefile));
44 if (itsEyesCascade->empty()) LFATAL("Error loading eye cascade file " << eyefile);
45 }
46}
47
48// ####################################################################################################
49void FaceDetector::process(cv::Mat const & img, std::vector<cv::Rect> & faces,
50 std::vector<std::vector<cv::Rect> > & eyes, bool detect_eyes)
51{
52 // Clear any input junk:
53 faces.clear();
54 eyes.clear();
55
56 // First, detect the faces:
57 itsFaceCascade->detectMultiScale(img, faces, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE,
58 cv::Size(img.cols / 2, img.rows / 2));
59
60 // Create one entry in eyes vector for each face:
61 eyes.resize(faces.size());
62
63 // Then, for each face, detect the eyes:
64 if (detect_eyes)
65 for (size_t i = 0; i < faces.size(); ++i)
66 {
67 // Get a crop around this face:
68 cv::Mat faceROI = img(faces[i]);
69
70 // Detect eyes in the ROI:
71 itsEyesCascade->detectMultiScale(faceROI, eyes[i], 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE,
72 cv::Size(img.cols / 8, img.rows / 8), cv::Size(img.cols / 2, img.rows / 2));
73 }
74}
75
FaceDetector(std::string const &instance)
Constructor, loads the cascade classifiers from disk.
std::shared_ptr< cv::CascadeClassifier > itsFaceCascade
~FaceDetector()
Destructor.
void postInit() override
std::shared_ptr< cv::CascadeClassifier > itsEyesCascade
void process(cv::Mat const &img, std::vector< cv::Rect > &faces, std::vector< std::vector< cv::Rect > > &eyes, bool detect_eyes=false)
Process an image, results are held in our data members.
std::filesystem::path absolutePath(std::filesystem::path const &path="")
#define LFATAL(msg)