/
facereg.py
103 lines (85 loc) · 3.88 KB
/
facereg.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
from google.cloud import vision
from google.cloud.vision import types
from PIL import Image, ImageDraw
import requests
def detect_face(face_file, max_results):
"""Uses the Vision API to detect faces in the given file.
Args:
face_file: A file-like object containing an image with faces.
Returns:
An array of Face objects with information about the picture.
"""
client = vision.ImageAnnotatorClient()
content = face_file.read()
image = types.Image(content=content)
return client.face_detection(
image=image, max_results=max_results).face_annotations
def highlight_faces(image, faces, output_filename):
"""Draws a polygon around the faces, then saves to output_filename.
Args:
image: a file containing the image with the faces.
faces: a list of faces found in the file. This should be in the format
returned by the Vision API.
output_filename: the name of the image file to be created, where the
faces have polygons drawn around them.
"""
im = Image.open(image)
draw = ImageDraw.Draw(im)
# Sepecify the font-family and the font-size
for face in faces:
box = [(vertex.x, vertex.y)
for vertex in face.bounding_poly.vertices]
draw.line(box + [box[0]], width=5, fill='#00ff00')
# Place the confidence value/score of the detected faces above the
# detection box in the output image
draw.text(((face.bounding_poly.vertices)[0].x,
(face.bounding_poly.vertices)[0].y - 30),
str(format(face.detection_confidence, '.3f')) + '%',
fill='#FF0000')
im.save(output_filename)
def main(input_filename, output_filename, max_results = 4):
with open(input_filename, 'rb') as image:
faces = detect_face(image, max_results)
print('Found {} face{}'.format(
len(faces), '' if len(faces) == 1 else 's'))
print(faces)
print('Writing to file {}'.format(output_filename))
# Reset the file pointer, so we can read the file again
image.seek(0)
highlight_faces(image, faces, output_filename)
def detect_faces_uri(uri):
"""Detects faces in the file located in Google Cloud Storage or the web."""
from google.cloud import vision
client = vision.ImageAnnotatorClient()
image = vision.types.Image()
image.source.image_uri = uri
response = client.face_detection(image=image)
faces = response.face_annotations
#get img and annote to local
res = requests.get(uri)
with open('local.jpg','wb') as handle:
for block in res.iter_content(1024):
if not block:
break
handle.write(block)
highlight_faces('local.jpg',faces,'local_annotate.jpg')
# Names of likelihood from google.cloud.vision.enums
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY_LIKELY')
print('Faces:')
for face in faces:
print('anger: {}'.format(likelihood_name[face.anger_likelihood]))
print('joy: {}'.format(likelihood_name[face.joy_likelihood]))
print('surprise: {}'.format(likelihood_name[face.surprise_likelihood]))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in face.bounding_poly.vertices])
print('face bounds: {}'.format(','.join(vertices)))
if response.error.message:
raise Exception(
'{}\nFor more info on error messages, check: '
'https://cloud.google.com/apis/design/errors'.format(
response.error.message))
if __name__ == "__main__":
main('./face.jpg', './res.jpg')
#uri='https://d1qsx5nyffkra9.cloudfront.net/sites/default/files/article-image/eminence-organics-acne-face-mapping.jpg'
#detect_faces_uri(uri)