Skip to content

Commit 61b5c40

Browse files
peshmergeengelke
authored andcommitted
Display the score/confidence value [(#1429)](GoogleCloudPlatform/python-docs-samples#1429)
* Display the score/confidence value A small code addition to display the score/confidence value of a detected face above the face detection box on the output image. This is very useful to know the confidence! * Changes applied to meet coding style requirements I have edited the already submitted code to meet the coding style requirements! * Edits because white spaces
1 parent 3193fb1 commit 61b5c40

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

samples/snippets/face_detection/faces.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# [START vision_face_detection_tutorial_imports]
2222
from google.cloud import vision
2323
from google.cloud.vision import types
24-
from PIL import Image, ImageDraw
24+
from PIL import Image, ImageDraw, ImageFont
2525
# [END vision_face_detection_tutorial_imports]
2626

2727

@@ -59,12 +59,18 @@ def highlight_faces(image, faces, output_filename):
5959
"""
6060
im = Image.open(image)
6161
draw = ImageDraw.Draw(im)
62-
62+
# Sepecify the font-family and the font-size
63+
font = ImageFont.truetype("arial.ttf", 25)
6364
for face in faces:
6465
box = [(vertex.x, vertex.y)
6566
for vertex in face.bounding_poly.vertices]
6667
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')
6874
im.save(output_filename)
6975
# [END vision_face_detection_tutorial_process_response]
7076

0 commit comments

Comments
 (0)