In this section, we will crop out faces from images. We will be using python-animeface to crop the faces from the images. This is an open source GitHub repository that automatically crops faces from images from the command line. It is publicly available at the following link: https://github.com/nya3jp/python-animeface.
Execute the following steps to crop and resize the images:
- First of all, download python-animeface:
pip install animeface
- Next, import the module required for the task:
import glob
import os
import animeface
from PIL import Image
- Next, define the parameters:
total_num_faces = 0
- Next, iterate over all images to crop and resize them one by one:
for index, filename in
enumerate(glob.glob('/path/to/directory/containing/images/*.*')):
- Inside the loop, open the current image and detect a face inside it:
try:
# Open image
im = Image.open(filename)
# Detect faces
faces = animeface.detect(im)
except Exception as e:
print("Exception:{}".format(e))
continue
- Next, get coordinates of the face detected in the images:
fp = faces[0].face.pos
# Get coordinates of the face detected in the image
coordinates = (fp.x, fp.y, fp.x+fp.width, fp.y+fp.height)
- Now, crop the face out of the image:
# Crop image
cropped_image = im.crop(coordinates)
- Next, resize the cropped face image to have a dimension of (64, 64):
# Resize image
cropped_image = cropped_image.resize((64, 64), Image.ANTIALIAS)
- Finally, save the cropped and resized image to the desired directory:
cropped_image.save("/path/to/directory/to/store/cropped/images/filename.png"))
The complete code wrapped inside a Python function appears as follows:
import glob
import os
import animeface
from PIL import Image
total_num_faces = 0
for index, filename in enumerate(glob.glob('/path/to/directory/containing/images/*.*')):
# Open image and detect faces
try:
im = Image.open(filename)
faces = animeface.detect(im)
except Exception as e:
print("Exception:{}".format(e))
continue
# If no faces found in the current image
if len(faces) == 0:
print("No faces found in the image")
continue
fp = faces[0].face.pos
# Get coordinates of the face detected in the image
coordinates = (fp.x, fp.y, fp.x+fp.width, fp.y+fp.height)
# Crop image
cropped_image = im.crop(coordinates)
# Resize image
cropped_image = cropped_image.resize((64, 64), Image.ANTIALIAS)
# Show cropped and resized image
# cropped_image.show()
# Save it in the output directory
cropped_image.save("/path/to/directory/to/store/cropped/images/filename.png"))
print("Cropped image saved successfully")
total_num_faces += 1
print("Number of faces detected till now:{}".format(total_num_faces))
print("Total number of faces:{}".format(total_num_faces))
The preceding script will load all of the images from the folder containing downloaded images, detect faces using the python-animeface library, and crop out the face part from the initial image. Then, the cropped images will be resized to a size of 64 x 64. If you want to change the dimensions of the images, change the architecture of the generator and the discriminator accordingly. We are now ready to work on our network.