0
votes

I am trying to implement face recognition using python and Open Cv. I have successfully implemented face detection using python by following few tutorials available and its working fine.

Now what I am trying to do is to do face recognition I have followed few tutorials but none of them is working for me.

I have followed this tutorial which was clear enough but the code there is throwing a syntax error.

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

I tried to run this code

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

but I am getting following error in line number 6

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

I tried my best to solve it but nothing worked.

As I am a newbie to raspberry pi and python, any help will be appreciated.

Thanks in advance.

1
This question has quite a broad scope and might not be well suited to Stack Overflow. Do you have any specific questions about what was not working for you with the code you implemented? Can you post your code? - Paul Rooney
@PaulRooney thanks for answering .i have made a edit to the question.Please check.Hope it makes sense now. - Harry Sharma
Can you post the syntax error? Bear in mind, some of the code in that article is not python. - Paul Rooney
@PaulRooney please see my edited question.If you still find it incomplete please help to make this question better. Thanks - Harry Sharma
All your code is indented wrongly, as per the error message. It does not help that the original post has poorly formatted code blocks. Do you not know enough about python to correctly indent that code? It's actually impossible to know what the authors intended indentation was, you might have to try a few different possibilities. You should leave a message on his blog to sort his code samples out:) - Paul Rooney

1 Answers

1
votes

You can reformat it as follows and see if you are getting any frames.

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