python - 使用树莓派,Pi相机,Python和Open Cv进行人脸识别

标签 python python-2.7 opencv raspberry-pi2 raspberry-pi3

我正在尝试使用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行出现以下错误

There's an error in your program:expected an intended block.



我尽力解决了这个问题,但没有任何效果。

由于我是树莓派和python的新手,因此我们将不胜感激。

提前致谢。

最佳答案

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

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()

关于python - 使用树莓派,Pi相机,Python和Open Cv进行人脸识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37389693/

相关文章:

c++ - 无法在 OpenCV 中打开 1 位二进制图像?

python - 使字典函数表现得像类方法

python - 使用 html 模板格式化 csv 文件数据

python - 如何在 python 3 中替换此语句 "StringIO import StringIO "(在 python 2 中可用)

c# - 如何在 C# 中将变量参数传递给内部方法

Android JNI C++ 段错误

python - 从 Python 中的 Amazon Kinesis 输出读取字节图像

Python 从 MySQL 数据库中打印出错误的编码

python - Pandas str.extract : AttributeError: 'str' object has no attribute 'str'

python - openCV 失真校准分辨率缩放