我正在尝试使用 opencv3 和 python3 进行人脸识别:这是我的代码:
import errno
import numpy as np
import cv2
import os
import sys
import errno
os.chdir("E:\work\data")
face=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
def read_images(path):
x,y=[],[]
c=0
for (dir,dirs,files) in os.walk(path):
for folders in dirs:
spath=os.path.join(dir,folders)
print(spath)
for file in os.listdir(spath):
fpath=os.path.join(spath,file)
img=cv2.imread(fpath,0)
x.append(np.asarray(img,dtype=np.uint8))
y.append(c)
c=c+1
return [x,y]
def face_rec():
names=['Eldhose','Peter']
read_images("E:\work\data")
y=np.asarray(y,dtype=np.int32)
model=cv2.face.createEigenFaceRecognizer()
model.train(np.asarray(x),np.asarray(y))
cap=cv2.VideoCapture(0)
face_cascade=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
while True:
suc,frame=cap.read()
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces=face_cascade.detectMultiScale(gray,1.3,5)
for (a,b,c,d) in faces:
cv2.rectangle(frame,(a,b),(a+c,b+d),(255,0,0),2)
roi=gray[b:b+d,a:a+c]
roi=cv2.resize(roi,(200,200),interpolation=cv2.INTER_LINEAR)
params=model.predict(roi)
print("Label:%s,confidence %.2f" %(params[0],params[1])
cv2.imshow("camera",frame)
cv2.waitKey(30) & 0xff
face_rec()
cv2.destroyAllWindows()
它显示错误。我是初学者。我基础薄弱。其中一个错误是 cv2.imshow("camera",frame) SyntaxError: invalid syntax Please help me 。提前致谢 :)
`