0

我正在尝试使用 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 。提前致谢 :)

`

4

1 回答 1

0

OpenCV_test1.py

该程序打开同目录名“image.jpg”中的文件,并显示原始图像和原始图像的 Canny 边缘

导入 cv2 导入 numpy 作为 np 导入操作系统

#

def main(): imgOriginal = cv2.imread("image.jpg") # 打开图片

if imgOriginal is None:                             # if image was not read successfully
    print "error: image not read from file \n\n"        # print error message to std out
    os.system("pause")                                  # pause so user can see error message
    return                                              # and exit function (which exits program)
# end if

imgGrayscale = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2GRAY)        # convert to grayscale

imgBlurred = cv2.GaussianBlur(imgGrayscale, (5, 5), 0)              # blur

imgCanny = cv2.Canny(imgBlurred, 100, 200)                          # get Canny edges

cv2.namedWindow("imgOriginal", cv2.WINDOW_AUTOSIZE)        # create windows, use WINDOW_AUTOSIZE for a fixed window size
cv2.namedWindow("imgCanny", cv2.WINDOW_AUTOSIZE)           # or use WINDOW_NORMAL to allow window resizing

cv2.imshow("imgOriginal", imgOriginal)         # show windows
cv2.imshow("imgCanny", imgCanny)

cv2.waitKey()                               # hold windows open until user presses a key

cv2.destroyAllWindows()                     # remove windows from memory

return
#

如果名称== “”:主()

python OpenCV_test1.py 文件“OpenCV_test1.py”,第 4 行 import cv2 import numpy as np import os

我有同样的错误

于 2017-04-24T06:01:24.550 回答