如何显示多个预测AutoML Vision API

问题描述 投票:0回答:1

我正试图从图像上传中显示每个预测。目前,它只显示一个预测。如果我将score_threshhold设置为0,它只显示最不可能的预测而不是所有预测。

import io
from google.cloud import automl_v1beta1
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="automl_json.json"

#Provides the image, project id from google cloud, model id from AUTOML
def get_prediction(content, project_id, model_id):
  prediction_client = automl_v1beta1.PredictionServiceClient()

  name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
  payload = {'image': {'image_bytes': content }}
  params = { "score_threshold": "0.2" }
  #The response has the image classification and the confidence score for that image
  response = prediction_client.predict(name, payload, params)
  labels = response.payload # Get labels from response
  #Get the classification of that image from labels
  image_class=labels[0].display_name
  #score_result has  has the confidence score for that category
  score_result=labels[0].classification
  confidence=score_result.score
  #print(labels)
  return image_class,confidence  # waits till request is returned

def fetch_prediction(content,project_id,model_id):
    file_path = content
    project_id = project_id
    model_id = model_id

    with open(file_path, 'rb') as ff:
        content = ff.read()

    classification,confidence=get_prediction(content, project_id,  model_id)
    #results={classification:confidence}
    #results=get_prediction(content, project_id,  model_id)
    return classification,confidence

另一篇文章建议我设定门槛较低但不会产生更多产出。谢谢

python automl google-cloud-automl-nl
1个回答
0
投票

通过添加一个打印“Labels”数组的每个元素的循环来解决。

将此代码添加到get_prediction函数:

i = 0
while i < len(labels):
  predictedName = labels[i].display_name
  predictedConfidence = labels[i].classification.score
  print(predictedName) #DEBUG PRINTS ALL SCORES AND CONFIDENCES
  print(predictedConfidence)
© www.soinside.com 2019 - 2024. All rights reserved.