forked from bradtraversy/face_recognition_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindentify.py
64 lines (47 loc) · 1.86 KB
/
indentify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import face_recognition
from PIL import Image, ImageDraw
image_of_bill = face_recognition.load_image_file('./img/known/Bill Gates.jpg')
bill_face_encoding = face_recognition.face_encodings(image_of_bill)[0]
image_of_steve = face_recognition.load_image_file('./img/known/Steve Jobs.jpg')
steve_face_encoding = face_recognition.face_encodings(image_of_steve)[0]
image_of_elon = face_recognition.load_image_file('./img/known/Elon Musk.jpg')
elon_face_encoding = face_recognition.face_encodings(image_of_elon)[0]
# Create arrays of encodings and names
known_face_encodings = [
bill_face_encoding,
steve_face_encoding,
elon_face_encoding
]
known_face_names = [
"Bill Gates",
"Steve Jobs",
"Elon Musk"
]
# Load test image to find faces in
test_image = face_recognition.load_image_file('./img/groups/bill-steve-elon.jpg')
# Find faces in test image
face_locations = face_recognition.face_locations(test_image)
face_encodings = face_recognition.face_encodings(test_image, face_locations)
# Convert to PIL format
pil_image = Image.fromarray(test_image)
# Create a ImageDraw instance
draw = ImageDraw.Draw(pil_image)
# Loop through faces in test image
for(top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown Person"
# If match
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
# Draw box
draw.rectangle(((left, top), (right, bottom)), outline=(255,255,0))
# Draw label
text_width, text_height = draw.textsize(name)
draw.rectangle(((left,bottom - text_height - 10), (right, bottom)), fill=(255,255,0), outline=(255,255,0))
draw.text((left + 6, bottom - text_height - 5), name, fill=(0,0,0))
del draw
# Display image
pil_image.show()
# Save image
pil_image.save('identify.jpg')