2012-11-02 18 views
6

मैं ओपनसीवी के साथ प्रदान किए गए वीडियो नमूने में फेस रिकग्निशन का उपयोग करने की कोशिश कर रहा हूं। मैंने जो एकमात्र संशोधन किया है वह है: सीएसवी और कैस्केड क्लासिफायर पथ प्रदान करने के लिए कमांड लाइन तर्कों का उपयोग करने के बजाय, मैंने उन्हें सीधे कोड में दिया है।ओपनसीवी का उपयोग कर वीडियो में फेस पहचान अनचाहे अपवाद

C:\\Training\\extract0.jpg;0 
C:\\Training\\extract1.jpg;0 
C:\\Training\\extract2.jpg;0 

हालांकि जब मैं नमूना (यह ठीक बनाता है) चलाने का प्रयास है, मैं इन त्रुटियों को मिलता है और यह पूछता है:

#include "stdafx.h" 
#include "opencv2/core/core.hpp" 
#include "opencv2/contrib/contrib.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/objdetect/objdetect.hpp" 

#include <iostream> 
#include <fstream> 
#include <sstream> 

using namespace cv; 
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 = "C:\\OpenCV-2.4.2\\opencv\\data\\haarcascades\\haarcascade_frontalface_default.xml"; 
string fn_csv = "C:\\Users\\gaspl\\Desktop\\train.txt"; 
int deviceId = 1; 
// 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; 
} 

इस तरह क्या मेरी train.txt CSV फ़ाइल लग रहा है: यह कोड है फिशर पहचानकर्ता

First-chance exception at 0x000007FEFE04CAED in facrec.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000000025C530. 
Unhandled exception at at 0x000007FEFE04CAED in facrec.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000000025C530. 

बिंदु मैं कहाँ प्रारंभ में त्रुटि होती है:: मुझे Break को

Ptr<FaceRecognizer> model = createFisherFaceRecognizer(); 
model->train(images, labels); 

मैं विंडोज 7 का उपयोग कर रहा विजुअल स्टूडियो 2012 और OpenCV 2.4.2 के साथ 64-बिट (विन्यास बिल्ड: 64-रिलीज)

मैं पहले से ही चेहरे का पता लगाने और चेहरे निकासी की कोशिश की है और वे अपने कंप्यूटर पर ठीक से काम (If anyone wants to check out the code), इसलिए मेरे विजुअल स्टूडियो प्रोजेक्ट सेटिंग्स (लिंकर या सी/सी ++) के साथ स्पष्ट रूप से कोई समस्या नहीं है।

There is a similar question here लेकिन यह अभी भी कुछ भी हल नहीं करता है।

मैं जो कर रहा हूं उसके साथ कुछ भी गलत है?

+0

अपवाद आमतौर पर गलत कहता है। आपको इसे संभालना चाहिए और इसके संदेश को देखना चाहिए। –

+1

@AndreyKamaev मैंने इसे डीबग करने का प्रयास किया है, और ऐसा लगता है कि समस्या ~ images.push_back (imread (path, 0)) कोड में इस पंक्ति के साथ है, 'read_csv()' funtion के भीतर। यह 'छवियों' सरणी में छवि डेटा नहीं जोड़ता है, यही कारण है कि फिशर प्रशिक्षण और पूर्वानुमान कार्य एक अपवाद देते हैं। क्या 'imread() 'कार्यान्वयन या CSV फ़ाइल में पथ प्रारूप के साथ कुछ गड़बड़ है? –

उत्तर

11

जैसा कि हम पहले ही मेल द्वारा निष्कर्ष निकाला है। यह इसलिए होता है क्योंकि केवल एक लेबल अपनी CSV फ़ाइल में दिया जाता है:

 
C:\Training\extract0.jpg;0 
C:\Training\extract1.jpg;0 
C:\Training\extract2.jpg;0 

Fisherfaces विधि में कम से कम दो वर्गों की जरूरत है एक मॉडल को जानने के लिए। इस मामले में पकड़ा जाना चाहिए किया गया है और OpenCV 2.4.2 अपने सिस्टम पर निम्नलिखित अपवाद फेंकता है:

 
OpenCV Error: Bad argument (At least two classes are needed to perform a LDA. Reason: Only one class was given!) in lda, file /home/philipp/github/libfacerec/src/subspace.cpp, line 150 
terminate called after throwing an instance of 'cv::Exception' 
    what(): /home/philipp/github/libfacerec/src/subspace.cpp:150: error: (-5) At least two classes are needed to perform a LDA. Reason: Only one class was given! in function lda 

कौन सा प्रशिक्षण डेटा काफी स्पष्ट में त्रुटि बनाता है। मुझे नहीं पता, यह अपवाद आपके विंडोज 7 इंस्टॉलेशन पर क्यों नहीं फेंक दिया गया है, लेकिन मैं जितनी जल्दी हो सके पुन: उत्पन्न करने के लिए एक परीक्षण प्रणाली स्थापित करूँगा और तदनुसार ठीक करूँगा।

+0

कोई विकल्प जब मेरे पास केवल एक लेबल है? – lzt

संबंधित मुद्दे