JeVoisBase  1.6
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
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 // ####################################################################################################
22 FaceDetector::FaceDetector(std::string const & instance) :
23  jevois::Component(instance)
24 { }
25 
26 // ####################################################################################################
28 { }
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 // ####################################################################################################
49 void 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_HAAR_SCALE_IMAGE, cv::Size(img.cols / 2, img.rows / 2));
58 
59  // Create one entry in eyes vector for each face:
60  eyes.resize(faces.size());
61 
62  // Then, for each face, detect the eyes:
63  if (detect_eyes)
64  for (size_t i = 0; i < faces.size(); ++i)
65  {
66  // Get a crop around this face:
67  cv::Mat faceROI = img(faces[i]);
68 
69  // Detect eyes in the ROI:
70  itsEyesCascade->detectMultiScale(faceROI, eyes[i], 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE,
71  cv::Size(img.cols / 8, img.rows / 8), cv::Size(img.cols / 2, img.rows / 2));
72  }
73 }
74 
~FaceDetector()
Destructor.
Definition: FaceDetector.C:27
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.
Definition: FaceDetector.C:49
void postInit() override
Definition: FaceDetector.C:31
FaceDetector(std::string const &instance)
Constructor, loads the cascade classifiers from disk.
Definition: FaceDetector.C:22
std::shared_ptr< cv::CascadeClassifier > itsEyesCascade
Definition: FaceDetector.H:69
#define LFATAL(msg)
std::shared_ptr< cv::CascadeClassifier > itsFaceCascade
Definition: FaceDetector.H:68
std::string absolutePath(std::string const &path="")