Skip to content
Advertisement

Python: Image face detection and sorting into face and no face

I am trying to create a sorting system where an image is sorted based on having a face or not. and it doesn’t seem to run quite as expected. after the first image is sorted, the loop stops working and I can’t seem to figure out what is wrong. (I am aware of how inefficient it is). all in all, I would love some pointers as to why it might not be working.

import cv2
import os
from PIL import Image


lst = [
    file
    for file in os.listdir("~/face detect")
    if file.endswith(".jpg")
]


for image in lst:
    
    face_cascade=cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
    
    img = cv2.imread(image)
    gray_img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray_img, 
                                    scaleFactor= 1.15,
                                    minNeighbors= 15)

    print("Found {0} faces!".format(len(faces)))  
    
    if len(faces) > 0:
    
        directory = '~/Face'
        os.chdir(directory)
        output_filename = "".join(image.split('.')[:-1]) + "_face.jpg" # Set output file name
        cv2.imwrite(output_filename, img)

    else:
        directory = '~/No_face'
        os.chdir(directory)  
        output_filename = "".join(image.split('.')[:-1]) + "_no_face.jpg" # Set output file name
        cv2.imwrite(output_filename, img )
    print("image sorted")

#    resized=cv2.resize(img,(int(img.shape[1]/3), int(img.shape[0]/3))) 

#    cv2.imshow("Deteced-face", resized)
#    cv2.waitKey(0)
#    cv2.destroyAllWindows()

Advertisement

Answer

Your main problem is the paths. Let me take a guess that your python file is in ‘~/face detect’ folder, and that is why the first image is read. Then os.chdir(directory) comes and no more images can be found. I corrected the paths to use folders in a less naive way (the correct way was to use glob but i didn’t want to over complicate the answer). Also notice there is no need to change dir in order to save to it, and the fact i initialized the cv2.CascadeClassifier once outside the loop

import cv2
import os

INPUT_DIR = './input'
FACES_DIR = './out_faces'
NO_FACES_DIR = './out_no_faces'

images_names = [file for file in os.listdir(INPUT_DIR) if file.endswith(".jpg")]
print(images_names)

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")  # init once

for image_name in images_names:
    img = cv2.imread('{}/{}'.format(INPUT_DIR, image_name))  # notice the INPUT_DIR
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray_img, scaleFactor=1.15, minNeighbors=15)
    print("Found {0} faces!".format(len(faces)))

    if len(faces) > 0:
        output_fp = '{}/{}'.format(FACES_DIR, image_name.replace('.jpg', '_face.jpg'))  # notice the FACES_DIR
        for (x, y, w, h) in faces:  # if you want to add the face that was detected
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    else:
        output_fp = '{}/{}'.format(NO_FACES_DIR, image_name.replace('.jpg', '_no_face.jpg'))  # notice the NO_FACES_DIR

    cv2.imwrite(output_fp, img)
    print("image {} saved at {}".format(image_name, output_fp))
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement