OpenCV培训和测试人脸识别与Python错误

时间:2016-10-23 21:41:34

标签: python opencv face-recognition

当我尝试使用att_faces数据库中的图像识别来自unknowns/UNKNOWNS的图像时出现此错误。

OpenCV错误:

  

错误的参数(错误的输入图像大小。原因:训练和测试图像必须大小相同!预期图像包含10304个元素,但得到92.)in predict,file / home / irum / OpenCv / modules / contrib / src / facerec.cpp,第623行       Traceback(最近一次调用最后一次):         文件" rough.py",第62行,in           预测= model.predict(images2)       cv2.error:/home/irum/OpenCv/modules/contrib/src/facerec.cpp:623:错误:( - 5)输入图像大小错误。原因:训练和测试图像必须大小相同!预期有10304个元素的图像,但在函数预测中得到92.

但两个文件夹中的图像与高度相同。它们实际上是使用Haar Cascades在不同python脚本中的裁剪面部相同的图像,但现在我使用两个文件夹进行识别,它给了我错误。我只是不明白为什么? 下面是我正在使用的代码。

import cv2, sys, numpy, os
import json
size = 4
fn_dir2 = 'unknown'
fn_haar = 'haarcascade_frontalface_default.xml'
fn_dir = 'att_faces'
path2='/home/irum/Desktop/Face-Recognition/thakarrecog/UNKNOWNS'
path='/home/irum/Desktop/Face-Recognition/thakarrecog/att_faces'

# Prepare Train Set
print('Training...')

(images, lables, names, id) = ([], [], {}, 0)

for (subdirs, dirs, files) in os.walk(fn_dir):
    for subdir in dirs:
        names[id] = subdir 
        subjectpath = os.path.join(fn_dir, subdir) 
        for filename in os.listdir(subjectpath):
            path = subjectpath + '/' + filename
            lable = id
            images.append(cv2.imread(path, 0))
            lables.append(int(lable))
        id += 1

(images, lables) = [numpy.array(lis) for lis in [images, lables]]

# Create FisherFace Recognizer
model = cv2.createFisherFaceRecognizer()

# Load TrainSet
model.train(images, lables)

# Prepare Test Data
# Create a list of images and a list of corresponding names

(images2, lables2, names2, id) = ([], [], {}, 0)


for (subdirs, dirs, files) in os.walk(fn_dir2):
    for subdir in dirs:
        names2[id] = subdir 
        subjectpath = os.path.join(fn_dir2, subdir)  
        for filename in os.listdir(subjectpath):

            path = subjectpath + '/' + filename
            lable = id
            images2.append(cv2.imread(path, 0))
            lables2.append(int(lable))

            # Convert images2 to numpy
            images2 = numpy.array(images2)

            # Try to recognize/predict the face
            prediction  = model.predict(images2)

            print "Recognition Prediction" ,prediction
            result = {
                'face': {

                 'distance': prediction,
                 'coords': {
                   'x': str(faces[0][0]),
                   'y': str(faces[0][1]),
                   'width': str(faces[0][2]),
                   'height': str(faces[0][3])
                    }
                }
              }
            print "1 Result of Over all Prediction" ,result
        id += 1

0 个答案:

没有答案