user4772964
user4772964

Reputation:

OpenCV: Face detection taking advantage of a command line

I run this (first one) example that launches the webcam of my latop so that I can see myself on the screen.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

I installed OpenBr on Ubuntu 14.04 LTS and I run successfully this command on a picture of myself:

br - gui -algorithm ShowFaceDetection -enrollAll -enroll /home/nakkini/Desktop/myself.png

The above command I run on the Terminal displays my picture and draws a square around my face (face detection), it also highlights my eyes in green.

My Dream:

I wonder if there is a way to combine this command with the short program above so that when the webcam is launched I can see my face surrounded by the green rectangle ?

Why do I need this ?

I found similar programs in pure OpenCV/Python for this purpos. However, for later needs, I need more things than the simple face detection and I judge by myself that OpenBR will save me lot of headache. That is why I am looking for a way to run the command line somewhere inside the code above as a first but big step.

Hints:

The frame in the code corresponds to myself.png of the command line. The solution to be found will try to pass frame in the place of myself.png to the command line within the program itself.

Thank you very much in advance.


EDIT:

After correcting the typos of @Xavier's solution I have no errors. However the program does not run as I want it:

First, the camer is launched and I see myself but my face is not detected with a green rectangle. Secondly, I press any key to exit but the program does not exit: it shows me a picture of myself with my face detected. A last key press exists the program. My goal is to see my face detected during the camera functionment.

Upvotes: 3

Views: 1251

Answers (2)

Xavier Combelle
Xavier Combelle

Reputation: 11195

something like this should work

import numpy as np
import cv2
import os

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        cv2.imwrite( "/home/nakkini/Desktop/myself.png", gray );
        os.system('br - gui -algorithm -ShowFaceDetection -enrollAll -enroll /home/nakkini/Desktop/myself.png')
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

Upvotes: 1

berak
berak

Reputation: 39796

you do not need openbr for this at all.

just see opencv's python face-detect tutorial

Upvotes: 2

Related Questions