|
21 | 21 | # [START vision_face_detection_tutorial_imports]
|
22 | 22 | from google.cloud import vision
|
23 | 23 | from google.cloud.vision import types
|
24 |
| -from PIL import Image, ImageDraw |
| 24 | +from PIL import Image, ImageDraw, ImageFont |
25 | 25 | # [END vision_face_detection_tutorial_imports]
|
26 | 26 |
|
27 | 27 |
|
@@ -59,12 +59,18 @@ def highlight_faces(image, faces, output_filename):
|
59 | 59 | """
|
60 | 60 | im = Image.open(image)
|
61 | 61 | draw = ImageDraw.Draw(im)
|
62 |
| - |
| 62 | + # Sepecify the font-family and the font-size |
| 63 | + font = ImageFont.truetype("arial.ttf", 25) |
63 | 64 | for face in faces:
|
64 | 65 | box = [(vertex.x, vertex.y)
|
65 | 66 | for vertex in face.bounding_poly.vertices]
|
66 | 67 | draw.line(box + [box[0]], width=5, fill='#00ff00')
|
67 |
| - |
| 68 | + # Place the confidence value/score of the detected faces above the |
| 69 | + # detection box in the output image |
| 70 | + draw.text(((face.bounding_poly.vertices)[0].x, |
| 71 | + (face.bounding_poly.vertices)[0].y - 30), |
| 72 | + str(format(face.detection_confidence, '.3f')) + '%', |
| 73 | + font=font, fill='#FF0000') |
68 | 74 | im.save(output_filename)
|
69 | 75 | # [END vision_face_detection_tutorial_process_response]
|
70 | 76 |
|
|
0 commit comments