62 lines
1.7 KiB
C++
62 lines
1.7 KiB
C++
|
#include <opencv2/opencv.hpp>
|
||
|
#include <opencv2/face.hpp>
|
||
|
|
||
|
#include <graphics.hpp>
|
||
|
|
||
|
int main () {
|
||
|
initGraphics();
|
||
|
|
||
|
cv::CascadeClassifier faceDetector ("haarcascade_frontalface_alt2.xml");
|
||
|
|
||
|
cv::Ptr<cv::face::Facemark> facemark = cv::face::FacemarkLBF::create();
|
||
|
facemark->loadModel ("lbfmodel.yaml");
|
||
|
|
||
|
cv::VideoCapture vid (0);
|
||
|
|
||
|
cv::Mat frame, gray, small;
|
||
|
|
||
|
while (vid.read(frame)) {
|
||
|
cv::cvtColor (frame, gray, cv::COLOR_BGR2GRAY);
|
||
|
//downsample image for face detection, works too slow on full res
|
||
|
cv::pyrDown (gray, small);
|
||
|
cv::pyrDown (small, small);
|
||
|
|
||
|
std::vector<cv::Rect> faces;
|
||
|
faceDetector.detectMultiScale(small, faces);
|
||
|
|
||
|
//get biggest face
|
||
|
int biggestFace = 0;
|
||
|
int biggestArea = 0;
|
||
|
for (int i = 0; i < faces.size(); i++) {
|
||
|
//convert face region to full res, because we perform facemark on full res
|
||
|
faces[i] = cv::Rect (faces[i].x * 4, faces[i].y * 4, faces[i].width * 4, faces[i].height * 4);
|
||
|
|
||
|
int iArea = faces[i].area();
|
||
|
if (iArea > biggestArea) {
|
||
|
biggestFace = i;
|
||
|
biggestArea = iArea;
|
||
|
}
|
||
|
|
||
|
cv::rectangle (frame, faces[i], cv::Scalar (255, 255, 0));
|
||
|
}
|
||
|
|
||
|
std::vector<std::vector<cv::Point2f>> landmarks;
|
||
|
|
||
|
if (facemark->fit (frame, faces, landmarks)) {
|
||
|
for (int i = 0; i < landmarks[biggestFace].size(); i++) {
|
||
|
cv::circle (frame, landmarks[biggestFace][i], 2, cv::Scalar (255, 255, 255));
|
||
|
}
|
||
|
//send control information to graphics
|
||
|
updateModel(glm::vec2(
|
||
|
((faces[biggestFace].x + (faces[biggestFace].width / 2)) * 2) / (float)frame.cols - 1,
|
||
|
((faces[biggestFace].y + (faces[biggestFace].height / 2)) * 2) / -(float)frame.rows + 1
|
||
|
));
|
||
|
}
|
||
|
|
||
|
graphicsFrame ();
|
||
|
|
||
|
cv::imshow ("Video Input", frame);
|
||
|
cv::waitKey (33);
|
||
|
}
|
||
|
}
|