2012-11-01 61 views
1

OpenCV Error: Bad argument (Empty training data was given. You'll need more than one sample to learn a model.) in train, file C:/slave/WinInstallerMegaPack/src/opencv/modules/contrib/src/facerec.cpp, line 316列車錯誤和預測我的opencv程序

當我嘗試運行下面的程序時出現上述錯誤。我的意圖是提供一些輸入圖像,並檢查opencv的train()predict()函數。下面的代碼段中有什麼不對嗎?

int main(int argc, char* argv[]) { 

    const char * trainImage[6] = { "c:\\train\\00000000.jpg", "c:\\train\\00000001.jpg", "c:\\train\\00000002.jpg", "c:\\train\\00000003.jpg", "c:\\train\\00000004.jpg", "c:\\train\\00000005.jpg" }; 

    int imglabel[6] = {1, 2, 3, 4, 5, 6};   
    vector<Mat> img = vector<Mat>(); 
    vector<int> label = vector<int>(); 

    for (int i = 0; i >= 5; i++) { 
     img.push_back(imread(trainImage[i], 0)); 
     label.push_back(imglabel[i]); 
    } 

    Ptr<FaceRecognizer> model = createEigenFaceRecognizer(); 
    model->train(img, label); 
    model->save("c:\\train\\file.yaml"); 

    // Testing... 
    model->load("c:\\train\\file.yaml"); 
    Mat testsample = imread("c:\\test\\00000000.jpg", -1); 

    int PredictLabel = -1; 
    double confidence = 0.0; 

    model->predict(testsample, PredictLabel, confidence); 
    cout << PredictLabel; 
    return 0; 

} 

回答

2

爲每個標籤提供更多的培訓樣本。

documentation

見代碼示例:

// images for first person 
images.push_back(imread("person0/0.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0); 
images.push_back(imread("person0/1.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0); 
images.push_back(imread("person0/2.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0); 
// images for second person 
images.push_back(imread("person1/0.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1); 
images.push_back(imread("person1/1.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1); 
images.push_back(imread("person1/2.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1); 

而且你for循環條件是錯誤的:

for (int i = 0; i <= 5; i++) 

和測試圖像應加載灰度:

Mat testsample = imread("test.jpg", CV_LOAD_IMAGE_GRAYSCALE); 
+0

感謝您的答覆。我照你說的現在我得到錯誤OpenCV Error:Unspecified error(File無法打開寫入!)在加載,文件C:/slave/WinInstallerMegaPack/src/opencv/modules/contrib/src/facerec.cpp,304行 – 2vision2

+0

也許你無法訪問該文件夾,嘗試更改路徑到'「file.yml」' – Cfr

+0

我再次遇到同樣的錯誤! – 2vision2