问题描述
我在谷歌云中使用新的 Vertex AI 训练并部署了自定义对象检测模型的 End Point。当我在云上测试模型时,我在图像上看到了完美的边界框。但是当我使用 python 向端点发送请求时,响应的边界框似乎不正确。 请注意,我乘以宽度和高度。
我的代码:-
import base64
from google.cloud import aiplatform
import cv2
from google.cloud.aiplatform.gapic.schema import predict
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="C:\\Users\\tarunmis\\Downloads\\first-cascade-315219-ccaaa402f837.json"
IMAGE_PATH = "C:\\Users\\tarunmis\\Desktop\\p2.jpg"
def predict_image_object_detection_sample(
project: str="MY STR",endpoint_id: str="MY ID",filename: str=IMAGE_PATH,location: str = "us-central1",api_endpoint: str = "us-central1-aiplatform.googleapis.com",):
# The AI Platform services require regional API endpoints.
client_options = {"api_endpoint": api_endpoint}
# Initialize client that will be used to create and send requests.
# This client only needs to be created once,and can be reused for multiple requests.
client = aiplatform.gapic.PredictionServiceClient(client_options=client_options)
with open(filename,"rb") as f:
file_content = f.read()
# The format of each instance should conform to the deployed model's prediction input schema.
encoded_content = base64.b64encode(file_content).decode("utf-8")
instance = predict.instance.ImageObjectDetectionPredictionInstance(
content=encoded_content,).to_value()
instances = [instance]
# See gs://google-cloud-aiplatform/schema/predict/params/image_object_detection_1.0.0.yaml for the format of the parameters.
parameters = predict.params.ImageObjectDetectionPredictionParams(
confidence_threshold=0.5,max_predictions=10,).to_value()
endpoint = client.endpoint_path(
project=project,location=location,endpoint=endpoint_id
)
response = client.predict(
endpoint=endpoint,instances=instances,parameters=parameters
)
print("response")
print(" deployed_model_id:",response.deployed_model_id)
# See gs://google-cloud-aiplatform/schema/predict/prediction/image_object_detection.yaml for the format of the predictions.
predictions = response.predictions
preds = list()
print(response)
for prediction in predictions:
preds.append(dict(prediction))
return preds
# [END aiplatform_predict_image_object_detection_sample]
predictions = predict_image_object_detection_sample()
prediction = predictions[0]
image = cv2.imread(IMAGE_PATH,1)
h,w,c = image.shape
Boxes = prediction['bBoxes']
confs = prediction["confidences"]
for Box,conf in zip(Boxes,confs):
x1 = int(w*Box[0])
y1 = int(h*Box[1])
x2 = int(w*Box[2])
y2 = int(h*Box[3])
if conf>0.1:
cv2.circle(image,(x1,y1),5,(0,255),cv2.FILLED)
cv2.circle(image,(x2,y2),(255,0),cv2.FILLED)
cv2.rectangle(image,255,0))
cv2.imshow("img",image)
cv2.waitKey()
响应是:-
predictions {
struct_value {
fields {
key: "bBoxes"
value {
list_value {
values {
list_value {
values {
number_value: 0.678395331
}
values {
number_value: 0.779298723
}
values {
number_value: 0.645786881
}
values {
number_value: 0.683837295
}
}
}
values {
list_value {
values {
number_value: 0.18701905
}
values {
number_value: 0.287654519
}
values {
number_value: 0.627796173
}
values {
number_value: 0.669630647
}
}
}
}
}
}
fields {
key: "confidences"
value {
list_value {
values {
number_value: 0.813014865
}
values {
number_value: 0.748636127
}
}
}
}
fields {
key: "displayNames"
value {
list_value {
values {
string_value: "plate"
}
values {
string_value: "plate"
}
}
}
}
fields {
key: "ids"
value {
list_value {
values {
string_value: "66451184247898112"
}
values {
string_value: "66451184247898112"
}
}
}
}
}
}
deployed_model_id: "1371469231836626944"
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)