0

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.

10
  • 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? Commented May 23, 2016 at 11:29
  • @PaulRooney thanks for answering .i have made a edit to the question.Please check.Hope it makes sense now. Commented May 23, 2016 at 11:32
  • 1
    Can you post the syntax error? Bear in mind, some of the code in that article is not python. Commented May 23, 2016 at 11:37
  • @PaulRooney please see my edited question.If you still find it incomplete please help to make this question better. Thanks Commented May 23, 2016 at 11:47
  • 1
    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:) Commented May 23, 2016 at 11:49

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.