- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用 C++ 中的 EigenFaceRecognizer 识别正面。
问题是:
1) 在高阈值时,两个人被识别为同一个人,并且还识别出"new"面孔,而不是将其声明为新面孔
2)在低阈值下,将训练集中已有的人脸识别为新人脸
3) 也会遇到误报。虽然不是问题,但如果建议一种简单的方法来减少它们,我们将不胜感激>
有什么方法可以改进识别器以准确识别人脸吗?
以下是我正在做的。
#include<opencv2\opencv.hpp> //For opencv functions
#include<opencv2\highgui\highgui.hpp> //For window based functions
#include<fstream> //For dealing with I/O operations on file
using namespace std;
using namespace cv;
// Function to read the File containing paths and labels of the training images and push them into images and labels vector
static void read_data(vector <Mat> & images,vector <int>& labels, char separator=' ')
{
ifstream file("images.txt"); //images.txt contains paths and labels separated by a space
string line;
string a[2];
while(getline(file,line)) // read images.txt line by line
{
int i=0;
stringstream iss(line);
while (iss.good() && i < 2)
{
iss>>a[i];
++i;
}
images.push_back(imread(a[0],CV_LOAD_IMAGE_GRAYSCALE)); // a[0] = "path of images"
labels.push_back(atoi(a[1].c_str())); //a[1] = "labels"
}
file.close();
}
// Function to take input from webcam and recognize faces
int face_recognition::face_rec(int time_flag, int trigger_flag)
{
vector<Mat> images; //stores the paths of all images
vector<int> labels; //stores the corresponding labels
//function call to function read_data
read_data(images,labels);
//take the size of the sample images
int im_width = images[0].cols;
int im_height = images[0].rows;
//threshold is the minimum value of magnitude of vector of EigenFaces
double threshold=10.0;
//create instance of EigenFaceRecognizer
Ptr<FaceRecognizer> model = createEigenFaceRecognizer(10,threshold);
double current_threshold =model->getDouble("threshold");
// set a threshold value, for face prediction
model->set("threshold",5000.0);
// train the face recognizer using the sample images
model->train(images,labels);
// Create face_cascade to detect people
CascadeClassifier face_cascade;
if(!face_cascade.load("haarcascade_frontalface_default.xml")) // load haarcascade_frontaface_default.xml
{
cout<<"ERROR Loading cascade file";
return 1;
}
// capture the video input from webcam
VideoCapture capture(CV_CAP_ANY);
capture.set(CV_CAP_PROP_FRAME_WIDTH, 320);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
Size frameSize(static_cast<int>(320), static_cast<int>(240));
//initialize the VideoWriter object
VideoWriter oVideoWriter ("MyVideo.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); // video is save in the VS project
if(!capture.isOpened())
{
cout<<"Error in camera";
return 1;
}
Mat cap_img, gray_img;
//store the detected faces
vector<Rect> faces;
while(1)
{
//capture frame by frame in cap_img
capture>>cap_img;
waitKey(10);
// Image conversion: Color to Gray
cvtColor(cap_img,gray_img,CV_BGR2GRAY);
//Histogram Equilization to increase contrast by stretching intensity ranges
equalizeHist(gray_img,gray_img);
// detects faces in the frame
//CV_HAAR_SCALE_IMAGE to scale the size of the detect face
//CV_HAAR_DO_CANNY_PRUNING to increase speed as it skips image regions that are unlikely to contain a face
face_cascade.detectMultiScale(gray_img,faces,1.1,10,CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING, Size(20,20),Size(300,300));
Mat Normalized;
//Loop over the detected faces
for(int i=0;i<faces.size();i++)
{
Rect face_i = faces[i];
Mat face = gray_img(face_i);
Mat face_resized;
//resize the detected face to the size of sample images
resize(face,face_resized, Size(im_width,im_height),1.0,1.0,INTER_CUBIC);
// predict the person the face belongs to, returns label
int predicted_label = -1;
predicted_label=model->predict(face_resized);
// Draws a rectangle around the faces
rectangle(cap_img,face_i, CV_RGB(0,255,0),1);
//text to be put with the face, by default "new" for new faces
string box_text=format("new");
// Change the text based on label
if(predicted_label>-1)
switch(predicted_label)
{
case 0:box_text = format("keanu");
break;
case 1:box_text = format("selena");
break;
case 2:box_text = format("shubham");
break;
}
// calculate the coordinates to put the text based on the postion of the face
int pos_x = max(face_i.tl().x - 10, 0);
int pos_y = max(face_i.tl().y - 10, 0);
// put text on the output screen
putText(cap_img, box_text , Point(pos_x,pos_y), FONT_HERSHEY_PLAIN,0.8, CV_RGB(0,255,0), 1,CV_AA);
if (box_text=="new")
{
oVideoWriter.write(cap_img); //writer the frame into the file
}
}
// show the frame on the result window
imshow("Press Esc to exit",cap_img);
if(waitKey(30)==27)
break;
}
返回0;
最佳答案
我遇到了同样的问题。答案 ( and code ) 在 Mastering OpenCV 的第 8 章中很棒的 Shervin Emami 的书。 Here's his blog post关于这个问题。
主要是对人脸做一些预处理,包括以下几个步骤
干杯。
关于c++ - 如何提高 EigenFaceRecognizer 的准确率,将两个人识别为一个人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31191177/
我训练了 LSTM 分类模型,但得到了奇怪的结果(0 准确率)。这是我的带有预处理步骤的数据集: import pandas as pd from sklearn.model_selection im
使用 TFlearn 构建 DNN 后,我想计算网络的准确性。 这是代码: def create_model(self): x = tf.placeholder(dtype= tf.float
Duplicate calculating Precision, Recall and F Score 我有一个带有文本描述和分类级别(即levelA和levelB)的输入文件。我想编写一个 SVM
如何计算语义分割中前 k 个准确率?在分类中,我们可以将 topk 准确率计算为: correct = output.eq(gt.view(1, -1).expand_as(output)) 最佳答案
我正在尝试解决多标签分类问题 from sklearn.preprocessing import MultiLabelBinarizer traindf = pickl
我是一名优秀的程序员,十分优秀!