Ask Your Question

Revision history [back]

OpenCV image cropping error

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)
click to hide/show revision 2
No.2 Revision

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

berak gravatar image

OpenCV image cropping error

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"

"haarcascade_frontalface_default.xml" # Create the haar cascade

cascade faceCascade = cv2.CascadeClassifier(cascPath)

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

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)