基于Python的OpenCV人脸识别训练与测试

2024-10-01 19:21:07 发布

您现在位置:Python中文网/ 问答频道 /正文

当从CDU中获取图像时,{att}尝试从CDU中获取图像。在

OpenCV错误:

Bad argument (Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 10304 elements, but got 92.) in predict, file /home/irum/OpenCv/modules/contrib/src/facerec.cpp, line 623 Traceback (most recent call last): File "rough.py", line 62, in prediction = model.predict(images2) cv2.error: /home/irum/OpenCv/modules/contrib/src/facerec.cpp:623: error: (-5) Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 10304 elements, but got 92. in function predict

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

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

Tags: ofpathinidforsizeoscv2

热门问题