标签拷贝 labelme json格式

label_copy.py

python 复制代码
import glob
import json
import os

from natsort import natsorted


def copy_labels_from_a_to_b(labels_to_copy, a_file='a.json', b_file='b.json'):
    """
    从 a.json 中复制多个标签到 b.json

    参数:
    labels_to_copy: 要复制的标签名称列表
    a_file: 源文件路径
    b_file: 目标文件路径
    """
    try:
        # 读取源文件
        with open(a_file, 'r', encoding='utf-8') as f:
            data_a = json.load(f)

        # 读取目标文件
        with open(b_file, 'r', encoding='utf-8') as f:
            data_b = json.load(f)

        # 确保 b.json 中有 shapes 列表
        if 'shapes' not in data_b:
            data_b['shapes'] = []

        total_copied = 0
        # 查找并复制所有匹配的标签
        for label in labels_to_copy:
            shapes_to_copy = []
            for shape in data_a.get('shapes', []):
                if shape.get('label') == label:
                    shape_copy = shape.copy()
                    shapes_to_copy.append(shape_copy)

            if shapes_to_copy:
                data_b['shapes'].extend(shapes_to_copy)
                total_copied += len(shapes_to_copy)
                print(f"找到并复制 {len(shapes_to_copy)} 个 '{label}' 标签")

        if total_copied > 0:
            # 保存更新后的文件
            with open(b_file, 'w', encoding='utf-8') as f:
                json.dump(data_b, f, indent=4, ensure_ascii=False)

            print(f"总计复制 {total_copied} 个标签到 {b_file}")
        else:
            print(f"在 {a_file} 中未找到指定的任何标签: {labels_to_copy}")

        return total_copied

    except Exception as e:
        print(f"发生错误: {e}")
        return 0

# 使用示例
if __name__ == "__main__":
    # 复制单个标签
    # copy_labels_from_a_to_b(['zhi'])

    to_base=r"D:\data\jiezhi\det_1201\1201"

    dirs=glob.glob(os.path.join(to_base, "*"))
    dirs=[r"D:\data\jiezhi\det_1201\1201\20251201-201937"]

    for to_dir in dirs:

        dir_name=os.path.basename(to_dir)
        from_dir=rf"D:\data\jiezhi\det_1201\zhi_from\{dir_name}"

        json_from=glob.glob(os.path.join(from_dir, "*.json"))

        json_from=natsorted(json_from)


        json_to=glob.glob(os.path.join(to_dir, "*.json"))

        json_to=natsorted(json_to)

        for json_i, json_file in enumerate(json_from[:len(json_to)]):

            json_to_path=json_to[json_i]

            # 复制多个标签
            copy_labels_from_a_to_b(['zhi'],a_file=json_file,b_file=json_to_path)
            print(json_to_path)
相关推荐
wuweijianlove3 小时前
算法性能的渐近与非渐近行为对比的技术4
算法
_dindong3 小时前
cf1091div2 C.Grid Covering(数论)
c++·算法
AI成长日志3 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
黎阳之光4 小时前
黎阳之光:视频孪生领跑者,铸就中国数字科技全球竞争力
大数据·人工智能·算法·安全·数字孪生
skywalker_114 小时前
力扣hot100-3(最长连续序列),4(移动零)
数据结构·算法·leetcode
6Hzlia4 小时前
【Hot 100 刷题计划】 LeetCode 17. 电话号码的字母组合 | C++ 回溯算法经典模板
c++·算法·leetcode
wfbcg4 小时前
每日算法练习:LeetCode 209. 长度最小的子数组 ✅
算法·leetcode·职场和发展
AI人工智能+5 小时前
基于高精度身份证OCR识别、炫彩活体检测及人脸比对技术的人脸核身系统,为通信行业数字化转型提供了坚实的安全底座
人工智能·计算机视觉·人脸识别·ocr·人脸核身
_日拱一卒5 小时前
LeetCode:除了自身以外数组的乘积
数据结构·算法·leetcode
计算机安禾5 小时前
【数据结构与算法】第36篇:排序大总结:稳定性、时间复杂度与适用场景
c语言·数据结构·c++·算法·链表·线性回归·visual studio