如何从视频中识别人脸识别中的未知人?
Posted
技术标签:
【中文标题】如何从视频中识别人脸识别中的未知人?【英文标题】:How to identify unknown persons in facerecognition from videos? 【发布时间】:2017-08-28 11:28:29 【问题描述】:我正在使用 Philipp Wagner 的视频中的面部识别,我更新了代码以使用 opencv 3.2,之后我很难创建合适的面部数据库,但我的问题是我该如何给出未知人的值?到目前为止,当我运行我的代码时,它会从我的数据库中为未知人提供一个值,我为自己使用“0”,为另一个人使用“1”。例如,对于未知主题,我如何将其设置为“-1”?到目前为止,这是我的代码,我尝试使用阈值但没有得到任何结果。
#include "opencv2/core.hpp"
#include "opencv2/face.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/objdetect.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace cv;
using namespace cv::face;
using namespace std;
static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';')
std::ifstream file(filename.c_str(), ifstream::in);
if (!file)
string error_message = "No valid input file was given, please check the given filename.";
CV_Error(CV_StsBadArg, error_message);
string line, path, classlabel;
while (getline(file, line))
stringstream liness(line);
getline(liness, path, separator);
getline(liness, classlabel);
if(!path.empty() && !classlabel.empty())
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
int main(int argc, const char *argv[])
// Check for valid command line arguments, print usage
// if no arguments were given.
if (argc != 4)
cout << "usage: " << argv[0] << " </path/to/haar_cascade> </path/to/csv.ext> </path/to/device id>" << endl;
cout << "\t </path/to/haar_cascade> -- Path to the Haar Cascade for face detection." << endl;
cout << "\t </path/to/csv.ext> -- Path to the CSV file with the face database." << endl;
cout << "\t <device id> -- The webcam device id to grab frames from." << endl;
exit(1);
// Get the path to your CSV:
string fn_haar = string(argv[1]);
string fn_csv = string(argv[2]);
int deviceId = atoi(argv[3]);
// These vectors hold the images and corresponding labels:
vector<Mat> images;
vector<int> labels;
// Read in the data (fails if no valid input filename is given, but you'll get an error message):
try
read_csv(fn_csv, images, labels);
catch (cv::Exception& e)
cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
// nothing more we can do
exit(1);
// Get the height from the first image. We'll need this
// later in code to reshape the images to their original
// size AND we need to reshape incoming faces to this size:
int im_width = images[0].cols;
int im_height = images[0].rows;
// Create a FaceRecognizer and train it on the given images:
Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
model->train(images, labels);
// That's it for learning the Face Recognition model. You now
// need to create the classifier for the task of Face Detection.
// We are going to use the haar cascade you have specified in the
// command line arguments:
//
CascadeClassifier haar_cascade;
haar_cascade.load(fn_haar);
// Get a handle to the Video device:
VideoCapture cap(deviceId);
// Check if we can use this device at all:
if(!cap.isOpened())
cerr << "Capture Device ID " << deviceId << "cannot be opened." << endl;
return -1;
// Holds the current frame from the Video device:
Mat frame;
for(;;)
cap >> frame;
// Clone the current frame:
Mat original = frame.clone();
// Convert the current frame to grayscale:
Mat gray;
cvtColor(original, gray, CV_BGR2GRAY);
// Find the faces in the frame:
vector< Rect_<int> > faces;
haar_cascade.detectMultiScale(gray, faces);
// At this point you have the position of the faces in
// faces. Now we'll get the faces, make a prediction and
// annotate it in the video. Cool or what?
for(int i = 0; i < faces.size(); i++)
// Process face by face:
Rect face_i = faces[i];
// Crop the face from the image. So simple with OpenCV C++:
Mat face = gray(face_i);
// Resizing the face is necessary for Eigenfaces and Fisherfaces. You can easily
// verify this, by reading through the face recognition tutorial coming with OpenCV.
// Resizing IS NOT NEEDED for Local Binary Patterns Histograms, so preparing the
// input data really depends on the algorithm used.
//
// I strongly encourage you to play around with the algorithms. See which work best
// in your scenario, LBPH should always be a contender for robust face recognition.
//
// Since I am showing the Fisherfaces algorithm here, I also show how to resize the
// face you have just found:
Mat face_resized;
cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
// Now perform the prediction, see how easy that is:
int prediction = model->predict(face_resized);
// And finally write all we've found out to the original image!
// First of all draw a green rectangle around the detected face:
rectangle(original, face_i, CV_RGB(0, 255,0), 1);
// Create the text we will annotate the box with:
string box_text = format("Prediction = %d", prediction);
// Calculate the position for annotated text (make sure we don't
// put illegal values in there):
int pos_x = std::max(face_i.tl().x - 10, 0);
int pos_y = std::max(face_i.tl().y - 10, 0);
// And now put it into the image:
putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
// Show the result:
imshow("face_recognizer", original);
// And display it:
char key = (char) waitKey(20);
// Exit this loop on escape:
if(key == 27)
break;
return 0;
【问题讨论】:
【参考方案1】:阅读此文档:Fisher Face Recognizer。阅读您使用的每种方法。这应该会为您提供解决问题所需的信息。
来自model->set
上的文档:如果到最近邻的距离大于阈值,则该方法返回-1
。在您的情况下,您没有收到任何 -1
的返回,这意味着您的阈值可能设置为高,这将允许不相似的面孔返回正匹配。
您似乎尚未设置阈值变量。尝试使用:model->set("threshold", DOUBLE_VALUE_HERE);
将阈值设置为较低的值。
0.0
的阈值几乎总是会返回 -1
,因为图像在距离 > 0.0
时总是会有细微差别。尝试不同的阈值,看看是否能得到你想要的结果。我建议从 5.0
: model->set("threshold", 5.0);
的值开始,然后从那里向上或向下工作。
【讨论】:
谢谢你,你是一个救生员,我用你的方法测试,但是当我给阈值一个小值时(这意味着如果我得到一张未知的脸,我想得到返回的值)我得到值“0”而不是“-1”假设我的面部数据库中没有“0”标签,这是否意味着它有效? 我的建议是让程序在没有命令行参数的情况下运行,然后从那里扩展。例如:首先手动为标签 0 加载同一人的多张图像,为标签 1 加载不同人的多张图像,然后与第三张静止图像进行比较。一旦你能得到它的工作,它会更容易扩展 我整理了这个示例程序,它可以在没有网络摄像头的情况下完成您想要做的事情。 My github link。它要简单得多,但它显示了阈值的重要性。 当我实际将一个人的标签设置为 0 并将另一个标签设置为 1 时,将阈值设置为 0.0 意味着它不会识别任何人,因此它会给出它不识别的 -1 值,我'm using : Ptr以上是关于如何从视频中识别人脸识别中的未知人?的主要内容,如果未能解决你的问题,请参考以下文章
opencv人脸识别用哪种方法比较好?Eigenfaces?Fisherfaces?LBP?