python - 如何显示多个预测 AutoML Vision API
问题描述
我正在尝试显示来自图像上传的每个预测。目前,它只显示一个预测。如果我将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
另一篇文章建议我将阈值设置得更低,但不会产生更多输出。谢谢
解决方案
通过添加一个打印“标签”数组的每个元素的循环来解决。
将此代码添加到 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)
推荐阅读
- php - PHP SQL | 在表中查找行,如果不存在,使用其他表
- reactjs - 如果直接从 index.js 调用,Nexjs getStaticProps / getInitialProps 通过工作返回未定义的组件
- android - Android 模拟器中的布局看起来与布局编辑器上显示的不同
- flutter - Flutter 2.0.0 Link Widget 使用方法?
- python - 如何创建n个变量的函数(python)
- mongodb - 获取所有文档,其中 diff beetwen 当前时间戳和一些具有纳秒值的列的值更多一些间隔
- android - 如何更改材质 TabItem 中的图标大小?
- ios - swift - iOS 如何防止 ios 自定义导航栏在键盘出现时向上移动
- http - 找不到与具有绑定 BasicHttpBinding 的端点的方案 https 匹配的基地址。注册的基地址方案是 [http]
- here-api - 使用 HERE Rest API 的最佳实践 - 直接通过客户端或通过我的后端服务器