0

我正在尝试使用 python 和 Open Cv 实现人脸识别。通过遵循一些可用的教程,我已经成功地使用 python 实现了人脸检测,并且工作正常。

现在我想做的是做人脸识别,我遵循了一些教程,但没有一个对我有用。

我已按照本教程进行操作,该教程很清楚,但那里的代码引发了语法错误。

https://oscarliang.com/raspberry-pi-face-recognition-opencv/

我试图运行这段代码

import cv
cv.NamedWindow(“w1”, cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)
def repeat():
global capture #declare as globals since we are assigning to them now
global camera_index
frame = cv.QueryFrame(capture)
cv.ShowImage(“w1″, frame)
c = cv.WaitKey(10)

if(c==”n”): #in “n” key is pressed while the popup window is in focus
            camera_index += 1 #try the next camera index
            capture = cv.CaptureFromCAM(camera_index)

            if not capture: #if the next camera index didn’t work, reset to 0.
            camera_index = 0
            capture = cv.CaptureFromCAM(camera_index)

            while True:
            repeat()

但我在第 6 行出现以下错误

您的程序中有一个错误:预期的块。

我尽力解决它,但没有任何效果。

由于我是树莓派和 python 的新手,任何帮助将不胜感激。

提前致谢。

4

1 回答 1

1

您可以按如下方式重新格式化它,看看是否有任何帧。

import cv2.cv as cv

cv.NamedWindow('w1', cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)

def repeat():
    global capture #declare as globals since we are assigning to them now
    global camera_index
    frame = cv.QueryFrame(capture)
    if frame:
        cv.ShowImage('w1', frame)
        c = cv.WaitKey(10)
        if(c=='n'): #in “n” key is pressed while the popup window is in focus
            camera_index += 1 #try the next camera index
            capture = cv.CaptureFromCAM(camera_index)
            if not capture: #if the next camera index didn’t work, reset to 0.
                camera_index = 0
                capture = cv.CaptureFromCAM(camera_index)

 while True:
     repeat()
于 2016-05-23T11:59:14.257 回答