Detect face from image using python script with OpenCV

Using OpenCV and python script we could able detect face from image. Here is sample code for it, but some times there is false detection due to image not clear. Here are some factors can affects face detection …

  1. Face in the image not clear.
  2. High definition colored image.
  3. Very big sized.
  4. Image having design similar to human eyes.

Prerequisites

  1. OpenCV latest version.
  2.  haarcascade_frontalface_default.xml – You can find it opencv lib.
import numpy as np
import cv2
import os
import sys

# load the image and convert

image = cv2.imread(sys.argv[1])
haarcascade =haarcascade_frontalface_default.xml

# convert the image to grayscale, load the face cascade detector,
# and detect faces in the image
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
detector = cv2.CascadeClassifier(haarcascade)
rects = detector.detectMultiScale(image, scaleFactor=1.1, minNeighbors=7,
minSize=(10, 10), flags=cv2.cv.CV_HAAR_SCALE_IMAGE)

# construct <a href="http://www.callingallcakes.org/">cartier love necklace</a> a list of bounding boxes from the detection
rects = [(int(x), int(y), int(x + w), int(y + h)) for (x, y, w, h) in rects]

# update the data <a href="http://www.uwst.org/bracelet-replica-car">cartier bracelets</a> dictionary with <a href="http://www.cartierlovebracelet.co/">cartier bracelet
</a> the faces detected
print len(rects)

Categories: Other