forked from FernandaOchoa/LabsIADRA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaces.py
131 lines (110 loc) · 4.95 KB
/
faces.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def show_faces(image_path, detected_faces, show_id=False):
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw
# Open an image
img = Image.open(image_path)
# Create a figure to display the results
fig = plt.figure(figsize=(8, 6))
if detected_faces:
# If there are faces, how many?
num_faces = len(detected_faces)
prediction = ' (' + str(num_faces) + ' faces detected)'
# Draw a rectangle around each detected face
for face in detected_faces:
r = face.face_rectangle
bounding_box = ((r.left, r.top), (r.left + r.width, r.top + r.height))
draw = ImageDraw.Draw(img)
draw.rectangle(bounding_box, outline='magenta', width=5)
if show_id:
plt.annotate(face.face_id,(r.left, r.top + r.height + 15), backgroundcolor='white')
#a = fig.add_subplot(1,1,1)
fig.suptitle(prediction)
plt.axis('off')
plt.imshow(img)
def show_face_attributes(image_path, detected_faces):
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw
# Open an image
img = Image.open(image_path)
# Create a figure to display the results
fig = plt.figure(figsize=(8, 6))
if detected_faces:
# If there are faces, how many?
num_faces = len(detected_faces)
prediction = ' (' + str(num_faces) + ' faces detected)'
# Draw a rectangle around each detected face
for face in detected_faces:
r = face.face_rectangle
bounding_box = ((r.left, r.top), (r.left + r.width, r.top + r.height))
draw = ImageDraw.Draw(img)
draw.rectangle(bounding_box, outline='magenta', width=5)
# Annotate with face attributes (only age and emotion are used in this sample)
detected_attributes = face.face_attributes.as_dict()
age = 'age unknown' if 'age' not in detected_attributes.keys() else int(detected_attributes['age'])
annotations = 'Person aged approximately {}'.format(age)
txt_lines = 1
if 'emotion' in detected_attributes.keys():
for emotion_name in detected_attributes['emotion']:
txt_lines += 1
annotations += '\n - {}: {}'.format(emotion_name, detected_attributes['emotion'][emotion_name])
plt.annotate(annotations,((r.left + r.width), (r.top + r.height + (txt_lines * 12))), backgroundcolor='white')
# Plot the image
#a = fig.add_subplot(1,1,1)
fig.suptitle(prediction)
plt.axis('off')
plt.imshow(img)
def show_similar_faces(image_1_path, image_1_face, image_2_path, image_2_faces, similar_faces):
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw
# Create a figure to display the results
fig = plt.figure(figsize=(16, 6))
# Show face 1
img1 = Image.open(image_1_path)
r = image_1_face.face_rectangle
bounding_box = ((r.left, r.top), (r.left + r.width, r.top + r.height))
draw = ImageDraw.Draw(img1)
draw.rectangle(bounding_box, outline='magenta', width=5)
a = fig.add_subplot(1,2,1)
plt.axis('off')
plt.imshow(img1)
# get the matching face IDs
matching_face_ids = list(map(lambda face: face.face_id, similar_faces))
# Draw a rectangle around each similar face in image 2
img2 = Image.open(image_2_path)
a = fig.add_subplot(1,2,2)
plt.axis('off')
for face in image_2_faces:
r = face.face_rectangle
bounding_box = ((r.left, r.top), (r.left + r.width, r.top + r.height))
draw = ImageDraw.Draw(img2)
if face.face_id in matching_face_ids:
draw.rectangle(bounding_box, outline='lightgreen', width=10)
plt.annotate('Match!',(r.left, r.top + r.height + 15), backgroundcolor='white')
else:
draw.rectangle(bounding_box, outline='red', width=5)
plt.imshow(img2)
plt.show()
def show_recognized_faces(image_path, detected_faces, recognized_face_names):
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw
# Open an image
img = Image.open(image_path)
# Create a figure to display the results
fig = plt.figure(figsize=(8, 6))
if detected_faces:
# If there are faces, how many?
num_faces = len(recognized_face_names)
caption = ' (' + str(num_faces) + ' faces recognized)'
# Draw a rectangle around each detected face
for face in detected_faces:
r = face.face_rectangle
bounding_box = ((r.left, r.top), (r.left + r.width, r.top + r.height))
draw = ImageDraw.Draw(img)
draw.rectangle(bounding_box, outline='magenta', width=5)
if face.face_id in recognized_face_names:
plt.annotate(recognized_face_names[face.face_id],
(r.left, r.top + r.height + 15), backgroundcolor='white')
#a = fig.add_subplot(1,1,1)
fig.suptitle(caption)
plt.axis('off')
plt.imshow(img)