mirror of
https://github.com/RustyRaptor/learnopencv.git
synced 2026-12-03 22:15:16 -07:00
cleaner simplified code for landmark detection
This commit is contained in:
1 parent
17524abf4a
commit
0431181a5c
2 files changed
+84
-56
No files matched your search
@@ -0,0 +1,59 @@
|
||||
#ifndef _renderFace_H_
|
||||
#define _renderFace_H_
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
#define COLOR Scalar(255, 200,0)
|
||||
|
||||
// drawPolyLine draws a poly line by joining
|
||||
// successive points between the start and end indices.
|
||||
void drawPolyline
|
||||
(
|
||||
Mat &im,
|
||||
const vector<Point2f> &landmarks,
|
||||
const int start,
|
||||
const int end,
|
||||
bool isClosed = false
|
||||
)
|
||||
{
|
||||
// Gather all points between the start and end indices
|
||||
vector <Point> points;
|
||||
for (int i = start; i <= end; i++)
|
||||
{
|
||||
points.push_back(cv::Point(landmarks[i].x, landmarks[i].y));
|
||||
}
|
||||
// Draw polylines.
|
||||
polylines(im, points, isClosed, COLOR, 2, 16);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void drawLandmarks(Mat &im, vector<Point2f> &landmarks)
|
||||
{
|
||||
// Draw face for the 68-point model.
|
||||
if (landmarks.size() == 68)
|
||||
{
|
||||
drawPolyline(im, landmarks, 0, 16); // Jaw line
|
||||
drawPolyline(im, landmarks, 17, 21); // Left eyebrow
|
||||
drawPolyline(im, landmarks, 22, 26); // Right eyebrow
|
||||
drawPolyline(im, landmarks, 27, 30); // Nose bridge
|
||||
drawPolyline(im, landmarks, 30, 35, true); // Lower nose
|
||||
drawPolyline(im, landmarks, 36, 41, true); // Left eye
|
||||
drawPolyline(im, landmarks, 42, 47, true); // Right Eye
|
||||
drawPolyline(im, landmarks, 48, 59, true); // Outer lip
|
||||
drawPolyline(im, landmarks, 60, 67, true); // Inner lip
|
||||
}
|
||||
else
|
||||
{ // If the number of points is not 68, we do not know which
|
||||
// points correspond to which facial features. So, we draw
|
||||
// one dot per landamrk.
|
||||
for(int i = 0; i < landmarks.size(); i++)
|
||||
{
|
||||
circle(im,landmarks[i],3, COLOR, FILLED);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // _renderFace_H_
|
||||
@@ -1,97 +1,66 @@
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <opencv2/face.hpp>
|
||||
#include "renderFace.hpp"
|
||||
#include "drawLandmarks.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
using namespace cv::face;
|
||||
|
||||
|
||||
static bool detectFace(InputArray image, OutputArray faces, CascadeClassifier *cascade)
|
||||
{
|
||||
// Convert image to grayscale
|
||||
Mat imGray;
|
||||
cvtColor(image, imGray, COLOR_BGR2GRAY);
|
||||
|
||||
// Variable for storing face rectangles
|
||||
vector<Rect> facesRects;
|
||||
|
||||
// Run OpenCV HAAR based face detector
|
||||
cascade->detectMultiScale(imGray, facesRects, 1.5, 5, CASCADE_SCALE_IMAGE, Size(100, 100));
|
||||
|
||||
// Copy results to output array
|
||||
Mat(facesRects).copyTo(faces);
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc,char** argv)
|
||||
{
|
||||
// Load Face Detector
|
||||
CascadeClassifier faceDetector("haarcascade_frontalface_alt2.xml");
|
||||
|
||||
// Create an instance of FacemarkLBF.
|
||||
// The optional argument params can be used to read the parameters
|
||||
// of the trained model
|
||||
FacemarkLBF::Params params;
|
||||
Ptr<Facemark> facemark = FacemarkLBF::create(params);
|
||||
// Create an instance of Facemark
|
||||
Ptr<Facemark> facemark = FacemarkLBF::create();
|
||||
|
||||
// Set face detector
|
||||
facemark->setFaceDetector((FN_FaceDetector)detectFace, &faceDetector);
|
||||
|
||||
// Load landmark detector
|
||||
facemark->loadModel("lbfmodel.yaml");
|
||||
|
||||
|
||||
// Set up webcam for video capture
|
||||
VideoCapture cam(0);
|
||||
|
||||
// Variable to store a video frame
|
||||
Mat frame;
|
||||
|
||||
// Variable to store detected face rectangles
|
||||
vector<Rect> faces;
|
||||
// Variable to store a video frame and its grayscale
|
||||
Mat frame, gray;
|
||||
|
||||
int count = 0;
|
||||
|
||||
// Read a frame
|
||||
while(cam.read(frame))
|
||||
{
|
||||
// Run face detection every 10th frame.
|
||||
// This is done for speedup
|
||||
if (count % 10 == 0)
|
||||
{
|
||||
faces.clear();
|
||||
facemark->getFaces(frame,faces);
|
||||
count = 0;
|
||||
}
|
||||
|
||||
// Find face
|
||||
vector<Rect> faces;
|
||||
// Convert frame to grayscale because
|
||||
// faceDetector requires grayscale image.
|
||||
cvtColor(frame, gray, COLOR_BGR2GRAY);
|
||||
|
||||
// Detect faces
|
||||
faceDetector.detectMultiScale(gray, faces);
|
||||
|
||||
// Variable for landmarks.
|
||||
// Landmarks for one face is a vector of points
|
||||
// There can be more than one face. Hence, we
|
||||
// There can be more than one face in the image. Hence, we
|
||||
// use a vector of vector of points.
|
||||
vector< vector<Point2f> > landmarks;
|
||||
|
||||
|
||||
// Run landmark detector
|
||||
bool success = facemark->fit(frame,faces,landmarks);
|
||||
|
||||
if(success)
|
||||
{
|
||||
// If successful, render the landmarks on the face
|
||||
for(int i = 0; i < landmarks.size(); i++)
|
||||
{
|
||||
renderFace(frame, landmarks[i]);
|
||||
}
|
||||
// Display results
|
||||
imshow("Facial Landmark Detection", frame);
|
||||
int key = waitKey(1);
|
||||
|
||||
// Exit loop if ESC is pressed
|
||||
if (key == 27)
|
||||
{
|
||||
break;
|
||||
drawLandmarks(frame, landmarks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Display results
|
||||
imshow("Facial Landmark Detection", frame);
|
||||
// Exit loop if ESC is pressed
|
||||
if (waitKey(1) == 27) break;
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user