Autocropping Faces from Images in Python Using OpenCV

Today, with this quick blog post, I'm presenting before you a simple solution for one of the biggest issues faced by machine learning engineers, data scientists, and tech enthusiasts.

Extracting faces from a huge lot of images manually seems to be a big challenge. Suppose, if you are building a face recognition system, you need to prepare a pretty voluminous amount of dataset to train the artificial intelligence model.

This dataset should contain a considerable number of images of faces and for better performance of your model, it is necessary that you extract those faces from the images. This is because, an image of a person will certainly contain some background, which is a redundant piece of information for our task. Also if these images are directly used to training, it will reduce the efficiency of your model to a great extent.

Cropping and saving the extracted faces from these images manually is a seemingly impossible task. So let's try to do this with Python.


The script is performing two operations:
1. Detecting faces in the image
2. Cropping and saving the detected faces as a new image.



 autocrop_faces.py


I have already explained in detail how you can detect faces in an image in a previous blog post,
check it out here. I think the whole process of face detection is now clear to you, so let's continue with the additional features included in this program.


Here the directory which contains the images to be cropped is specified.

directory = "C:\\Users\Cyril Tom Mathew\Desktop\images_test"


Then we loop through all the images in the folder (images_test) and face detection is performed on every image. In order to eliminate any files other than images, ''if filename.endswith(".jpg") or filename.endswith(".png"):'' is used. If you have images of any other formats, specify that too in the 'if' condition. 

for filename in os.listdir(directory):
     if filename.endswith(".jpg") or filename.endswith(".png"):
         image = cv2.imread(os.path.join(directory, filename))
          gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


The next step is where the actual face detection and cropping happens(refer here).
If regions other than faces are been detected, changing the value of scaleFactor might help you with that.

faces = face_cascade.detectMultiScale(gray_img, 1.38, 5)


Finally, the cropped faces are saved in the folder crop_faces, with a random filename.

for (x, y, w, h) in faces:
    img_face = image[y:y+h, x:x+w]
    file_name = randrange(1000)
    cv2.imwrite("C:\\Users\Cyril Tom Mathew\Desktop\crop_faces\%s.png" %file_name, img_face)
 
























































































































































































Autocrop Faces from Images in Python
Output


I hope you enjoy reading this, if you have any trouble implementing this or if you need any help, feel free to comment below.

Thank You.


Autocropping Faces from Images in Python Using OpenCV Autocropping Faces from Images in Python Using OpenCV Reviewed by Cyril Tom Mathew on July 15, 2019 Rating: 5

No comments:

Powered by Blogger.