mirror of
https://github.com/opencv/opencv.git
synced 2026-07-28 23:03:03 +04:00
Move objdetect HaarCascadeClassifier and HOGDescriptor to contrib xobjdetect (#25198)
* Move objdetect parts to contrib * Move objdetect parts to contrib * Minor fixes.
This commit is contained in:
@@ -1,111 +0,0 @@
|
||||
#if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(ANDROID) || (defined(_MSC_VER) && _MSC_VER>=1800)
|
||||
|
||||
#include <opencv2/imgproc.hpp> // Gaussian Blur
|
||||
#include <opencv2/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
|
||||
#include <opencv2/videoio.hpp>
|
||||
#include <opencv2/highgui.hpp> // OpenCV window I/O
|
||||
#include <opencv2/features2d.hpp>
|
||||
#include <opencv2/objdetect.hpp>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
const string WindowName = "Face Detection example";
|
||||
|
||||
class CascadeDetectorAdapter: public DetectionBasedTracker::IDetector
|
||||
{
|
||||
public:
|
||||
CascadeDetectorAdapter(cv::Ptr<cv::CascadeClassifier> detector):
|
||||
IDetector(),
|
||||
Detector(detector)
|
||||
{
|
||||
CV_Assert(detector);
|
||||
}
|
||||
|
||||
void detect(const cv::Mat &Image, std::vector<cv::Rect> &objects) CV_OVERRIDE
|
||||
{
|
||||
Detector->detectMultiScale(Image, objects, scaleFactor, minNeighbours, 0, minObjSize, maxObjSize);
|
||||
}
|
||||
|
||||
virtual ~CascadeDetectorAdapter() CV_OVERRIDE
|
||||
{}
|
||||
|
||||
private:
|
||||
CascadeDetectorAdapter();
|
||||
cv::Ptr<cv::CascadeClassifier> Detector;
|
||||
};
|
||||
|
||||
int main(int , char** )
|
||||
{
|
||||
namedWindow(WindowName);
|
||||
|
||||
VideoCapture VideoStream(0);
|
||||
|
||||
if (!VideoStream.isOpened())
|
||||
{
|
||||
printf("Error: Cannot open video stream from camera\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string cascadeFrontalfilename = samples::findFile("data/lbpcascades/lbpcascade_frontalface.xml");
|
||||
cv::Ptr<cv::CascadeClassifier> cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename);
|
||||
cv::Ptr<DetectionBasedTracker::IDetector> MainDetector = makePtr<CascadeDetectorAdapter>(cascade);
|
||||
if ( cascade->empty() )
|
||||
{
|
||||
printf("Error: Cannot load %s\n", cascadeFrontalfilename.c_str());
|
||||
return 2;
|
||||
}
|
||||
|
||||
cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename);
|
||||
cv::Ptr<DetectionBasedTracker::IDetector> TrackingDetector = makePtr<CascadeDetectorAdapter>(cascade);
|
||||
if ( cascade->empty() )
|
||||
{
|
||||
printf("Error: Cannot load %s\n", cascadeFrontalfilename.c_str());
|
||||
return 2;
|
||||
}
|
||||
|
||||
DetectionBasedTracker::Parameters params;
|
||||
DetectionBasedTracker Detector(MainDetector, TrackingDetector, params);
|
||||
|
||||
if (!Detector.run())
|
||||
{
|
||||
printf("Error: Detector initialization failed\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
Mat ReferenceFrame;
|
||||
Mat GrayFrame;
|
||||
vector<Rect> Faces;
|
||||
|
||||
do
|
||||
{
|
||||
VideoStream >> ReferenceFrame;
|
||||
cvtColor(ReferenceFrame, GrayFrame, COLOR_BGR2GRAY);
|
||||
Detector.process(GrayFrame);
|
||||
Detector.getObjects(Faces);
|
||||
|
||||
for (size_t i = 0; i < Faces.size(); i++)
|
||||
{
|
||||
rectangle(ReferenceFrame, Faces[i], Scalar(0,255,0));
|
||||
}
|
||||
|
||||
imshow(WindowName, ReferenceFrame);
|
||||
} while (waitKey(30) < 0);
|
||||
|
||||
Detector.stop();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
printf("This sample works for UNIX or ANDROID or Visual Studio 2013+ only\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,257 +0,0 @@
|
||||
#include "opencv2/objdetect.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/videoio.hpp"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
static void help(const char** argv)
|
||||
{
|
||||
cout << "\nThis program demonstrates the use of cv::CascadeClassifier class to detect objects (Face + eyes). You can use Haar or LBP features.\n"
|
||||
"This classifier can recognize many kinds of rigid objects, once the appropriate classifier is trained.\n"
|
||||
"It's most known use is for faces.\n"
|
||||
"Usage:\n"
|
||||
<< argv[0]
|
||||
<< " [--cascade=<cascade_path> this is the primary trained classifier such as frontal face]\n"
|
||||
" [--nested-cascade[=nested_cascade_path this an optional secondary classifier such as eyes]]\n"
|
||||
" [--scale=<image scale greater or equal to 1, try 1.3 for example>]\n"
|
||||
" [--try-flip]\n"
|
||||
" [filename|camera_index]\n\n"
|
||||
"example:\n"
|
||||
<< argv[0]
|
||||
<< " --cascade=\"data/haarcascades/haarcascade_frontalface_alt.xml\" --nested-cascade=\"data/haarcascades/haarcascade_eye_tree_eyeglasses.xml\" --scale=1.3\n\n"
|
||||
"During execution:\n\tHit any key to quit.\n"
|
||||
"\tUsing OpenCV version " << CV_VERSION << "\n" << endl;
|
||||
}
|
||||
|
||||
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
|
||||
CascadeClassifier& nestedCascade,
|
||||
double scale, bool tryflip );
|
||||
|
||||
string cascadeName;
|
||||
string nestedCascadeName;
|
||||
|
||||
int main( int argc, const char** argv )
|
||||
{
|
||||
VideoCapture capture;
|
||||
Mat frame, image;
|
||||
string inputName;
|
||||
bool tryflip;
|
||||
CascadeClassifier cascade, nestedCascade;
|
||||
double scale;
|
||||
|
||||
cv::CommandLineParser parser(argc, argv,
|
||||
"{help h||}"
|
||||
"{cascade|data/haarcascades/haarcascade_frontalface_alt.xml|}"
|
||||
"{nested-cascade|data/haarcascades/haarcascade_eye_tree_eyeglasses.xml|}"
|
||||
"{scale|1|}{try-flip||}{@filename||}"
|
||||
);
|
||||
if (parser.has("help"))
|
||||
{
|
||||
help(argv);
|
||||
return 0;
|
||||
}
|
||||
cascadeName = parser.get<string>("cascade");
|
||||
nestedCascadeName = parser.get<string>("nested-cascade");
|
||||
scale = parser.get<double>("scale");
|
||||
if (scale < 1)
|
||||
scale = 1;
|
||||
tryflip = parser.has("try-flip");
|
||||
inputName = parser.get<string>("@filename");
|
||||
if (!parser.check())
|
||||
{
|
||||
parser.printErrors();
|
||||
return 0;
|
||||
}
|
||||
if (!nestedCascade.load(samples::findFileOrKeep(nestedCascadeName)))
|
||||
cerr << "WARNING: Could not load classifier cascade for nested objects" << endl;
|
||||
if (!cascade.load(samples::findFile(cascadeName)))
|
||||
{
|
||||
cerr << "ERROR: Could not load classifier cascade" << endl;
|
||||
help(argv);
|
||||
return -1;
|
||||
}
|
||||
if( inputName.empty() || (isdigit(inputName[0]) && inputName.size() == 1) )
|
||||
{
|
||||
int camera = inputName.empty() ? 0 : inputName[0] - '0';
|
||||
if(!capture.open(camera))
|
||||
{
|
||||
cout << "Capture from camera #" << camera << " didn't work" << endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (!inputName.empty())
|
||||
{
|
||||
image = imread(samples::findFileOrKeep(inputName), IMREAD_COLOR);
|
||||
if (image.empty())
|
||||
{
|
||||
if (!capture.open(samples::findFileOrKeep(inputName)))
|
||||
{
|
||||
cout << "Could not read " << inputName << endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
image = imread(samples::findFile("lena.jpg"), IMREAD_COLOR);
|
||||
if (image.empty())
|
||||
{
|
||||
cout << "Couldn't read lena.jpg" << endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if( capture.isOpened() )
|
||||
{
|
||||
cout << "Video capturing has been started ..." << endl;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
capture >> frame;
|
||||
if( frame.empty() )
|
||||
break;
|
||||
|
||||
Mat frame1 = frame.clone();
|
||||
detectAndDraw( frame1, cascade, nestedCascade, scale, tryflip );
|
||||
|
||||
char c = (char)waitKey(10);
|
||||
if( c == 27 || c == 'q' || c == 'Q' )
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Detecting face(s) in " << inputName << endl;
|
||||
if( !image.empty() )
|
||||
{
|
||||
detectAndDraw( image, cascade, nestedCascade, scale, tryflip );
|
||||
waitKey(0);
|
||||
}
|
||||
else if( !inputName.empty() )
|
||||
{
|
||||
/* assume it is a text file containing the
|
||||
list of the image filenames to be processed - one per line */
|
||||
FILE* f = fopen( inputName.c_str(), "rt" );
|
||||
if( f )
|
||||
{
|
||||
char buf[1000+1];
|
||||
while( fgets( buf, 1000, f ) )
|
||||
{
|
||||
int len = (int)strlen(buf);
|
||||
while( len > 0 && isspace(buf[len-1]) )
|
||||
len--;
|
||||
buf[len] = '\0';
|
||||
cout << "file " << buf << endl;
|
||||
image = imread( buf, IMREAD_COLOR );
|
||||
if( !image.empty() )
|
||||
{
|
||||
detectAndDraw( image, cascade, nestedCascade, scale, tryflip );
|
||||
char c = (char)waitKey(0);
|
||||
if( c == 27 || c == 'q' || c == 'Q' )
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "Aw snap, couldn't read image " << buf << endl;
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
|
||||
CascadeClassifier& nestedCascade,
|
||||
double scale, bool tryflip )
|
||||
{
|
||||
double t = 0;
|
||||
vector<Rect> faces, faces2;
|
||||
const static Scalar colors[] =
|
||||
{
|
||||
Scalar(255,0,0),
|
||||
Scalar(255,128,0),
|
||||
Scalar(255,255,0),
|
||||
Scalar(0,255,0),
|
||||
Scalar(0,128,255),
|
||||
Scalar(0,255,255),
|
||||
Scalar(0,0,255),
|
||||
Scalar(255,0,255)
|
||||
};
|
||||
Mat gray, smallImg;
|
||||
|
||||
cvtColor( img, gray, COLOR_BGR2GRAY );
|
||||
double fx = 1 / scale;
|
||||
resize( gray, smallImg, Size(), fx, fx, INTER_LINEAR_EXACT );
|
||||
equalizeHist( smallImg, smallImg );
|
||||
|
||||
t = (double)getTickCount();
|
||||
cascade.detectMultiScale( smallImg, faces,
|
||||
1.1, 2, 0
|
||||
//|CASCADE_FIND_BIGGEST_OBJECT
|
||||
//|CASCADE_DO_ROUGH_SEARCH
|
||||
|CASCADE_SCALE_IMAGE,
|
||||
Size(30, 30) );
|
||||
if( tryflip )
|
||||
{
|
||||
flip(smallImg, smallImg, 1);
|
||||
cascade.detectMultiScale( smallImg, faces2,
|
||||
1.1, 2, 0
|
||||
//|CASCADE_FIND_BIGGEST_OBJECT
|
||||
//|CASCADE_DO_ROUGH_SEARCH
|
||||
|CASCADE_SCALE_IMAGE,
|
||||
Size(30, 30) );
|
||||
for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); ++r )
|
||||
{
|
||||
faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
|
||||
}
|
||||
}
|
||||
t = (double)getTickCount() - t;
|
||||
printf( "detection time = %g ms\n", t*1000/getTickFrequency());
|
||||
for ( size_t i = 0; i < faces.size(); i++ )
|
||||
{
|
||||
Rect r = faces[i];
|
||||
Mat smallImgROI;
|
||||
vector<Rect> nestedObjects;
|
||||
Point center;
|
||||
Scalar color = colors[i%8];
|
||||
int radius;
|
||||
|
||||
double aspect_ratio = (double)r.width/r.height;
|
||||
if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
|
||||
{
|
||||
center.x = cvRound((r.x + r.width*0.5)*scale);
|
||||
center.y = cvRound((r.y + r.height*0.5)*scale);
|
||||
radius = cvRound((r.width + r.height)*0.25*scale);
|
||||
circle( img, center, radius, color, 3, 8, 0 );
|
||||
}
|
||||
else
|
||||
rectangle( img, Point(cvRound(r.x*scale), cvRound(r.y*scale)),
|
||||
Point(cvRound((r.x + r.width-1)*scale), cvRound((r.y + r.height-1)*scale)),
|
||||
color, 3, 8, 0);
|
||||
if( nestedCascade.empty() )
|
||||
continue;
|
||||
smallImgROI = smallImg( r );
|
||||
nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
|
||||
1.1, 2, 0
|
||||
//|CASCADE_FIND_BIGGEST_OBJECT
|
||||
//|CASCADE_DO_ROUGH_SEARCH
|
||||
//|CASCADE_DO_CANNY_PRUNING
|
||||
|CASCADE_SCALE_IMAGE,
|
||||
Size(30, 30) );
|
||||
for ( size_t j = 0; j < nestedObjects.size(); j++ )
|
||||
{
|
||||
Rect nr = nestedObjects[j];
|
||||
center.x = cvRound((r.x + nr.x + nr.width*0.5)*scale);
|
||||
center.y = cvRound((r.y + nr.y + nr.height*0.5)*scale);
|
||||
radius = cvRound((nr.width + nr.height)*0.25*scale);
|
||||
circle( img, center, radius, color, 3, 8, 0 );
|
||||
}
|
||||
}
|
||||
imshow( "result", img );
|
||||
}
|
||||
@@ -1,215 +0,0 @@
|
||||
/*
|
||||
* Author: Samyak Datta (datta[dot]samyak[at]gmail.com)
|
||||
*
|
||||
* A program to detect facial feature points using
|
||||
* Haarcascade classifiers for face, eyes, nose and mouth
|
||||
*
|
||||
*/
|
||||
|
||||
#include "opencv2/objdetect.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
// Functions for facial feature detection
|
||||
static void help(char** argv);
|
||||
static void detectFaces(Mat&, vector<Rect_<int> >&, string);
|
||||
static void detectEyes(Mat&, vector<Rect_<int> >&, string);
|
||||
static void detectNose(Mat&, vector<Rect_<int> >&, string);
|
||||
static void detectMouth(Mat&, vector<Rect_<int> >&, string);
|
||||
static void detectFacialFeaures(Mat&, const vector<Rect_<int> >, string, string, string);
|
||||
|
||||
string input_image_path;
|
||||
string face_cascade_path, eye_cascade_path, nose_cascade_path, mouth_cascade_path;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
cv::CommandLineParser parser(argc, argv,
|
||||
"{eyes||}{nose||}{mouth||}{help h||}{@image||}{@facexml||}");
|
||||
if (parser.has("help"))
|
||||
{
|
||||
help(argv);
|
||||
return 0;
|
||||
}
|
||||
input_image_path = parser.get<string>("@image");
|
||||
face_cascade_path = parser.get<string>("@facexml");
|
||||
eye_cascade_path = parser.has("eyes") ? parser.get<string>("eyes") : "";
|
||||
nose_cascade_path = parser.has("nose") ? parser.get<string>("nose") : "";
|
||||
mouth_cascade_path = parser.has("mouth") ? parser.get<string>("mouth") : "";
|
||||
if (input_image_path.empty() || face_cascade_path.empty())
|
||||
{
|
||||
cout << "IMAGE or FACE_CASCADE are not specified";
|
||||
return 1;
|
||||
}
|
||||
// Load image and cascade classifier files
|
||||
Mat image;
|
||||
image = imread(samples::findFile(input_image_path));
|
||||
|
||||
// Detect faces and facial features
|
||||
vector<Rect_<int> > faces;
|
||||
detectFaces(image, faces, face_cascade_path);
|
||||
detectFacialFeaures(image, faces, eye_cascade_path, nose_cascade_path, mouth_cascade_path);
|
||||
|
||||
imshow("Result", image);
|
||||
|
||||
waitKey(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void help(char** argv)
|
||||
{
|
||||
cout << "\nThis file demonstrates facial feature points detection using Haarcascade classifiers.\n"
|
||||
"The program detects a face and eyes, nose and mouth inside the face."
|
||||
"The code has been tested on the Japanese Female Facial Expression (JAFFE) database and found"
|
||||
"to give reasonably accurate results. \n";
|
||||
|
||||
cout << "\nUSAGE: " << argv[0] << " [IMAGE] [FACE_CASCADE] [OPTIONS]\n"
|
||||
"IMAGE\n\tPath to the image of a face taken as input.\n"
|
||||
"FACE_CASCSDE\n\t Path to a haarcascade classifier for face detection.\n"
|
||||
"OPTIONS: \nThere are 3 options available which are described in detail. There must be a "
|
||||
"space between the option and it's argument (All three options accept arguments).\n"
|
||||
"\t-eyes=<eyes_cascade> : Specify the haarcascade classifier for eye detection.\n"
|
||||
"\t-nose=<nose_cascade> : Specify the haarcascade classifier for nose detection.\n"
|
||||
"\t-mouth=<mouth-cascade> : Specify the haarcascade classifier for mouth detection.\n";
|
||||
|
||||
|
||||
cout << "EXAMPLE:\n"
|
||||
"(1) " << argv[0] << " image.jpg face.xml -eyes=eyes.xml -mouth=mouth.xml\n"
|
||||
"\tThis will detect the face, eyes and mouth in image.jpg.\n"
|
||||
"(2) " << argv[0] << " image.jpg face.xml -nose=nose.xml\n"
|
||||
"\tThis will detect the face and nose in image.jpg.\n"
|
||||
"(3) " << argv[0] << " image.jpg face.xml\n"
|
||||
"\tThis will detect only the face in image.jpg.\n";
|
||||
|
||||
cout << " \n\nThe classifiers for face and eyes can be downloaded from : "
|
||||
" \nhttps://github.com/opencv/opencv/tree/5.x/data/haarcascades";
|
||||
|
||||
cout << "\n\nThe classifiers for nose and mouth can be downloaded from : "
|
||||
" \nhttps://github.com/opencv/opencv_contrib/tree/5.x/modules/face/data/cascades\n";
|
||||
}
|
||||
|
||||
static void detectFaces(Mat& img, vector<Rect_<int> >& faces, string cascade_path)
|
||||
{
|
||||
CascadeClassifier face_cascade;
|
||||
face_cascade.load(samples::findFile(cascade_path));
|
||||
|
||||
if (!face_cascade.empty())
|
||||
face_cascade.detectMultiScale(img, faces, 1.15, 3, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
|
||||
return;
|
||||
}
|
||||
|
||||
static void detectFacialFeaures(Mat& img, const vector<Rect_<int> > faces, string eye_cascade,
|
||||
string nose_cascade, string mouth_cascade)
|
||||
{
|
||||
for(unsigned int i = 0; i < faces.size(); ++i)
|
||||
{
|
||||
// Mark the bounding box enclosing the face
|
||||
Rect face = faces[i];
|
||||
rectangle(img, Point(face.x, face.y), Point(face.x+face.width, face.y+face.height),
|
||||
Scalar(255, 0, 0), 1, 4);
|
||||
|
||||
// Eyes, nose and mouth will be detected inside the face (region of interest)
|
||||
Mat ROI = img(Rect(face.x, face.y, face.width, face.height));
|
||||
|
||||
// Check if all features (eyes, nose and mouth) are being detected
|
||||
bool is_full_detection = false;
|
||||
if( (!eye_cascade.empty()) && (!nose_cascade.empty()) && (!mouth_cascade.empty()) )
|
||||
is_full_detection = true;
|
||||
|
||||
// Detect eyes if classifier provided by the user
|
||||
if(!eye_cascade.empty())
|
||||
{
|
||||
vector<Rect_<int> > eyes;
|
||||
detectEyes(ROI, eyes, eye_cascade);
|
||||
|
||||
// Mark points corresponding to the centre of the eyes
|
||||
for(unsigned int j = 0; j < eyes.size(); ++j)
|
||||
{
|
||||
Rect e = eyes[j];
|
||||
circle(ROI, Point(e.x+e.width/2, e.y+e.height/2), 3, Scalar(0, 255, 0), -1, 8);
|
||||
/* rectangle(ROI, Point(e.x, e.y), Point(e.x+e.width, e.y+e.height),
|
||||
Scalar(0, 255, 0), 1, 4); */
|
||||
}
|
||||
}
|
||||
|
||||
// Detect nose if classifier provided by the user
|
||||
double nose_center_height = 0.0;
|
||||
if(!nose_cascade.empty())
|
||||
{
|
||||
vector<Rect_<int> > nose;
|
||||
detectNose(ROI, nose, nose_cascade);
|
||||
|
||||
// Mark points corresponding to the centre (tip) of the nose
|
||||
for(unsigned int j = 0; j < nose.size(); ++j)
|
||||
{
|
||||
Rect n = nose[j];
|
||||
circle(ROI, Point(n.x+n.width/2, n.y+n.height/2), 3, Scalar(0, 255, 0), -1, 8);
|
||||
nose_center_height = (n.y + n.height/2);
|
||||
}
|
||||
}
|
||||
|
||||
// Detect mouth if classifier provided by the user
|
||||
double mouth_center_height = 0.0;
|
||||
if(!mouth_cascade.empty())
|
||||
{
|
||||
vector<Rect_<int> > mouth;
|
||||
detectMouth(ROI, mouth, mouth_cascade);
|
||||
|
||||
for(unsigned int j = 0; j < mouth.size(); ++j)
|
||||
{
|
||||
Rect m = mouth[j];
|
||||
mouth_center_height = (m.y + m.height/2);
|
||||
|
||||
// The mouth should lie below the nose
|
||||
if( (is_full_detection) && (mouth_center_height > nose_center_height) )
|
||||
{
|
||||
rectangle(ROI, Point(m.x, m.y), Point(m.x+m.width, m.y+m.height), Scalar(0, 255, 0), 1, 4);
|
||||
}
|
||||
else if( (is_full_detection) && (mouth_center_height <= nose_center_height) )
|
||||
continue;
|
||||
else
|
||||
rectangle(ROI, Point(m.x, m.y), Point(m.x+m.width, m.y+m.height), Scalar(0, 255, 0), 1, 4);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static void detectEyes(Mat& img, vector<Rect_<int> >& eyes, string cascade_path)
|
||||
{
|
||||
CascadeClassifier eyes_cascade;
|
||||
eyes_cascade.load(samples::findFile(cascade_path, !cascade_path.empty()));
|
||||
|
||||
if (!eyes_cascade.empty())
|
||||
eyes_cascade.detectMultiScale(img, eyes, 1.20, 5, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
|
||||
return;
|
||||
}
|
||||
|
||||
static void detectNose(Mat& img, vector<Rect_<int> >& nose, string cascade_path)
|
||||
{
|
||||
CascadeClassifier nose_cascade;
|
||||
nose_cascade.load(samples::findFile(cascade_path, !cascade_path.empty()));
|
||||
|
||||
if (!nose_cascade.empty())
|
||||
nose_cascade.detectMultiScale(img, nose, 1.20, 5, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
|
||||
return;
|
||||
}
|
||||
|
||||
static void detectMouth(Mat& img, vector<Rect_<int> >& mouth, string cascade_path)
|
||||
{
|
||||
CascadeClassifier mouth_cascade;
|
||||
mouth_cascade.load(samples::findFile(cascade_path, !cascade_path.empty()));
|
||||
|
||||
if (!mouth_cascade.empty())
|
||||
mouth_cascade.detectMultiScale(img, mouth, 1.20, 5, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
|
||||
return;
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html
|
||||
|
||||
#include <opencv2/objdetect.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/videoio.hpp>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
class Detector
|
||||
{
|
||||
enum Mode { Default, Daimler } m;
|
||||
HOGDescriptor hog, hog_d;
|
||||
public:
|
||||
Detector() : m(Default), hog(), hog_d(Size(48, 96), Size(16, 16), Size(8, 8), Size(8, 8), 9)
|
||||
{
|
||||
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
|
||||
hog_d.setSVMDetector(HOGDescriptor::getDaimlerPeopleDetector());
|
||||
}
|
||||
void toggleMode() { m = (m == Default ? Daimler : Default); }
|
||||
string modeName() const { return (m == Default ? "Default" : "Daimler"); }
|
||||
vector<Rect> detect(InputArray img)
|
||||
{
|
||||
// Run the detector with default parameters. to get a higher hit-rate
|
||||
// (and more false alarms, respectively), decrease the hitThreshold and
|
||||
// groupThreshold (set groupThreshold to 0 to turn off the grouping completely).
|
||||
vector<Rect> found;
|
||||
if (m == Default)
|
||||
hog.detectMultiScale(img, found, 0, Size(8,8), Size(), 1.05, 2, false);
|
||||
else if (m == Daimler)
|
||||
hog_d.detectMultiScale(img, found, 0, Size(8,8), Size(), 1.05, 2, true);
|
||||
return found;
|
||||
}
|
||||
void adjustRect(Rect & r) const
|
||||
{
|
||||
// The HOG detector returns slightly larger rectangles than the real objects,
|
||||
// so we slightly shrink the rectangles to get a nicer output.
|
||||
r.x += cvRound(r.width*0.1);
|
||||
r.width = cvRound(r.width*0.8);
|
||||
r.y += cvRound(r.height*0.07);
|
||||
r.height = cvRound(r.height*0.8);
|
||||
}
|
||||
};
|
||||
|
||||
static const string keys = "{ help h | | print help message }"
|
||||
"{ camera c | 0 | capture video from camera (device index starting from 0) }"
|
||||
"{ video v | | use video as input }";
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
CommandLineParser parser(argc, argv, keys);
|
||||
parser.about("This sample demonstrates the use of the HoG descriptor.");
|
||||
if (parser.has("help"))
|
||||
{
|
||||
parser.printMessage();
|
||||
return 0;
|
||||
}
|
||||
int camera = parser.get<int>("camera");
|
||||
string file = parser.get<string>("video");
|
||||
if (!parser.check())
|
||||
{
|
||||
parser.printErrors();
|
||||
return 1;
|
||||
}
|
||||
|
||||
VideoCapture cap;
|
||||
if (file.empty())
|
||||
cap.open(camera);
|
||||
else
|
||||
{
|
||||
file = samples::findFileOrKeep(file);
|
||||
cap.open(file);
|
||||
}
|
||||
if (!cap.isOpened())
|
||||
{
|
||||
cout << "Can not open video stream: '" << (file.empty() ? "<camera>" : file) << "'" << endl;
|
||||
return 2;
|
||||
}
|
||||
|
||||
cout << "Press 'q' or <ESC> to quit." << endl;
|
||||
cout << "Press <space> to toggle between Default and Daimler detector" << endl;
|
||||
Detector detector;
|
||||
Mat frame;
|
||||
for (;;)
|
||||
{
|
||||
cap >> frame;
|
||||
if (frame.empty())
|
||||
{
|
||||
cout << "Finished reading: empty frame" << endl;
|
||||
break;
|
||||
}
|
||||
int64 t = getTickCount();
|
||||
vector<Rect> found = detector.detect(frame);
|
||||
t = getTickCount() - t;
|
||||
|
||||
// show the window
|
||||
{
|
||||
ostringstream buf;
|
||||
buf << "Mode: " << detector.modeName() << " ||| "
|
||||
<< "FPS: " << fixed << setprecision(1) << (getTickFrequency() / (double)t);
|
||||
putText(frame, buf.str(), Point(10, 30), FONT_HERSHEY_PLAIN, 2.0, Scalar(0, 0, 255), 2, LINE_AA);
|
||||
}
|
||||
for (vector<Rect>::iterator i = found.begin(); i != found.end(); ++i)
|
||||
{
|
||||
Rect &r = *i;
|
||||
detector.adjustRect(r);
|
||||
rectangle(frame, r.tl(), r.br(), cv::Scalar(0, 255, 0), 2);
|
||||
}
|
||||
imshow("People detector", frame);
|
||||
|
||||
// interact with user
|
||||
const char key = (char)waitKey(1);
|
||||
if (key == 27 || key == 'q') // ESC
|
||||
{
|
||||
cout << "Exit requested" << endl;
|
||||
break;
|
||||
}
|
||||
else if (key == ' ')
|
||||
{
|
||||
detector.toggleMode();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -1,215 +0,0 @@
|
||||
#include "opencv2/objdetect.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/videoio.hpp"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
static void help(const char** argv)
|
||||
{
|
||||
cout << "\nThis program demonstrates the smile detector.\n"
|
||||
"Usage:\n" <<
|
||||
argv[0] << " [--cascade=<cascade_path> this is the frontal face classifier]\n"
|
||||
" [--smile-cascade=[<smile_cascade_path>]]\n"
|
||||
" [--scale=<image scale greater or equal to 1, try 2.0 for example. The larger the faster the processing>]\n"
|
||||
" [--try-flip]\n"
|
||||
" [video_filename|camera_index]\n\n"
|
||||
"Example:\n" <<
|
||||
argv[0] << " --cascade=\"data/haarcascades/haarcascade_frontalface_alt.xml\" --smile-cascade=\"data/haarcascades/haarcascade_smile.xml\" --scale=2.0\n\n"
|
||||
"During execution:\n\tHit any key to quit.\n"
|
||||
"\tUsing OpenCV version " << CV_VERSION << "\n" << endl;
|
||||
}
|
||||
|
||||
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
|
||||
CascadeClassifier& nestedCascade,
|
||||
double scale, bool tryflip );
|
||||
|
||||
string cascadeName;
|
||||
string nestedCascadeName;
|
||||
|
||||
int main( int argc, const char** argv )
|
||||
{
|
||||
VideoCapture capture;
|
||||
Mat frame, image;
|
||||
string inputName;
|
||||
bool tryflip;
|
||||
|
||||
help(argv);
|
||||
|
||||
CascadeClassifier cascade, nestedCascade;
|
||||
double scale;
|
||||
cv::CommandLineParser parser(argc, argv,
|
||||
"{help h||}{scale|1|}"
|
||||
"{cascade|data/haarcascades/haarcascade_frontalface_alt.xml|}"
|
||||
"{smile-cascade|data/haarcascades/haarcascade_smile.xml|}"
|
||||
"{try-flip||}{@input||}");
|
||||
if (parser.has("help"))
|
||||
{
|
||||
help(argv);
|
||||
return 0;
|
||||
}
|
||||
cascadeName = samples::findFile(parser.get<string>("cascade"));
|
||||
nestedCascadeName = samples::findFile(parser.get<string>("smile-cascade"));
|
||||
tryflip = parser.has("try-flip");
|
||||
inputName = parser.get<string>("@input");
|
||||
scale = parser.get<int>("scale");
|
||||
if (!parser.check())
|
||||
{
|
||||
help(argv);
|
||||
return 1;
|
||||
}
|
||||
if (scale < 1)
|
||||
scale = 1;
|
||||
if( !cascade.load( cascadeName ) )
|
||||
{
|
||||
cerr << "ERROR: Could not load face cascade" << endl;
|
||||
help(argv);
|
||||
return -1;
|
||||
}
|
||||
if( !nestedCascade.load( nestedCascadeName ) )
|
||||
{
|
||||
cerr << "ERROR: Could not load smile cascade" << endl;
|
||||
help(argv);
|
||||
return -1;
|
||||
}
|
||||
if( inputName.empty() || (isdigit(inputName[0]) && inputName.size() == 1) )
|
||||
{
|
||||
int c = inputName.empty() ? 0 : inputName[0] - '0' ;
|
||||
if(!capture.open(c))
|
||||
cout << "Capture from camera #" << c << " didn't work" << endl;
|
||||
}
|
||||
else if( inputName.size() )
|
||||
{
|
||||
inputName = samples::findFileOrKeep(inputName);
|
||||
if(!capture.open( inputName ))
|
||||
cout << "Could not read " << inputName << endl;
|
||||
}
|
||||
|
||||
if( capture.isOpened() )
|
||||
{
|
||||
cout << "Video capturing has been started ..." << endl;
|
||||
cout << endl << "NOTE: Smile intensity will only be valid after a first smile has been detected" << endl;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
capture >> frame;
|
||||
if( frame.empty() )
|
||||
break;
|
||||
|
||||
Mat frame1 = frame.clone();
|
||||
detectAndDraw( frame1, cascade, nestedCascade, scale, tryflip );
|
||||
|
||||
char c = (char)waitKey(10);
|
||||
if( c == 27 || c == 'q' || c == 'Q' )
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "ERROR: Could not initiate capture" << endl;
|
||||
help(argv);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
|
||||
CascadeClassifier& nestedCascade,
|
||||
double scale, bool tryflip)
|
||||
{
|
||||
vector<Rect> faces, faces2;
|
||||
const static Scalar colors[] =
|
||||
{
|
||||
Scalar(255,0,0),
|
||||
Scalar(255,128,0),
|
||||
Scalar(255,255,0),
|
||||
Scalar(0,255,0),
|
||||
Scalar(0,128,255),
|
||||
Scalar(0,255,255),
|
||||
Scalar(0,0,255),
|
||||
Scalar(255,0,255)
|
||||
};
|
||||
Mat gray, smallImg;
|
||||
|
||||
cvtColor( img, gray, COLOR_BGR2GRAY );
|
||||
|
||||
double fx = 1 / scale;
|
||||
resize( gray, smallImg, Size(), fx, fx, INTER_LINEAR_EXACT );
|
||||
equalizeHist( smallImg, smallImg );
|
||||
|
||||
cascade.detectMultiScale( smallImg, faces,
|
||||
1.1, 2, 0
|
||||
//|CASCADE_FIND_BIGGEST_OBJECT
|
||||
//|CASCADE_DO_ROUGH_SEARCH
|
||||
|CASCADE_SCALE_IMAGE,
|
||||
Size(30, 30) );
|
||||
if( tryflip )
|
||||
{
|
||||
flip(smallImg, smallImg, 1);
|
||||
cascade.detectMultiScale( smallImg, faces2,
|
||||
1.1, 2, 0
|
||||
//|CASCADE_FIND_BIGGEST_OBJECT
|
||||
//|CASCADE_DO_ROUGH_SEARCH
|
||||
|CASCADE_SCALE_IMAGE,
|
||||
Size(30, 30) );
|
||||
for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); ++r )
|
||||
{
|
||||
faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
|
||||
}
|
||||
}
|
||||
|
||||
for ( size_t i = 0; i < faces.size(); i++ )
|
||||
{
|
||||
Rect r = faces[i];
|
||||
Mat smallImgROI;
|
||||
vector<Rect> nestedObjects;
|
||||
Point center;
|
||||
Scalar color = colors[i%8];
|
||||
int radius;
|
||||
|
||||
double aspect_ratio = (double)r.width/r.height;
|
||||
if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
|
||||
{
|
||||
center.x = cvRound((r.x + r.width*0.5)*scale);
|
||||
center.y = cvRound((r.y + r.height*0.5)*scale);
|
||||
radius = cvRound((r.width + r.height)*0.25*scale);
|
||||
circle( img, center, radius, color, 3, 8, 0 );
|
||||
}
|
||||
else
|
||||
rectangle( img, Point(cvRound(r.x*scale), cvRound(r.y*scale)),
|
||||
Point(cvRound((r.x + r.width-1)*scale), cvRound((r.y + r.height-1)*scale)),
|
||||
color, 3, 8, 0);
|
||||
|
||||
const int half_height=cvRound((float)r.height/2);
|
||||
r.y=r.y + half_height;
|
||||
r.height = half_height-1;
|
||||
smallImgROI = smallImg( r );
|
||||
nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
|
||||
1.1, 0, 0
|
||||
//|CASCADE_FIND_BIGGEST_OBJECT
|
||||
//|CASCADE_DO_ROUGH_SEARCH
|
||||
//|CASCADE_DO_CANNY_PRUNING
|
||||
|CASCADE_SCALE_IMAGE,
|
||||
Size(30, 30) );
|
||||
|
||||
// The number of detected neighbors depends on image size (and also illumination, etc.). The
|
||||
// following steps use a floating minimum and maximum of neighbors. Intensity thus estimated will be
|
||||
//accurate only after a first smile has been displayed by the user.
|
||||
const int smile_neighbors = (int)nestedObjects.size();
|
||||
static int max_neighbors=-1;
|
||||
static int min_neighbors=-1;
|
||||
if (min_neighbors == -1) min_neighbors = smile_neighbors;
|
||||
max_neighbors = MAX(max_neighbors, smile_neighbors);
|
||||
|
||||
// Draw rectangle on the left side of the image reflecting smile intensity
|
||||
float intensityZeroOne = ((float)smile_neighbors - min_neighbors) / (max_neighbors - min_neighbors + 1);
|
||||
int rect_height = cvRound((float)img.rows * intensityZeroOne);
|
||||
Scalar col = Scalar((float)255 * intensityZeroOne, 0, 0);
|
||||
rectangle(img, Point(0, img.rows), Point(img.cols/10, img.rows - rect_height), col, -1);
|
||||
}
|
||||
|
||||
imshow( "result", img );
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
#include "opencv2/objdetect.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/videoio.hpp"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
/** Function Headers */
|
||||
void detectAndDisplay( Mat frame );
|
||||
|
||||
/** Global variables */
|
||||
CascadeClassifier face_cascade;
|
||||
CascadeClassifier eyes_cascade;
|
||||
|
||||
/** @function main */
|
||||
int main( int argc, const char** argv )
|
||||
{
|
||||
CommandLineParser parser(argc, argv,
|
||||
"{help h||}"
|
||||
"{face_cascade|data/haarcascades/haarcascade_frontalface_alt.xml|Path to face cascade.}"
|
||||
"{eyes_cascade|data/haarcascades/haarcascade_eye_tree_eyeglasses.xml|Path to eyes cascade.}"
|
||||
"{camera|0|Camera device number.}");
|
||||
|
||||
parser.about( "\nThis program demonstrates using the cv::CascadeClassifier class to detect objects (Face + eyes) in a video stream.\n"
|
||||
"You can use Haar or LBP features.\n\n" );
|
||||
parser.printMessage();
|
||||
|
||||
String face_cascade_name = samples::findFile( parser.get<String>("face_cascade") );
|
||||
String eyes_cascade_name = samples::findFile( parser.get<String>("eyes_cascade") );
|
||||
|
||||
//-- 1. Load the cascades
|
||||
if( !face_cascade.load( face_cascade_name ) )
|
||||
{
|
||||
cout << "--(!)Error loading face cascade\n";
|
||||
return -1;
|
||||
};
|
||||
if( !eyes_cascade.load( eyes_cascade_name ) )
|
||||
{
|
||||
cout << "--(!)Error loading eyes cascade\n";
|
||||
return -1;
|
||||
};
|
||||
|
||||
int camera_device = parser.get<int>("camera");
|
||||
VideoCapture capture;
|
||||
//-- 2. Read the video stream
|
||||
capture.open( camera_device );
|
||||
if ( ! capture.isOpened() )
|
||||
{
|
||||
cout << "--(!)Error opening video capture\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
Mat frame;
|
||||
while ( capture.read(frame) )
|
||||
{
|
||||
if( frame.empty() )
|
||||
{
|
||||
cout << "--(!) No captured frame -- Break!\n";
|
||||
break;
|
||||
}
|
||||
|
||||
//-- 3. Apply the classifier to the frame
|
||||
detectAndDisplay( frame );
|
||||
|
||||
if( waitKey(10) == 27 )
|
||||
{
|
||||
break; // escape
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** @function detectAndDisplay */
|
||||
void detectAndDisplay( Mat frame )
|
||||
{
|
||||
Mat frame_gray;
|
||||
cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
|
||||
equalizeHist( frame_gray, frame_gray );
|
||||
|
||||
//-- Detect faces
|
||||
std::vector<Rect> faces;
|
||||
face_cascade.detectMultiScale( frame_gray, faces );
|
||||
|
||||
for ( size_t i = 0; i < faces.size(); i++ )
|
||||
{
|
||||
Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 );
|
||||
ellipse( frame, center, Size( faces[i].width/2, faces[i].height/2 ), 0, 0, 360, Scalar( 255, 0, 255 ), 4 );
|
||||
|
||||
Mat faceROI = frame_gray( faces[i] );
|
||||
|
||||
//-- In each face, detect eyes
|
||||
std::vector<Rect> eyes;
|
||||
eyes_cascade.detectMultiScale( faceROI, eyes );
|
||||
|
||||
for ( size_t j = 0; j < eyes.size(); j++ )
|
||||
{
|
||||
Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y + eyes[j].y + eyes[j].height/2 );
|
||||
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
|
||||
circle( frame, eye_center, radius, Scalar( 255, 0, 0 ), 4 );
|
||||
}
|
||||
}
|
||||
|
||||
//-- Show what you got
|
||||
imshow( "Capture - Face detection", frame );
|
||||
}
|
||||
Reference in New Issue
Block a user