【游戏设计】游戏循环的奥秘
2026/5/23 23:49:18
本脚本用于批量复制LabelMe标注信息,特别适用于以下场景:
核心功能:
pipinstallopencv-python tqdmcopy_labelme_annotations(): 核心功能函数,负责复制标注main(): 程序入口,配置参数并调用核心函数defmain():source_json="../1765555160.json"copy_labelme_annotations(source_json_path=source_json,image_dir="../bbbb",output_dir="../bbbb")当前配置说明:
../1765555160.json../bbbb../bbbb(标注文件将保存在同一目录)创建目录结构
your_project/ ├── copy_annotations.py # 本脚本 ├── 1765555160.json # 您的源标注文件 └── bbbb/ # 存放所有图片的目录 ├── image1.jpg ├── image2.jpg ├── image3.jpg └── ...将脚本参数修改为您实际的路径
defmain():source_json="../1765555160.json"# 修改为您的源JSON文件路径copy_labelme_annotations(source_json_path=source_json,image_dir="../bbbb",# 修改为您的图片目录output_dir="../bbbb"# 修改为输出目录)运行脚本
python copy_annotations.py如果您希望使用命令行参数,可以将main()函数修改为:
defmain():parser=argparse.ArgumentParser(description='批量复制LabelMe标注')parser.add_argument('--source_json',required=True,help='源标注JSON文件路径')parser.add_argument('--image_dir',required=True,help='图片目录路径')parser.add_argument('--output_dir',default=None,help='输出目录路径(可选,默认与image_dir相同)')args=parser.parse_args()copy_labelme_annotations(source_json_path=args.source_json,image_dir=args.image_dir,output_dir=args.output_dir)命令行使用示例:
python copy_annotations.py\--source_json../1765555160.json\--image_dir../bbbb\--output_dir../bbbb| 参数名 | 类型 | 必需 | 说明 |
|---|---|---|---|
source_json_path | str | ✓ | 源标注JSON文件路径,包含第一张图片的标注信息 |
image_dir | str | ✓ | 存放所有图片的目录路径 |
output_dir | str | ✗ | 输出目录路径,如果为None则使用image_dir |
脚本支持以下图片格式(不区分大小写):
.jpg/.jpeg.png.bmp.tiff/.tif生成的JSON文件包含以下关键字段:
version: LabelMe版本号flags: 标签标志shapes: 标注形状列表(点、线、矩形、多边形等)imagePath: 对应的图片文件名imageHeight: 图片高度imageWidth: 图片宽度imageData: None(不包含图像数据,减小文件大小)源标注文件: ../1765555160.json 检测到 5 个标注对象 找到 15 张图片,将为 14 张图片复制标注 复制标注: 100%|██████████| 14/14 [00:02<00:00, 6.21it/s] 完成! 已为 14 张图片创建标注文件在../bbbb/目录下,每张图片都会生成对应的JSON文件:
bbbb/ ├── image1.jpg ├── image1.json # 自动生成 ├── image2.jpg ├── image2.json # 自动生成 ├── ... └── 1765555160.jpg # 源图片(不会重复生成)✅图片内容相似性:所有图片中的物体位置、形状、大小应基本相同
✅图片格式一致性:确保图片是脚本支持的格式
✅路径正确性:检查所有路径是否存在,特别是源JSON文件和图片目录
警告: 无法读取图片 ../bbbb/image1.jpg, 跳过解决方法:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)解决方法:
解决方法:
如果您需要在复制过程中修改标注,可以在循环中添加自定义逻辑:
# 在复制形状标注的循环中添加forshapeinsource_data['shapes']:new_shape=shape.copy()# 示例:修改所有矩形的标签ifnew_shape['shape_type']=='rectangle':new_shape['label']='modified_'+new_shape['label']# 示例:缩放所有坐标(适应不同尺寸)scale_x=width/source_data['imageWidth']scale_y=height/source_data['imageHeight']new_points=[[x*scale_x,y*scale_y]forx,yinnew_shape['points']]new_shape['points']=new_points new_data['shapes'].append(new_shape)# 1. 先复制标注python copy_annotations.py --source_json template.json --image_dir images# 2. 然后用LabelMe验证和微调labelme images/importjsonimportosimportcv2fromtqdmimporttqdmimportargparsedefcopy_labelme_annotations(source_json_path,image_dir,output_dir=None):""" 将第一张图片的labelme标注信息复制到同一目录下的其他图片 参数: source_json_path: 第一张已标注图片对应的JSON文件路径 image_dir: 图片所在的目录 output_dir: 输出目录,如果为None则使用image_dir 功能流程: 1. 读取源JSON文件 2. 获取所有目标图片 3. 为每张目标图片: - 读取图片获取尺寸 - 创建新的标注数据 - 复制标注形状 - 保存JSON文件 """# 读取源JSON文件withopen(source_json_path,'r',encoding='utf-8')asf:source_data=json.load(f)print(f"源标注文件:{source_json_path}")print(f"检测到{len(source_data['shapes'])}个标注对象")# 获取源图片信息source_image_path=source_data['imagePath']source_image_name=os.path.basename(source_image_path)# 设置输出目录ifoutput_dirisNoneoroutput_dir.strip()=="":output_dir=image_dir# 创建输出目录(如果不存在)os.makedirs(output_dir,exist_ok=True)# 获取所有图片文件image_extensions=['.jpg','.jpeg','.png','.bmp','.tiff','.tif']all_images=[]forfinos.listdir(image_dir):ext=os.path.splitext(f)[1].lower()ifextinimage_extensions:all_images.append(f)# 排除源图片target_images=[imgforimginall_imagesifimg!=source_image_name]print(f"找到{len(all_images)}张图片,将为{len(target_images)}张图片复制标注")# 处理每张目标图片successful_count=0forimage_nameintqdm(target_images,desc="复制标注"):image_path=os.path.join(image_dir,image_name)# 读取图片获取尺寸try:img=cv2.imread(image_path)ifimgisNone:print(f"警告: 无法读取图片{image_path}, 跳过")continueheight,width=img.shape[:2]# 创建新的标注数据new_data={"version":source_data.get("version","5.0.1"),"flags":source_data.get("flags",{}),"shapes":[],"imagePath":image_name,"imageData":None,# 不包含图像数据以减小文件大小"imageHeight":height,"imageWidth":width}# 复制形状标注forshapeinsource_data['shapes']:new_shape=shape.copy()# 确保shape_type字段存在if'shape_type'notinnew_shape:new_shape['shape_type']='polygon'new_data['shapes'].append(new_shape)# 生成输出JSON文件名output_json_name=os.path.splitext(image_name)[0]+'.json'output_json_path=os.path.join(output_dir,output_json_name)# 保存新的JSON文件withopen(output_json_path,'w',encoding='utf-8')asf:json.dump(new_data,f,ensure_ascii=False,indent=2)successful_count+=1exceptExceptionase:print(f"处理图片{image_name}时出错:{str(e)}")continueprint(f"完成! 成功为{successful_count}/{len(target_images)}张图片创建标注文件")ifsuccessful_count<len(target_images):print(f"警告: 有{len(target_images)-successful_count}张图片处理失败,请检查日志")defmain():""" 主函数 - 当前配置版本 使用前请根据实际情况修改以下路径: """# ====== 请在此处修改您的路径配置 ======source_json="../1765555160.json"# 源标注JSON文件路径image_dir="../bbbb"# 图片目录路径output_dir="../bbbb"# 输出目录路径(可与image_dir相同)# ======================================print("="*50)print("LabelMe标注批量复制工具")print("="*50)print(f"配置信息:")print(f" 源标注文件:{source_json}")print(f" 图片目录:{image_dir}")print(f" 输出目录:{output_dir}")print("-"*50)# 检查文件和目录是否存在ifnotos.path.exists(source_json):print(f"错误: 源标注文件不存在 -{source_json}")returnifnotos.path.exists(image_dir):print(f"错误: 图片目录不存在 -{image_dir}")returncopy_labelme_annotations(source_json_path=source_json,image_dir=image_dir,output_dir=output_dir)if__name__=="__main__":main()这个脚本能够大幅提升标注效率,特别适用于:
使用流程总结:
通过这个工具,您可以将原本需要几小时的标注工作缩短到几分钟,大大提高数据准备效率!