JeVoisBase  1.2
JeVois Smart Embedded Machine Vision Toolkit Base Modules
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  LINFO("my path is " << absolutePath());
34  std::string const & facename = facedetector::face_cascade::get();
35  if (facename.empty()) LFATAL("face_cascade paremeter cannot be empty");
36  std::string const facefile = absolutePath(facename);
37  itsFaceCascade.reset(new cv::CascadeClassifier(facefile));
38  if (itsFaceCascade->empty()) LFATAL("Error loading face cascade file " << facefile);
39 
40  std::string const & eyename = facedetector::eye_cascade::get();
41  if (eyename.empty() == false)
42  {
43  std::string const eyefile = absolutePath(eyename);
44  itsEyesCascade.reset(new cv::CascadeClassifier(eyefile));
45  if (itsEyesCascade->empty()) LFATAL("Error loading eye cascade file " << eyefile);
46  }
47 }
48 
49 // ####################################################################################################
50 void FaceDetector::process(cv::Mat const & img, std::vector<cv::Rect> & faces,
51  std::vector<std::vector<cv::Rect> > & eyes, bool detect_eyes)
52 {
53  // Clear any input junk:
54  faces.clear();
55  eyes.clear();
56 
57  // First, detect the faces:
58  itsFaceCascade->detectMultiScale(img, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, 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_HAAR_SCALE_IMAGE,
72  cv::Size(img.cols / 8, img.rows / 8), cv::Size(img.cols / 2, img.rows / 2));
73  }
74 }
75 
~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:50
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
#define LINFO(msg)
std::string absolutePath(std::string const &path="")