Object Recognition Web API Using OpenCV

Motivation

  • Need for Object Recognition arrived from our experience with building Multilingual Captcha System.

  • No Open Source and efficient solution present , available solutions were Very Expensive and not up to mark.
  • Will to learn more about Image Processing and its application.
  • Need to Create Haar-Cascade Library

Technology Used:

  • OpenCV for Image Processing

  • Python as a language of development.
  • XML file for cascade Classifier file
  • Request/Django for Web API.
  • C++ as library for training Haar Cascade Classifier
  • Digital Ocean for hosting Casscade Classifier File

Brief Introduction:

 

 

Stands for Open Computer Vison is , started by Intel Russia research center for advanced Image Processing Stuff.

 

  • OpenCV:
  • Python:

Open Source Scripting language for varied implementation

  • XML:

XML is scripting language to represent Meta-Data, used for cascade classifier.

  • Request/Django:

Web Framework's for Web API handling. To serve as an endpoint for various Image Service Calls, it returns coordinates of Recognised Images.

  • C++:

Use of C++ as library in order to train Cascade Classifier

  • DigitalOcean

DigitalOcean is used as server hosting services for Casscade Classifier training

Research Work Done:

  • Project Development was started as a Matlab Project

  • Earlier Used  Image Intensities approach for Object Recognition.
  • But Working with Matlab Caused many Problems for the present and future prospect of the project
  • Problem encountered while using above two approaches are :

Problems Encountered:

  • Version Support for MATLAB, caused the biggest problem in developing object Detection for some objects
  • Matlab is slow, now ideal for server sided/ Application development work
  • Image Intensities approach  is not much efficient and undesirable for Real Time Object Recognition purpose.
  • Image Intensities approach is computationally expensive.
  • Lack of Training System in Image Intensities caused, left us with no scope in object Recognition 
  • Uses Haar feature-based cascade classifiers for Object Recognition.
  • It is a machine learning based approach where a cascade function is trained from a lot of positive and negative images.
  • Need to meet large number of training samples is met by Adaboost Algorithm
  • The paper says even 200 features provide detection with 95% accuracy
  • Most efficient Object Detection framework available.

Work Implemented:

Using Viola-Jones framework we have implemented face detection .

Sample Applications:

  1. Face detection using haar cascade face classifier
  2. Face and eye detection using haar cascade eye and frontalface classifier
  3. Hand detection using haar cascade hand classifier
  4. Car detection using haar cascade car classifier

Face detection using haar cascade face classifier

import cv2
import sys

# Get user supplied values
imagePath = sys.argv[1]
cascPath = sys.argv[2]

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)

# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=50,
    minSize=(300, 300),
    flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)

print "Found {0} faces!".format(len(faces))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

cv2.imshow("Faces found", image)
cv2.waitKey(0)

Code Implemented

Output Recieved:

Face and eye detection using haar cascade eye and frontalface classifier

Code Implemented

import cv
imcolor = cv.LoadImage('detectionimg.jpg') # input image
# loading the classifiers
haarFace = cv.Load('haarcascade_frontalface_default.xml')
haarEyes = cv.Load('haarcascade_eye.xml')
# running the classifiers
storage = cv.CreateMemStorage()
detectedFace = cv.HaarDetectObjects(imcolor, haarFace, storage)
detectedEyes = cv.HaarDetectObjects(imcolor, haarEyes, storage)

# draw a green rectangle where the face is detected
if detectedFace:
 for face in detectedFace:
  cv.Rectangle(imcolor,(face[0][0],face[0][1]),
               (face[0][0]+face[0][2],face[0][1]+face[0][3]),
               cv.RGB(155, 255, 25),2)

# draw a purple rectangle where the eye is detected
if detectedEyes:
 for face in detectedEyes:
  cv.Rectangle(imcolor,(face[0][0],face[0][1]),
               (face[0][0]+face[0][2],face[0][1]+face[0][3]),
               cv.RGB(155, 55, 200),2)

print imcolor
cv.NamedWindow('Face Detection', cv.CV_WINDOW_AUTOSIZE)
cv.ShowImage('Face Detection', imcolor) 
#cv.WaitKey()
cv.WaitKey(0)

Output

Hand detection using haar cascade hand classifier

 

Code Implemented

import cv2
import sys

# Get user supplied values
imagePath = sys.argv[1]
cascPath = sys.argv[2]

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)

# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=50,
    minSize=(300, 300),
    flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)

print "Hands {0} faces!".format(len(faces))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

cv2.imshow("Hands found", image)
cv2.waitKey(0)

OUTPUT

Car detection using haar cascade car classifier

Code Implemented

import numpy as np
import cv2

# Loads the data as a VideoCapture format, which is really just
# an image sequence.
image_sequence = 'Data/Camera3/image_%05d.jpg'
cap = cv2.VideoCapture(image_sequence)

# Load our cascade classifier from cars3.xml
car_cascade = cv2.CascadeClassifier(r'cars3.xml')

# Reduce frame number of tests.
number_of_frames_to_load = 30
for frame_id in xrange(number_of_frames_to_load):
    ret, image = cap.read()
    
    # Crop so that only the roads remain, eliminatives the distraction.
    image = image[120:,:-20]
    
    # Use Cascade Classifier to detect cars, may have to tune the
    # parameters for less false positives.
    cars = car_cascade.detectMultiScale(image, 1.008, 5)
    for (x,y,w,h) in cars:
        cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
    
    print 'Processing %d : cars detected : [%s]' % (frame_id, len(cars))

    cv2.imshow('frame', image)
    cv2.waitKey(300)

cap.release()
cv2.destroyAllWindows()

OUTPUT

Cascade-Classifier:

  • Cascade-Clasifier are simple XML file containing Meta Data.
  • These Cascade-Clasifier files are obtained after cascade classifier traning.
  • Adaboost algorithm is used to avoid the need to training large number of sample images

Implementation Done:

  • Trained 6 Haar Cascade Classifier namely of: Cars, Frontal Face,Human Eye, Human Hand, Wall Clock, Human Body.

  • Developed a standalone cascade classifier can be used for variety of implementation.
  • Develop a Web API, for the given library , so that scientist / Developers around the world can use the library, just by sending request to the server.
  • Use Haar-Cascade Classifier  Library with WebCam and even gesture control.

Possible Usage:

  • Scope of usage of the project is very wide.
  • Can be used by scientist to make  Image Processing (Object Detection) based analysis.
  • Can be used by developers to make different type of applications.
  • Can be used for Artificial Intelligence / Machine Learning Purpose.

Future Development Goal

  • Due to Complexity in computing Haar Cascade Classifier technique we plan to export the project into Grid Computing Technique.
  • Port more Object detection classifiers into our library
  • Provide more training to the present objects, in order to increase precision of Casscade Classifier.
  • Build proper JSON API for our Classifier.

 

Biblography:

Project Hosted at:

 

Thank You

deck

By Aalekh Nigam

deck

  • 1,683