[英]Python custom vision predictor fails
I'm using Python to retrieve a Blob image from Azure storage and then send it to Custom Vision for a prediction. 我正在使用Python从Azure存储中检索Blob图像,然后将其发送到Custom Vision进行预测。 This is the code:
这是代码:
import io
from azure.storage.blob import BlockBlobService
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
block_blob_service = BlockBlobService(
account_name=account_name,
account_key=account_key
)
fp = io.BytesIO()
block_blob_service.get_blob_to_stream(
container_name,
blob_name,
fp,
max_connections=2
)
predictor = CustomVisionPredictionClient(
cv_prediction_key,
endpoint=cv_endpoint
)
# This call breaks with the below error message
results = predictor.predict_image(
cv_project_id,
image_data.getvalue(),
iteration_id=cv_iteration_id
)
However, executing the predict_image
function results in the following error: 但是,执行
predict_image
函数会导致以下错误:
System.Private.CoreLib: Exception while executing function: Functions.ReloadPostgres. System.Private.CoreLib: Result: Failure
Exception: HttpOperationError: Operation returned an invalid status code 'Resource Not Found'
Stack: File "~/.local/share/virtualenvs/py_func_app-GVYYSfCn/lib/python3.6/site-packages/azure/functions_worker/dispatcher.py", line 288, in _handle__invocation_request
self.__run_sync_func, invocation_id, fi.func, args)
File "~/.pyenv/versions/3.6.8/lib/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "~/.local/share/virtualenvs/py_func_app-GVYYSfCn/lib/python3.6/site-packages/azure/functions_worker/dispatcher.py", line 347, in __run_sync_func
return func(**params)
File "~/py_func_app/ReloadPostgres/__init__.py", line 14, in main
data_handler.fetch_prediction_data()
File "~/py_func_app/Shared_Code/data_handler.py", line 127, in fetch_prediction_data
cv_handler.predict_image(image_data.getvalue(), cv_model)
File "~/py_func_app/Shared_Code/custom_vision.py", line 30, in predict_image
raise e
File "~/py_func_app/Shared_Code/custom_vision.py", line 26, in predict_image
iteration_id=cv_model.cv_iteration_id
File "~/.local/share/virtualenvs/py_func_app-GVYYSfCn/lib/python3.6/site-packages/azure/cognitiveservices/vision/customvision/prediction/custom_vision_prediction_client.py", line 215, in predict_image
raise HttpOperationError(self._deserialize, response)
Here in below i am providing similar example using custom vision prediction using image URL, you can change it to image file : 在下面的示例中,我提供了使用图像URL进行自定义视觉预测的类似示例,您可以将其更改为图像文件:
# -*- coding: utf-8 -*- """ Created on Tue Mar 19 11:04:54 2019 @author: moverm """ #from azure.storage.blob import BlockBlobService from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient #block_blob_service = BlockBlobService( # account_name=account_name, # account_key=account_key #) # #fp = io.BytesIO() #block_blob_service.get_blob_to_stream( # container_name, # blob_name, # fp, # max_connections=2 #) predictor = CustomVisionPredictionClient( "prediction-key", endpoint="https://southcentralus.api.cognitive.microsoft.com" ) # This call breaks with the below error message #results = predictor.predict_image( # 'prediction-key', # image_data.getvalue(), # iteration_id=cv_iteration_id #) test_img_url = "https://pointsprizes-blog.s3-accelerate.amazonaws.com/316.jpg" results = predictor.predict_image_url("project-Id", "Iteration-Id", url=test_img_url) # Display the results. for prediction in results.predictions: print ("\\t" + prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100))
Basically issue is related to endpoint.Use https://southcentralus.api.cognitive.microsoft.com for an endpoint. 基本上问题与端点有关。使用https://southcentralus.api.cognitive.microsoft.com作为端点。
It should work, and you should be able to see the prediction probability. 它应该起作用,并且您应该能够看到预测概率。
Hope it helps. 希望能帮助到你。
I tried to reproduce your issue and got a similar issue, which was caused by using the incorrect endpoint from Azure portal when I created a Cognitive Service on the region of Janpa East, as the figure below. 我试图重现您的问题,并遇到了类似的问题,这是由于在Janpa East地区创建认知服务时使用Azure门户的不正确端点导致的,如下图所示。
As the figure above shown, the endpoint is https://japaneast.api.cognitive.microsoft.com/customvision/training/v1.0
for version 1, but the azure-cognitiveservices-vision-customvision
PyPI page points out the corrent endpoint which should be https://{AzureRegion}.api.cognitive.microsoft.com
as the figure below. 如上图所示,版本1的端点为
https://japaneast.api.cognitive.microsoft.com/customvision/training/v1.0
,但azure-cognitiveservices-vision-customvision
PyPI页指出了相应的端点如下图所示,它应该是https://{AzureRegion}.api.cognitive.microsoft.com
。
So I got the similar issue with yours if using the incorrent endpoint, as below. 因此,如果使用不适当的端点,我也会遇到类似的问题,如下所示。 My code used is the same as yours, the only difference is the running environment which yours is on Azure Functions, but mine is a console script.
我使用的代码与您的代码相同,唯一的区别是您在Azure Functions上运行的环境,但我的是控制台脚本。
Meanwhile, according to the source code custom_vision_prediction_client.py
of Azure Cognitive Service SDK for Custom Vision, you can see the code base_url = '{Endpoint}/customvision/v2.0/Prediction'
to concat your passed endpoint with /customvision/v2.0/Prediction
to generate the real endpoint for calling prediction api. 同时,根据用于自定义视觉的Azure认知服务SDK的源代码
custom_vision_prediction_client.py
,您可以看到代码base_url = '{Endpoint}/customvision/v2.0/Prediction'
来将传递的端点与/customvision/v2.0/Prediction
base_url = '{Endpoint}/customvision/v2.0/Prediction'
/customvision/v2.0/Prediction
生成用于调用预测api的真实端点。
Therefore, as @MohitVerma-MSFT said, using https://<your cognitive service region>.api.cognitive.microsoft.com
for the current version of Python package. 因此,正如@ MohitVerma-MSFT所说,使用
https://<your cognitive service region>.api.cognitive.microsoft.com
作为当前版本的Python包。
Additional notes as below, there is an announce of important update for customvision.ai
you need to know, it may make effect for your current code working soon after. 下面的附加说明,您需要知道有关
customvision.ai
的重要更新的公告,它可能对不久之后的当前代码生效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.