2017-04-03 138 views
0

我正在使用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; 
} 

回答

0

閱讀本文件: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

謝謝你本人是生命的保護者,我用你的方法睾丸,但當我給門檻一個小值(意思是我想得到的價值返回,如果我得到一個未知的臉)我得到值「0」不是「-1」假設我的臉部數據庫中沒有「0」標籤,這是否意味着它工作? – deepmore

+0

我的建議是讓程序在沒有命令行參數的情況下工作,然後從那裏展開。例如:首先手動加載標籤0的同一人的多個圖像,標籤1的另一個人的多個圖像,然後與第三個靜止圖像比較。一旦你可以得到這個工作,它將更容易擴展 –

+0

我把這個示例程序放在一起,這個程序是在沒有網絡攝像機的情況下做你想做的。 [我的github鏈接](https://github.com/benkrig/OpenCV/blob/master/facial_recognition_fisher.cpp)。這簡單得多,但它顯示了閾值的重要性。 –