Пример #1
0
import facial_recognition

# Load the jpg files into numpy arrays
biden_image = facial_recognition.load_image_file("biden.jpg")
obama_image = facial_recognition.load_image_file("obama.jpg")
unknown_image = facial_recognition.load_image_file("obama2.jpg")

# Get the face encodings for each face in each image file
# Since there could be more than one face in each image, it returns a list of encordings.
# But since I know each image only has one face, I only care about the first encoding in each image, so I grab index 0.
biden_face_encoding = facial_recognition.face_encodings(biden_image)[0]
obama_face_encoding = facial_recognition.face_encodings(obama_image)[0]
unknown_face_encoding = facial_recognition.face_encodings(unknown_image)[0]

known_faces = [biden_face_encoding, obama_face_encoding]

# results is an array of True/False telling if the unknown face matched anyone in the known_faces array
results = facial_recognition.compare_faces(known_faces, unknown_face_encoding)

print("Is the unknown face a picture of Biden? {}".format(results[0]))
print("Is the unknown face a picture of Obama? {}".format(results[1]))
print(
    "Is the unknown face a new person that we've never seen before? {}".format(
        not True in results))
from PIL import Image
import facial_recognition

# Load the jpg file into a numpy array
image = facial_recognition.load_image_file("biden.jpg")

# Find all the faces in the image
face_locations = facial_recognition.face_locations(image)

print("I found {} face(s) in this photograph.".format(len(face_locations)))

for face_location in face_locations:

    # Print the location of each face in this image
    top, right, bottom, left = face_location
    print(
        "A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}"
        .format(top, left, bottom, right))

    # You can access the actual face itself like this:
    face_image = image[top:bottom, left:right]
    pil_image = Image.fromarray(face_image)
    pil_image.show()
import cv2

# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of video.

# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the facial_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

# Load a sample picture and learn how to recognize it.
Batman_image = facial_recognition.load_image_file("Batman.jpg")
Batman_face_encoding = facial_recognition.face_encodings(Batman_image)[0]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)