OpenCV image cropping error

asked 2015-09-19 03:33:32 -0600

updated 2015-09-19 03:44:41 -0600

berak gravatar image

I executed a program that reads all .jpg files from directory, performs face detection, crops the faces and saves them. The problem is that when run an official python program I am able to detect all faces, but it saves only few faces from every image.currently it is cropping per image. What am I doing wrong?

import cv2
import sys
import glob 

cascPath = "haarcascade_frontalface_default.xml"

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

files=glob.glob("*.jpg")   
for file in files:

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

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

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

    # Crop Padding
    left = 10
    right = 10
    top = 10
    bottom = 10

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        print x, y, w, h

        # Dubugging boxes
        # cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)


    image  = image[y-top:y+h+bottom, x-left:x+w+right]

    print "cropped_{1}{0}".format(str(file),str(x))
    cv2.imwrite("cropped_{1}_{0}".format(str(file),str(x)), image)
edit retag flag offensive close merge delete

Comments

do you understand, why only the last face image is saved from your code ?

berak gravatar imageberak ( 2015-09-19 05:34:46 -0600 )edit

No can u please tell me

deepak2293 gravatar imagedeepak2293 ( 2015-09-22 21:01:58 -0600 )edit