Face Detection using Tensorflow

Tensorflow is a machine learning library, we have got its stable release two months back. Google is officially using for their internal Production purposes. They got better results, so no matter about the lag on it, all their products including Google Vision, Images, YouTube, and almost every product that we see.

Google Vision one such a great tool which uses TensorFlow that analyses the image that we upload and bring us the useful information in Human Readable Format(HRF) and they are also offering Vision API for the users who just want to analyze the image, the output will be in the form of JSON formatted data.

The Face detection with Tensorflow in an image seems a bit easier than one in the video stream as it contains 15-20 frames per second. There is a nice Code for Object Detection using TensorFlow. The reason why I’m giving is closely related to my current Work. The dataset and the way we train the data over, training and testing images all seem merely same.

Simple Face Detection with Haarcascade Library:

#!/usr/bin/python
"""Face Detection in WebCam."""
import sys
import cv2

try:
    FACE_CASCADE = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    EYE_CASCADE = cv2.CascadeClassifier('haarcascade_eye.xml')

    # 0 for Internal Camera and 1,2,etc., for External Cameras
    # CAP = cv2.VideoCapture(0)
    # IP Camera
    # CAP = cv2.VideoCapture('http://ipaddress/videostream.cgi?user=camerauname&pwd=camerapass')
    # Detect Faces in a Video File
    CAP = cv2.VideoCapture('sample_video.mp4') 

    if CAP.isOpened() is True:
        print "Capturing"
    else:
        print "Can't Capture"
        sys.exit()

    while True:
        RET, IMG = CAP.read()
        GRAY = cv2.cvtColor(IMG, cv2.COLOR_BGR2GRAY)
        FACES = FACE_CASCADE.detectMultiScale(GRAY, 1.3, 5)
        for (x, y, w, h) in FACES:
            cv2.rectangle(IMG, (x, y), (x + w, y + h), (255, 250, 30), 2)
            roi_gray = GRAY[y:y + h, x:x + w]
            roi_color = IMG[y:y + h, x:x + w]
            eyes = EYE_CASCADE.detectMultiScale(roi_gray)
            for (ex, ey, ew, eh) in eyes:
                cv2.rectangle(roi_color, (ex, ey),
                              (ex + ew, ey + eh), (0, 255, 0), 2)
        cv2.imshow('Video Stream', IMG)
        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break

    CAP.release()
    cv2.destroyAllWindows()
except KeyboardInterrupt:
    print('User Termination', )

It uses haar cascade library to detect faces and eyes. The top command in Ubuntu gives me the following output.

fd_detect

It merely uses 1.8% of memory and 271% of CPU resource. Below are some of the processes which involved in face detection,

(i) Face detection in Video Stream,

(ii) Create a class which detects faces within the frames,

(iii) Analyse the location(time) of the video in which faces detected.

(iv) Return the response in JSON format.

 

Leave a comment