OpenCV is an amazing piece of software and the newer Pi's are getting the power to run real time image processing tasks, like face detection.

 

 

 

 

 

However, OpenCv is not that user friendly, Lets make an attempt to simplify it a little. Most detection scripts need accompanying files that contain the information to identify what we are searching for in the image we just captured. Luckily OpenCv comes pre packed with some of these Haar-Cascaded [The files that tell open cv what to look for], they can be found in:

 

/usr/share/opencv/haarcascades/          [for Pi]

 

 

[for information on what a HaarCascade is or want to build your own to identify a fish or banana? see:

>http://docs.opencv.org/master/d7/d8b/tutorial_py_face_detection.html#gsc.tab=0

>Robotics@Cyborg: How to make your own haar trained ".xml" files

 

OpenCv comes with default Haar-Cascades to find:

>Faces [frontal and side]

>Eyes [Right or left]

>Number Plates

>Upper and lower bodies of people

You can find many more by a quick google search and paste them into the above folder.

 

So how do we use these to detect faces, run in the terminal:

 

$sudo python Scriptname <Path to Haar-Cascade you like to use]

 

To run the one in the video,

>paste the code below into a empty text file saved as facedetect.py, right click and select any one can execute, then:

>Plug in a usb webcam into the pi [the cheap square ones from ebay work well $3]

>Type into terminal:

 

sudo python favedetect.py /usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml

 

if you want to exit, click on the video window and press esc on keyboard

 

facedetect.py

## ********** Importing Some Libraries **************##

import cv2

import sys

##Dont know what this bit does, but it works

 

 

cascPath = sys.argv[1]

faceCascade = cv2.CascadeClassifier(cascPath)

#Telling script to capture from usb camera

 

 

video_capture = cv2.VideoCapture(0)

 

#************************Main LOOP *****************##

 

 

while True:

 

##********** We burn a few frames to make sure we have the newest one  [stops lag]

    # Capture frame-by-frame

    ret, frame = video_capture.read()

    ret, frame = video_capture.read()

    ret, frame = video_capture.read()

    ret, frame = video_capture.read()

    ret, frame = video_capture.read()

 

##****** Resizing Incoming Image

    res = cv2.resize(frame,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_AREA)

    frame=res

##********Converting to Grey Scale

 

 

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

##******** Finding Faces, changing the values for size of faces in camera view

 

 

    faces = faceCascade.detectMultiScale(

        gray,

        scaleFactor=1.1,

        minNeighbors=5,

        minSize=(30, 30),

        flags=cv2.cv.CV_HAAR_SCALE_IMAGE

    )

 

 

    # Draw a rectangle around the faces

    for (x, y, w, h) in faces:

        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

 

 

    # Display the resulting frame

    cv2.imshow('Video', frame)

 

 

    if cv2.waitKey(33)== 27:

        break

 

 

# When everything is done, release the capture

video_capture.release()

cv2.destroyAllWindows()

 

Checkout the main home page for updates:

 

Animated_Grim Blog: Home Page