Skip to content

Commit

Permalink
Update webcam examples to show how to recognize more than one face
Browse files Browse the repository at this point in the history
  • Loading branch information
ageitgey committed Jan 26, 2018
1 parent 1477c3e commit 743815e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
23 changes: 20 additions & 3 deletions examples/facerec_from_webcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
obama_face_encoding,
biden_face_encoding
]
known_face_names = [
"Barack Obama",
"Joe Biden"
]

while True:
# Grab a single frame of video
ret, frame = video_capture.read()
Expand All @@ -29,11 +43,14 @@
# Loop through each face in this frame of video
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
# See if the face is a match for the known face(s)
match = face_recognition.compare_faces([obama_face_encoding], face_encoding)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)

name = "Unknown"
if match[0]:
name = "Barack"

# If a match was found in known_face_encodings, just use the first one.
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]

# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
Expand Down
22 changes: 19 additions & 3 deletions examples/facerec_from_webcam_faster.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
obama_face_encoding,
biden_face_encoding
]
known_face_names = [
"Barack Obama",
"Joe Biden"
]

# Initialize some variables
face_locations = []
face_encodings = []
Expand All @@ -42,11 +56,13 @@
face_names = []
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
match = face_recognition.compare_faces([obama_face_encoding], face_encoding)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"

if match[0]:
name = "Barack"
# If a match was found in known_face_encodings, just use the first one.
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]

face_names.append(name)

Expand Down

0 comments on commit 743815e

Please sign in to comment.