Pankaj Vatsa
Pankaj Vatsa

Reputation: 2669

Face detection accuracy using opencv and python

I have recently started learning opencv and written a program to detect faces in an image in python and save all detected faces as separate images. It works fine for some images but fails to detect all the faces in many images.

It fails in even this(https://i.sstatic.net/g65FV.jpg) simple image. It detects only the right face but not the left face. Please help how to correct this to increase accuracy?

import cv2.cv as cv
import string
im = cv.LoadImageM("D:\Test\Dia.jpg")
storage = cv.CreateMemStorage()
haar=cv.Load("C:\opencv\data\haarcascades\haarcascade_frontalface_default.xml")
detected = cv.HaarDetectObjects(im, haar, storage, 1.1, 2,cv.CV_HAAR_DO_CANNY_PRUNING,(10,10))
i = 0
if detected:
    for face in detected:
        i = i + 1
        xx = face[0][0]
        yy = face[0][1]
        width = face[0][2]
        height = face[0][3]

        pankaj12 = (width,height)
        cvIm = cv.LoadImage("D:\Test\Dia.jpg")

        cropped = cv.CreateImage(pankaj12,cvIm.depth, cvIm.nChannels)
        src_region = cv.GetSubRect(cvIm, face[0])
        cv.Copy(src_region, cropped)
        cv.SaveImage("D:\Test\Pankaj"+str(i)+".jpg",cropped)

input("Press Enter to continue...")

Upvotes: 2

Views: 5934

Answers (1)

micurs
micurs

Reputation: 543

I had better luck by running the algorithm multiple time using the different haarcascades classifiers provided with OpenCV and then by combining the results.

In practice if two classifiers find similar results you can combine them and give to the combined result a higher score value.

To improve the detection even more you can also use the classifiers to detect eyes, noses and mouths. If you find those inside detected faces you can also add more value to those faces.

At the of the process you can get a list of detected faces with score value. The higher the score the more probable the detection is the correct one.

hope this may help.

Upvotes: 1

Related Questions