问题遇到的现象和发生背景
刚刚学习openCV,打算调用笔记本电脑上的摄像头来简单实现人脸识别,输出身份。但在进行识别时报错,一时无法解决,前来请教。传入识别器的图片和训练用图片都一样是640*480大小,3通道。将图像转化为灰度图像后程序可以运行,但彩色该怎么办?
问题相关代码,请勿粘贴截图
import cv2
import numpy as np
from cv2 import VideoCapture
photos=list()
lables=list()
photos.append(cv2.imread("a1.jpg"))
photos.append(cv2.imread("a2.jpg"))
photos.append(cv2.imread("a3.jpg"))
lables.append(0)
lables.append(0)
lables.append(0)
photos.append(cv2.imread("lb1.jpg"))
photos.append(cv2.imread("b2.jpg"))
photos.append(cv2.imread("b3.jpg"))
lables.append(1)
lables.append(1)
lables.append(1)
names={"0":"a","1":"b"}
recognizer=cv2.face.EigenFaceRecognizer_create()
recognizer.train(photos,np.array(lables))
capture=VideoCapture(0)
while capture.isOpened():
retval,image=capture.read()
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
faces = faceCascade.detectMultiScale(image, 1.15)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 5)
#image=cv2.putText(image,names[str(lable)],(x,y),cv2.FONT_HERSHEY_DUPLEX,2,(0,255,0),5)
cv2.imshow("Video",image)
key=cv2.waitKey(1)
if key==32:
lable, confidence = recognizer.predict(image)
print(names[str(lable)])
if key==27:
print(image.shape)
break
capture.release()
cv2.destroyAllWindows()
运行结果及报错内容
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\lda.cpp:183: error: (-5:Bad argument) Wrong shapes for given matrices. Was size(src) = (1,921600), size(W) = (307200,6). in function 'cv::LDA::subspaceProject'