-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgcp.py
56 lines (44 loc) · 2.44 KB
/
gcp.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
import os
from collections import OrderedDict
from PIL import Image, ImageDraw
from google.cloud import vision
from google.cloud.vision import types
def analyze_image_gcp(image_path, image_file, image_content):
# Get GCP client
client = vision.ImageAnnotatorClient()
image = types.Image(content=image_content)
# Performs label detection on the image file
response = client.label_detection(image=image)
raw_labels = response.label_annotations
dict_labels = {label.description: label.score for label in raw_labels}
ordered_labels = OrderedDict(sorted(dict_labels.items(), key=lambda t: t[1], reverse=True))
# Call GCP to detect faces and transform to convenient dictionary
faces = client.face_detection(image=image)
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY_LIKELY')
faces_features = [{'roll_angle': face.roll_angle,
'pan_angle': face.pan_angle,
'tilt_angle': face.tilt_angle,
'detection_confidence': face.detection_confidence,
'landmarking_confidence': face.landmarking_confidence,
'joy': likelihood_name[face.joy_likelihood],
'sorrow': likelihood_name[face.sorrow_likelihood],
'anger': likelihood_name[face.anger_likelihood],
'surprise': likelihood_name[face.surprise_likelihood],
'under_exposed': likelihood_name[face.under_exposed_likelihood],
'blurred': likelihood_name[face.blurred_likelihood],
'headwear': likelihood_name[face.headwear_likelihood],
'img': highlight_faces(image_path, image_file, face, face_id)}
for face_id, face in enumerate(faces.face_annotations, start=1)]
results = {'labels': ordered_labels, 'faces': faces_features}
return results
def highlight_faces(image_path, image_file, face, face_id):
image = Image.open(image_file)
draw = ImageDraw.Draw(image)
box = [(vertex.x, vertex.y) for vertex in face.bounding_poly.vertices]
draw.line(box + [box[0]], width=5, fill='#00ff00')
dirname = os.path.dirname(image_path)
orig_filename, ext = os.path.splitext(os.path.basename(image_path))
filename = '{}_gpc_{}{}'.format(orig_filename, face_id, ext)
image.save(os.path.join(dirname, filename))
return filename