您的位置:首页 > 运维架构

基于Opencv的视频人脸检测

2017-02-12 09:12 405 查看
#include <opencv2\opencv.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv\highgui.h>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\imgproc\types_c.h>
#include <opencv2\objdetect\objdetect_c.h>
#include <iostream>
using namespace std;
using namespace cv;

int video()
{
string xmlPath = "C:\\opencv3.0\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml";
CascadeClassifier ccf;
ccf.load(xmlPath);
vector<Rect> faces;
Mat img, gray;

namedWindow("myVideoPlayer");
VideoCapture cap;
cap.open("sample.mp4");

VideoWriter writer;
writer.open("output.avi", CV_FOURCC_DEFAULT, 30, Size(gray.cols, gray.rows));

while (1)
{
cap >> img;
if (img.empty())break;
cvtColor(img, gray, CV_BGR2GRAY);
equalizeHist(gray, gray);
ccf.detectMultiScale(gray, faces, 1.1, 3, 0, Size(10, 10), Size(100, 100));
for (vector<Rect>::const_iterator iter = faces.begin(); iter != faces.end(); iter++)
{
rectangle(img, *iter, Scalar(0, 0, 255), 2, 8);
}
imshow("myVideoPlayer", img);

writer << img;
waitKey(10);
}
return 1;
}
int picture()
{
string xmlPath = "C:\\opencv3.0\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml";
CascadeClassifier ccf;
ccf.load(xmlPath);
vector<Rect> faces;
Mat gray;
Mat img = imread("sample.jpg");
cvtColor(img, gray, CV_BGR2GRAY);
equalizeHist(gray, gray);
ccf.detectMultiScale(gray, faces, 1.1, 3, 0, Size(10, 10), Size(100, 100));
for (vector<Rect>::const_iterator iter = faces.begin(); iter != faces.end(); iter++)
{
rectangle(img, *iter, Scalar(0, 0, 255), 2, 8);
}
imshow("result", img);
imwrite("outputqjf.jpg",img);
//waitKey();
return 1;
}

int main()
{
//检测视频
//video();
//检测图片
picture();
return 1;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息