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

在尝试使用OpenCV进行人脸识别训练时遇到错误:'cv2.error: Bad argument (Wrong input image size...)。尽管训练和测试图像的高度和宽度相同,但依然报错。代码涉及读取图像、创建FisherFace Recognizer、训练和测试数据的准备。问题可能出在图像处理或数据加载阶段。
摘要由CSDN通过智能技术生成

当从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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值