import cv2 import numpy as np img1=cv2.imread('110.jpg',0),(200,200) img2=cv2.imread('111.jpg',0),(200,200) img3=cv2.imread('112.jpg',0),(200,200) img4=cv2.imread('210.jpg',0),(200,200) img5=cv2.imread('211.jpg',0),(200,200) img6=cv2.imread('212.jpg',0),(200,200) img7=cv2.imread('310.jpg',0),(200,200) img8=cv2.imread('311.jpg',0),(200,200) img9=cv2.imread('312.jpg',0),(200,200) train_images=[img1,img2,img3,img4,img5,img6,img7,img8,img9] labels=np.array([0,0,1,1]) recognizer=cv2.face.EigenFaceRecognizer_create() recognizer.train(train_images,labels) testimg=cv2.imread('paizhao.jpg',0) label,confidence=recognizer.predict(testimg) print('匹配标签:',label) print('可信程度:',confidence)
时间: 2023-12-01 07:02:09 浏览: 91
使用python-cv2实现Harr+Adaboost人脸识别的示例
这段代码存在一些问题,首先应该将第一行代码中的两个import语句分开写,使用换行符或分号分隔。其次,对于每个图像,应该使用cv2.resize()函数将其大小调整为(200,200)。最后,labels应该包含与train_images中的图像数量相同的元素。
下面是修改后的代码:
```
import cv2
import numpy as np
img1 = cv2.imread('110.jpg', 0)
img1 = cv2.resize(img1, (200, 200))
img2 = cv2.imread('111.jpg', 0)
img2 = cv2.resize(img2, (200, 200))
img3 = cv2.imread('112.jpg', 0)
img3 = cv2.resize(img3, (200, 200))
img4 = cv2.imread('210.jpg', 0)
img4 = cv2.resize(img4, (200, 200))
img5 = cv2.imread('211.jpg', 0)
img5 = cv2.resize(img5, (200, 200))
img6 = cv2.imread('212.jpg', 0)
img6 = cv2.resize(img6, (200, 200))
img7 = cv2.imread('310.jpg', 0)
img7 = cv2.resize(img7, (200, 200))
img8 = cv2.imread('311.jpg', 0)
img8 = cv2.resize(img8, (200, 200))
img9 = cv2.imread('312.jpg', 0)
img9 = cv2.resize(img9, (200, 200))
train_images = [img1, img2, img3, img4, img5, img6, img7, img8, img9]
labels = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
recognizer = cv2.face.EigenFaceRecognizer_create()
recognizer.train(train_images, labels)
testimg = cv2.imread('paizhao.jpg', 0)
testimg = cv2.resize(testimg, (200, 200))
label, confidence = recognizer.predict(testimg)
print('匹配标签:', label)
print('可信程度:', confidence)
```
请注意,我们还将标签修改为包含与train_images中的图像数量相同的元素,并将图像112的标签从1更改为0,图像310和312的标签从1更改为2。这样,labels就对应于train_images中每个图像的标签。
阅读全文