From 0b0d619b1c889c637f2f54a316d214be1adb1650 Mon Sep 17 00:00:00 2001 From: Peshmerge Date: Mon, 19 Nov 2018 22:51:22 +0100 Subject: [PATCH] Display the score/confidence value (#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 --- vision/cloud-client/face_detection/faces.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/vision/cloud-client/face_detection/faces.py b/vision/cloud-client/face_detection/faces.py index df44462ea1fb..085c2c1747f8 100755 --- a/vision/cloud-client/face_detection/faces.py +++ b/vision/cloud-client/face_detection/faces.py @@ -21,7 +21,7 @@ # [START vision_face_detection_tutorial_imports] from google.cloud import vision from google.cloud.vision import types -from PIL import Image, ImageDraw +from PIL import Image, ImageDraw, ImageFont # [END vision_face_detection_tutorial_imports] @@ -59,12 +59,18 @@ def highlight_faces(image, faces, output_filename): """ im = Image.open(image) draw = ImageDraw.Draw(im) - + # Sepecify the font-family and the font-size + font = ImageFont.truetype("arial.ttf", 25) 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')) + '%', + font=font, fill='#FF0000') im.save(output_filename) # [END vision_face_detection_tutorial_process_response]