Skip to content

Instantly share code, notes, and snippets.

@kolserdav
Created January 14, 2021 11:40
Show Gist options
  • Save kolserdav/fd801cfacec83ee284537043b65f5f9e to your computer and use it in GitHub Desktop.
Save kolserdav/fd801cfacec83ee284537043b65f5f9e to your computer and use it in GitHub Desktop.
OpenCV face recognition example
#include <vector>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/objdetect.hpp>
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
int main(int, char**)
{
CascadeClassifier bodyCascade = CascadeClassifier("/usr/local/share/opencv4/haarcascades/haarcascade_frontalface_default.xml");
Mat frame, frameGray;
VideoCapture cap;
int deviceID = 0;
int apiID = cv::CAP_ANY;
cap.open(deviceID, apiID);
if (!cap.isOpened()) {
cerr << "ERROR! Unable to open camera\n";
return -1;
}
cout << "Start grabbing" << endl
<< "Press any key to terminate" << endl;
for (;;)
{
cap.read(frame);
if (frame.empty()) {
cerr << "ERROR! blank frame grabbed\n";
break;
}
cvtColor(frame, frameGray, COLOR_BGR2GRAY);
vector<Rect> bodies;
bodyCascade.detectMultiScale(frameGray, bodies, 1.1, 3, CASCADE_FIND_BIGGEST_OBJECT, Size(30, 30), Size(200,200));
for (int i = 0; i < bodies.size(); i++) {
Rect r = bodies[i];
rectangle(frame, {r.x, r.y}, {r.x + r.width, r.y + r.height}, {0, 255, 0}, LINE_4);
}
cout << bodies.size() << endl;
imshow("Live", frame);
if (waitKey(5) >= 0) {
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment