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