find_faces_in_picture.py 907 B

1234567891011121314151617181920212223
  1. from PIL import Image
  2. import face_recognition
  3. # Load the jpg file into a numpy array
  4. image = face_recognition.load_image_file("biden.jpg")
  5. # Find all the faces in the image using the default HOG-based model.
  6. # This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
  7. # See also: find_faces_in_picture_cnn.py
  8. face_locations = face_recognition.face_locations(image)
  9. print("I found {} face(s) in this photograph.".format(len(face_locations)))
  10. for face_location in face_locations:
  11. # Print the location of each face in this image
  12. top, right, bottom, left = face_location
  13. print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
  14. # You can access the actual face itself like this:
  15. face_image = image[top:bottom, left:right]
  16. pil_image = Image.fromarray(face_image)
  17. pil_image.show()