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

使用Python SDK向Custom Vision批量上传带标签图片报错求助

问题分析与解决方案

错误原因

你遇到的'Image' object has no attribute 'error'错误,核心是对Custom Vision SDK返回结果的属性访问路径错误。upload_result.images中的每个元素是ImageFileCreateResult对象,它的错误信息、图片ID等属性直接挂载在对象本身,而非嵌套在image子属性中。

代码修复

修改批量上传后的结果处理逻辑,调整属性访问路径:

if not upload_result.is_batch_successful:
    for image in upload_result.images:
        if image.status != "OK":
            print(
                f"Image '{image.name}' upload failed. Status: {image.status}, Error Details: {image.error.message if image.error else 'Unknown'}"
            )
        else:
            print(
                f"Image '{image.name}' uploaded successfully with ID: {image.id}")

额外优化建议

  • 处理剩余图片:当前代码仅在image_batch达到设定的batch_size时执行上传,循环结束后会遗漏最后一批不足batch_size的图片,建议在循环结束后补充以下代码:
    # 上传最后一批剩余图片
    if image_batch:
        try:
            upload_result = trainer.create_images_from_files(
                PROJECT_ID, ImageFileCreateBatch(images=image_batch))
            if not upload_result.is_batch_successful:
                for image in upload_result.images:
                    if image.status != "OK":
                        print(
                            f"Image '{image.name}' upload failed. Status: {image.status}, Error Details: {image.error.message if image.error else 'Unknown'}"
                        )
                    else:
                        print(
                            f"Image '{image.name}' uploaded successfully with ID: {image.id}")
        except Exception as e:
            print(f"Final batch upload failed: {e}")
    
  • 优化标签获取逻辑:当前每次循环都调用trainer.get_tag请求API获取标签ID,存在重复请求的冗余。建议提前一次性获取所有标签并缓存为字典:
    # 预获取所有标签并缓存
    tags = trainer.get_tags(PROJECT_ID)
    tag_id_map = {tag.name: tag.id for tag in tags}
    
    # 后续处理标签时直接从字典读取
    for key, value in row.items():
        if key != "filename" and value == "1":
            if key in tag_id_map:
                tag_ids.append(tag_id_map[key])
            else:
                print(f"Warning: Tag '{key}' not found in project")
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 13:02:17