How to display an image in Python used by Google face detection Vision API

Viewed 77

I am using a Google API (called Vision API) to detect the faces and the emotions in a given image just like documented in cloud.google.com. Everything works well and I get the desired results, but only in the terminal (no GUI displayed) :

Image 1 Terminal results

I want to display the image using a GUI in Python. I am still a beginner in Python and the only way I know to display anything in a canvas is using grid(). I have tried it but it doesn't work because I don't know what variable should be used with grid().

import tkinter as tk
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfile
import io
import os
from google.cloud import vision

root = tk.Tk()
canvas = tk.Canvas(root, width=600, height=300)
canvas.grid(columnspan=3, rowspan=3)

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r'crack-producer-313920-5e16fbab8c24.json'


client = vision.ImageAnnotatorClient()

file_name = os.path.abspath('picture.jpg')



with io.open(file_name, 'rb') as image_file:
    content = image_file.read()
    

image = vision.Image(content=content)


response = client.face_detection(image=image)
faces = response.face_annotations

# 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))

root.mainloop()

I hope my explanation is clear. Any helped is highly appreciated.

0 Answers
Related