You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python调用Azure Custom Vision detect_image时遇PermissionDenied错误

解决Azure Custom Vision detect_image PermissionDenied错误

核心问题定位

调用detect_image属于预测操作,你当前使用训练密钥和训练客户端发起请求,但预测操作需要单独的预测密钥、预测端点,以及CustomVisionPredictionClient,这是触发权限拒绝错误的最常见原因。

具体修复步骤

  1. 创建正确的预测客户端
    从Azure门户的Custom Vision预测资源中获取预测密钥和预测端点,替换训练相关的凭证与客户端:

    from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
    from azure.core.credentials import ApiKeyCredentials
    
    # 替换为你的预测资源密钥和端点
    prediction_key = "你的预测资源密钥"
    prediction_endpoint = "你的预测资源端点"
    
    # 使用Prediction-key创建预测客户端
    credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
    predictor = CustomVisionPredictionClient(endpoint=prediction_endpoint, credentials=credentials)
    
  2. 验证发布迭代参数

    • 确保publish_iteration_name与发布时指定的名称完全一致,该参数大小写敏感
    • 确认prediction_resource_id是预测资源的完整ID,可在Azure门户预测资源的「属性」页面复制
  3. 检查资源关联与权限

    • 确认训练资源与预测资源已正确绑定:在Custom Vision项目的「设置」中,检查预测资源是否关联到当前项目
    • 验证预测密钥有效性:在Azure门户预测资源的「密钥和端点」页面,确认密钥未过期或被删除
  4. 核对API调用参数
    调用detect_image时,确保传入的project.id和publish_iteration_name与发布迭代时的参数完全匹配:

    with open(os.path.join(base_image_location, "test", "test_image.jpg"), mode="rb") as test_data:
        results = predictor.detect_image(project.id, publish_iteration_name, test_data)
    

额外排查点

  • 若使用免费层资源,检查是否达到调用次数上限
  • 确认网络环境未拦截Azure API请求(如防火墙、代理限制)

内容的提问来源于stack exchange,提问作者mintteaplease

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 08:35:19