OpenCV  4.5.1
Open Source Computer Vision
samples/cpp/peopledetect.cpp
// 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 <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)
{
}
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;
}
if (file.empty())
cap.open(camera);
else
{
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;
}
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;
}
cv::Point_< int >
cv::HOGDescriptor::detectMultiScale
virtual void detectMultiScale(InputArray img, std::vector< Rect > &foundLocations, std::vector< double > &foundWeights, double hitThreshold=0, Size winStride=Size(), Size padding=Size(), double scale=1.05, double finalThreshold=2.0, bool useMeanshiftGrouping=false) const
Detects objects of different sizes in the input image. The detected objects are returned as a list of...
cv::VideoCapture
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:628
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
cv::HOGDescriptor::getDaimlerPeopleDetector
static std::vector< float > getDaimlerPeopleDetector()
Returns coefficients of the classifier trained for people detection (for 48x96 windows).
cv::HOGDescriptor
Implementation of HOG (Histogram of Oriented Gradients) descriptor and object detector.
Definition: objdetect.hpp:373
highgui.hpp
int64
int64_t int64
Definition: interface.h:61
cv::Scalar_< double >
cv::Size
Size2i Size
Definition: types.hpp:347
cv::Rect_::tl
Point_< _Tp > tl() const
the top-left corner
cv::rectangle
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple, thick, or filled up-right rectangle.
cv::FONT_HERSHEY_PLAIN
@ FONT_HERSHEY_PLAIN
small size sans-serif font
Definition: imgproc.hpp:819
cv::HOGDescriptor::getDefaultPeopleDetector
static std::vector< float > getDefaultPeopleDetector()
Returns coefficients of the classifier trained for people detection (for 64x128 windows).
cv::Rect_::y
_Tp y
y coordinate of the top-left corner
Definition: types.hpp:454
cv::getTickCount
int64 getTickCount()
Returns the number of ticks.
cv::Rect_::br
Point_< _Tp > br() const
the bottom-right corner
cv::VideoCapture::isOpened
virtual bool isOpened() const
Returns true if video capturing has been initialized already.
cvRound
int cvRound(double value)
Rounds floating-point number to the nearest integer.
Definition: fast_math.hpp:197
cv::Rect_
Template class for 2D rectangles.
Definition: types.hpp:421
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::Rect_::width
_Tp width
width of the rectangle
Definition: types.hpp:455
cv::getTickFrequency
double getTickFrequency()
Returns the number of ticks per second.
cv::Scalar
Scalar_< double > Scalar
Definition: types.hpp:669
cv::Rect_::height
_Tp height
height of the rectangle
Definition: types.hpp:456
cv::putText
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
Draws a text string.
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:798
cv::samples::findFileOrKeep
cv::String findFileOrKeep(const cv::String &relative_path, bool silentMode=false)
Definition: utility.hpp:1156
cv::CommandLineParser
Designed for command line parsing.
Definition: utility.hpp:789
cv
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:52
imgproc.hpp
cv::_InputArray
This is the proxy class for passing read-only input arrays into OpenCV functions.
Definition: mat.hpp:159
objdetect.hpp
videoio.hpp
cv::Rect_::x
_Tp x
x coordinate of the top-left corner
Definition: types.hpp:453
cv::HOGDescriptor::setSVMDetector
virtual void setSVMDetector(InputArray svmdetector)
Sets coefficients for the linear SVM classifier.
cv::LINE_AA
@ LINE_AA
antialiased line
Definition: imgproc.hpp:811
cv::VideoCapture::open
virtual bool open(const String &filename, int apiPreference=CAP_ANY)
Opens a video file or a capturing device or an IP video stream for video capturing.