Search code examples
pythonflaskgoogle-cloud-platformgoogle-cloud-vision

how to get the percentage for the predicted labels in google cloud vision


i have succesfully get the description for each and every label from google vision API ,but i still need to get the percentage predicted for each label , can any one help me

i do this to get description for label

filename = photos.save(request.files['photo'])
file_url = photos.url(filename)
response = client.label_detection(image=image)
labels = response.label_annotations
for label in labels:
    print(label.description)

the outputs

Hair
Forehead
Chin
White-collar worker
Eyebrow
Hairstyle
Cheek

i'd like to get result like this :

Hair 80%
Forehead 10%
Chin 50%
White-collar worker 0%
Eyebrow 5%
Hairstyle 1%
Cheek 20%

Solution

  • Try this,

    for label in labels:
        desc, score = label.description, int(label.score * 100)
        print(f"{desc} {score}%")