Skip to content

Instantly share code, notes, and snippets.

@seanballais
Last active June 6, 2018 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanballais/ca02fa90b44addc4da7bae16a5feda65 to your computer and use it in GitHub Desktop.
Save seanballais/ca02fa90b44addc4da7bae16a5feda65 to your computer and use it in GitHub Desktop.
Facial Detection Test using face_recognition
# The image you will detect faces in must be named 'test.jpg' and
# must reside in the same directory where you placed this script.
#
# Resulting image, where the detected faces are drawn a white rectangle
# over, is saved in the same directory where you placed this script and
# has the file name, 'result.jpeg'.
from PIL import Image, ImageDraw
import face_recognition
import time
start_time = time.time()
image = face_recognition.load_image_file('test.jpg')
face_locations = face_recognition.face_locations(image)
end_time = time.time()
elapsed_time = (end_time - start_time)
print('Time taken by face recognizer: {} seconds'.format(elapsed_time))
drawing_image = Image.open('test.jpg')
draw = ImageDraw.Draw(drawing_image)
for top, right, bottom, left in face_locations:
draw.rectangle((left, top, right, bottom))
draw.rectangle((left - 1, top - 1, right + 1, bottom + 1))
draw.rectangle((left - 2, top - 2, right + 2, bottom + 2))
draw.rectangle((left - 3, top - 3, right + 3, bottom + 3))
draw.rectangle((left - 4, top - 4, right + 4, bottom + 4))
del draw
drawing_image.save('result.jpeg', 'JPEG')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment