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

如何为无标注图片创建Labelme格式JSON文件用于算法训练?

为无标注图片生成Labelme兼容的JSON文件

当然可以,你可以通过两种方式生成这类无标注图片对应的Labelme格式JSON文件,用来作为目标检测的负样本:

手动创建单文件模板

  • 复制一份你已有的、标注正常的Labelme JSON文件作为模板
  • 用文本编辑器打开模板,修改以下关键内容:
    • 将imagePath字段的值改为当前无标注图片的文件名(比如"imagePath": "no_tape_001.jpg")
    • 用图片查看工具获取图片的宽高,替换imageWidth和imageHeight的数值
    • 把shapes数组清空,改为"shapes": []
    • 可选:如果不需要imageData字段(该字段是图片的base64编码),可以直接删除这个字段,或者设为null
  • 保存修改后的文件,命名和对应图片一致(比如no_tape_001.json)

脚本批量生成(适合大量无标注图片)

如果有大量无标注图片,用Python脚本批量生成更高效,示例代码如下:

import os
import json
from PIL import Image

# 替换为你的无标注图片文件夹路径
img_dir = "./no_tape_images"

for img_name in os.listdir(img_dir):
    if not img_name.lower().endswith(('.png', '.jpg', '.jpeg')):
        continue
    img_path = os.path.join(img_dir, img_name)
    # 获取图片尺寸
    with Image.open(img_path) as img:
        width, height = img.size
    # 构建Labelme格式的JSON结构
    labelme_json = {
        "version": "5.0.1",
        "flags": {},
        "shapes": [],
        "imagePath": img_name,
        "imageData": None,
        "imageWidth": width,
        "imageHeight": height
    }
    # 保存JSON文件
    json_name = os.path.splitext(img_name)[0] + ".json"
    json_path = os.path.join(img_dir, json_name)
    with open(json_path, 'w', encoding='utf-8') as f:
        json.dump(labelme_json, f, indent=2)

运行脚本前需要确保安装了Pillow库(pip install pillow),它会自动遍历指定文件夹里的图片,生成对应名称的空标注JSON文件。

生成的这些JSON文件可以正常被Labelme打开,也能被绝大多数目标检测训练框架识别为无目标标注的负样本,满足你训练鲁棒模型的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 01:40:09