【问题标题】:How to display multiple predictions AutoML Vision API如何显示多个预测 AutoML Vision API
【发布时间】:2019-02-25 10:44:03
【问题描述】:

我正在尝试显示来自图像上传的每个预测。目前,它只显示一个预测。如果我将 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】:

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

    将此代码添加到 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)
    

    【讨论】:

      猜你喜欢
      • 2019-02-13
      • 2019-02-06
      • 2019-01-04
      • 1970-01-01
      • 2021-03-01
      • 2021-10-03
      • 2021-09-26
      • 1970-01-01
      • 2020-06-15
      相关资源
      最近更新 更多