#include<opencv2/highgui/highgui.hpp>
#include<opencv2/face.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/objdetect/objdetect.hpp>
#include<iostream>
#include<io.h>
//命名空間
using namespace std;
using namespace cv;
using namespace face;
CascadeClassifier faceDetect; //人臉檢測物件
//函式宣告
vector<Rect> FaceDetector(Mat img);//檢測人臉,并回傳人臉區域
void DrawFace(Mat img, vector<Rect> faces);//將人臉標注出來
void GetFilesName(string path, vector<string>& filesName);//獲取檔案夾里的所有檔案名稱
int main()
{
//變數
Mat frame;
Mat src;
Mat facePic;
Mat recogPic;
vector<Rect> faces;
vector<string> filesName;
string path = "..\\TrainPic";
string pic;
vector<Mat> trainPic;//訓練圖片
vector<int> labels;//與訓練圖片一一對應的標簽
int result;
map<int, string> labelsInfo;
faceDetect.load("../models/haarcascade_frontalface_alt2.xml");
Ptr<FaceRecognizer> LBPHRecog = LBPHFaceRecognizer::create(1, 8, 3, 3, 50);//構造LBPH人臉識別類的物件并初始化
//加載人臉庫
GetFilesName(path, filesName);
for (int i = 0; i < filesName.size(); i++)
{
pic = filesName[i];
src = imread(pic);
cvtColor(src, src, CV_RGB2GRAY);
trainPic.push_back(src);
labelsInfo.insert(make_pair(i, filesName[i]));
labels.push_back(i);
}
//LBPHRecog->setLabelsInfo(labelsInfo);
LBPHRecog->train(trainPic, labels); //LBP人臉識別訓練函式
VideoCapture cap(0);//打開攝像頭
int c = 0;
if (!cap.isOpened())
{
cerr << "攝像頭無法打開" << endl;
}
while (c != 27)
{
cap >> frame;
flip(frame, frame, 1);
recogPic = Mat::zeros(200, 200, frame.type());
double t = (double)getTickCount();//檢測的時間
faces = FaceDetector(frame);
t = ((double)getTickCount() - t) / getTickFrequency();
cout << t << endl;
DrawFace(frame, faces);
for (size_t i = 0; i < faces.size(); i++)
{
facePic = frame(Rect(faces[i]));
resize(facePic, recogPic, recogPic.size());
cvtColor(recogPic, recogPic, CV_RGB2GRAY);
//equalizeHist(recogPic, recogPic);
result = LBPHRecog->predict(recogPic);//進行識別
//將識別結果顯示在實時畫面上
if (result == -1)
putText(frame, "unknow", Point(faces[i].x, faces[i].y), 3, 0.5, Scalar(0, 255, 255), 1);
else
putText(frame, filesName[result], Point(faces[i].x, faces[i].y), 3, 0.5, Scalar(0, 255, 255), 1);
}
imshow("1", frame);
c = waitKey(1);
}
return 0;
}
vector<Rect> FaceDetector(Mat img)
{
Mat src = Mat::zeros(240, 320, img.type());
vector<Rect> faces;
cvtColor(img, img, CV_RGB2GRAY);
resize(img, src, src.size());
faceDetect.detectMultiScale(src, faces, 1.2, 6, 0, Size(30, 30));
for (int i = 0; i < faces.size(); i++)
{
faces[i].x = faces[i].x * 2;
faces[i].y = faces[i].y * 2;
faces[i].width = faces[i].width * 2;
faces[i].height = faces[i].height * 2;
}
return faces;
}
void DrawFace(Mat img, vector<Rect> faces)
{
for (size_t num = 0; num < faces.size(); num++)
{
rectangle(img, Point(faces[num].x, faces[num].y),
Point(faces[num].x + faces[num].width, faces[num].y + faces[num].height), Scalar(0, 255, 0), 1, 8);
}
}
void GetFilesName(string path, vector<string>& filesName)
{
//檔案句柄
long hFile = 0;
//檔案資訊
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
//如果是目錄,迭代之
//如果不是,加入串列
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
GetFilesName(p.assign(path).append("\\").append(fileinfo.name), filesName);
}
else
{
filesName.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
uj5u.com熱心網友回復:
你用的庫是C++庫,DLLs必須用編譯主程式的同樣C++編譯器編譯才行,否則會找不到符號,因為不同編譯器(甚至同樣編譯器的不同版本)使用的name mangling方法可能不同uj5u.com熱心網友回復:
我都是用vs 給他編譯的
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/22382.html
上一篇:linux
