Python自定义视觉预测器失败 [英] Python custom vision predictor fails
问题描述
我正在使用Python从Azure存储中检索Blob图像,然后将其发送到Custom Vision进行预测.这是代码:
import io从azure.storage.blob导入BlockBlobService从azure.cognitiveservices.vision.customvision.prediction导入CustomVisionPredictionClientblock_blob_service = BlockBlobService(account_name =帐户名,account_key =帐户_密钥)fp = io.BytesIO()block_blob_service.get_blob_to_stream(container_name,blob_name,fp,max_connections = 2)预测变量= CustomVisionPredictionClient(cv_prediction_key,端点= cv_endpoint)#此呼叫中断并显示以下错误消息结果= dictor.predict_image(cv_project_id,image_data.getvalue(),eration_id = cv_iteration_id)
但是,执行 predict_image
函数会导致以下错误:
System.Private.CoreLib:执行函数:Functions.ReloadPostgres时发生异常.System.Private.CoreLib:结果:失败异常:HttpOperationError:操作返回了无效的状态码未找到资源"堆栈:_handle__invocation_request中的第288行,文件〜/.local/share/virtualenvs/py_func_app-GVYYSfCn/lib/python3.6/site-packages/azure/functions_worker/dispatcher.py"self .__ run_sync_func,invocation_id,fi.func,args)运行中的文件〜/.pyenv/versions/3.6.8/lib/python3.6/concurrent/futures/thread.py",第56行结果= self.fn(* self.args,** self.kwargs)__run_sync_func的第347行〜/.local/share/virtualenvs/py_func_app-GVYYSfCn/lib/python3.6/site-packages/azure/functions_worker/dispatcher.py"返回func(** params)主文件第14行中的文件〜/py_func_app/ReloadPostgres/__ init__.py"data_handler.fetch_prediction_data()文件〜/py_func_app/Shared_Code/data_handler.py",第127行,位于fetch_prediction_data中cv_handler.predict_image(image_data.getvalue(),cv_model)第30行中的〜/py_func_app/Shared_Code/custom_vision.py"文件,位于预报图像中提高e在predict_image中的文件〜/py_func_app/Shared_Code/custom_vision.py",第26行eration_id = cv_model.cv_iteration_id文件〜/.local/share/virtualenvs/py_func_app-GVYYSfCn/lib/python3.6/site-packages/azure/cognitiveservices/vision/customvision/prediction/custom_vision_prediction_client.py",行215,位于predict_image中引发HttpOperationError(self._deserialize,response)
我尝试重现您的问题,并遇到了类似的问题,这是由于在Azure区域上创建认知服务时使用Azure门户的不正确终结点引起的扬帕东(Janpa East),如下图.
如上图所示,版本1的端点为 https://japaneast.api.cognitive.microsoft.com/customvision/training/v1.0
,但
因此,如果使用不适当的端点,我也会遇到类似的问题,如下所示.我使用的代码与您的代码相同,唯一的区别是您在Azure Functions上的运行环境,而我的是控制台脚本.
同时,根据用于自定义视觉的Azure认知服务SDK的源代码 custom_vision_prediction_client.py
,您可以看到代码
I'm using Python to retrieve a Blob image from Azure storage and then send it to Custom Vision for a prediction. 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:
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)
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.
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.
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.
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.
Therefore, as @MohitVerma-MSFT said, using https://<your cognitive service region>.api.cognitive.microsoft.com
for the current version of Python package.
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.
这篇关于Python自定义视觉预测器失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!