Face detection

With the support of Python libraries and the OpenCV-Library many image processing problems can be solved. In this example we show a simple itom script to detect faces acquired by a live webcam.

Detection with Webcam

First of all, we need to start the camera with the PLUGIN OpenCVGrabber. The best result for face detection is accomplished with gray images, disregarding the color profile. Using the OpenCV CascadeClassifier function and the MultiScale Detection it returns coordinates of the ROI of the Feature (–> Face) With the Coordinate we simply draw a rectangle onto the image ROI.

We also built a small GUI (Right-click->Save) for seeing a LIVE image of the webcam and a snapshot feature to start calculating the faces.

If you need further assistance, check out the links below.

import cv2
import numpy as np

def detect(img, cascade):
    rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4)
    if len(rects) == 0:
        return []
    rects[:,2:] += rects[:,:2]
    return rects

def draw_rects(img, faces, color):
    for x1, y1, x2, y2 in faces:
        cv2.rectangle(img, (x1, y1), (x2, y2), color, 3)

def autoGrabbing_changed(checked):
    if(checked):
        cam.enableAutoGrabbing()
    else:
        cam.disableAutoGrabbing()

def snap():
    d = dataObject()
    cam.startDevice()
    autoGrabbingStatus = cam.getAutoGrabbing()
    cam.disableAutoGrabbing()
    cam.acquire()
    cam.getVal(d)
    img=np.array(d)
    faces = detect(img, cascade)
    draw_rects(img, faces, (255, 255, 255))
    win.plot["source"] = img
    if(autoGrabbingStatus):
        cam.enableAutoGrabbing()
    cam.stopDevice()

def live():
    win.plot["camera"] = cam

def __del__():
    del cam
    gc()

if __name__ == '__main__':

    win = ui("FaceDetect.ui", ui.TYPEWINDOW, childOfMainWindow = True)
    cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    nested = cv2.CascadeClassifier('haarcascade_eye.xml')

    try:
        cam= dataIO("OpenCVGrabber",0,'gray')
    except Exception:
        cam = dataIO("DummyGrabber",2048,1024,8)

    #Connect buttons to program
    win.btnSnap.connect("clicked()", snap)
    win.btnLive.connect("clicked()", live)
    win.checkAutoGrabbing.connect("clicked(bool)", autoGrabbing_changed)
    #Initialize gui elements
    win.checkAutoGrabbing["checked"] = cam.getAutoGrabbing()
    #start GUI
    win.show(0)

Result

Facedetection

Resources

Face Detection with Python and OpenCV

arrow_backPREVIOUS
Examples
NEXT arrow_forward
Hough Transform and Edge detection