Ask Your Question

green266's profile - activity

2018-11-15 07:06:16 -0600 received badge  Notable Question (source)
2018-04-25 21:53:45 -0600 received badge  Popular Question (source)
2016-03-14 11:18:37 -0600 commented answer OpenCV python: face crop program

Ye thanks I found that 500 x 500 worked best with 1.7 and 5

2016-03-13 05:28:30 -0600 received badge  Scholar (source)
2016-03-13 05:27:51 -0600 commented answer OpenCV python: face crop program

Thanks for the help

Works nearly perfectly now!! Just ran a 100 hundred images on it only made a few mistakes by detecting two faces in one. Surprisingly I think the program's a bit racist as it just doesn't work very well with coloured people. Apparently Obama has a two faces.

2016-03-12 14:08:22 -0600 asked a question OpenCV python: face crop program

I was just experimenting with some code and I didn't do what I think it should of done (at least it worked).

So the code below was meant to identify faces saved in an "input" folder. Then it would detect all the faces sometimes the errors as well, and save them cropped in a separate "output" folder. Unfortunately It only saves the last face which was detected on the image rather than all the faces detected, including some of the errors. I tried to do a if eyes detected then statement to filter some errors out but it didn't work very well.

Will appreciate any help given

Thanks

Here is the code:

import numpy as np
import cv2
import os, os.path

#multiple cascades: https://github.com/Itseez/opencv/tree/master/data/haarcascades

#https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
face_cascade = cv2.CascadeClassifier('faces.xml')
#https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_eye.xml
eye_cascade = cv2.CascadeClassifier('eye.xml')

DIR = 'input'
numPics = len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])

for pic in range(1, (numPics+1)):
    img = cv2.imread('input/'+str(pic)+'.jpg')
    height = img.shape[0]
    width = img.shape[1]
    size = height * width

    if size > (500^2):
        r = 500.0 / img.shape[1]
        dim = (500, int(img.shape[0] * r))
        img2 = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
        img = img2

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    eyesn = 0

    for (x,y,w,h) in faces:
        imgCrop = img[y:y+h,x:x+w]
        #cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            #cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
            eyesn = eyesn +1
        if eyesn >= 2:
            cv2.imwrite("output/crop"+str(pic)+".jpg", imgCrop)

    #cv2.imshow('img',imgCrop)
    print("Image"+str(pic)+" has been processed and cropped")
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

#cap.release()
print("All images have been processed!!!")
cv2.destroyAllWindows()
cv2.destroyAllWindows()