Python文件操作之批量生成已标注好的.xml、.json

1. 批量生成.xml并重命名

在深度学习进行数据标注时,重复位置的标签可以通过提前批量复制来解决,而对于此python表现出了很好的应用

#批量赋值图像的标注文件.xml并重命名为图像名字

import os
import shutil

imgspath="file/imgs"
xmlpath="file/imgs/20220902000273.xml"

for root,dirs,files in os.walk(imgspath,topdown=True):
    for name in files:
        str=name.split('.')
        if(str[-1] == 'jpg'):
            newname=str[0]+'.xml'
            newxml=os.path.join(root,newname)
            print(newxml)
            if not os.path.exists(newxml):
                shutil.copy(xmlpath,newxml)

2. 批量生成.json并重命名

'''
批量复制json自动打标
'''


import os
import io
import shutil
import json
import base64
import PIL
from PIL import Image
import numpy


def img_arr_to_b64(img_arr):
    img_pil = Image.fromarray(img_arr)
    f = io.BytesIO()
    img_pil.save(f, format="JPEG")
    img_bin = f.getvalue()
    if hasattr(base64, "encodebytes"):
        img_b64 = base64.encodebytes(img_bin)
    else:
        img_b64 = base64.encodestring(img_bin)
    img_b64 = str(img_b64).replace(r'\n', '')
    img_b64 = str(img_b64).replace(r"b'", '')
    img_b64 = str(img_b64).replace(r"'", '')
    return img_b64


def walk_dir_and_copy(dir: str, label: str, dst_label: str):
    '''
    dir: 照片所在路径
    label: 待复制的标签文件的路径
    dst_label: 复制后标签文件存放路径
    '''
    fileNameList = []
    filePathList = []
    for root, dir, files in os.walk(dir):
        for file in files:
            if os.path.splitext(file)[-1] == '.jpg':
                fileNameList.append(file.split('.jpg')[0])
                filePathList.append(os.path.join(root, file))

    for (filename, file) in zip(fileNameList, filePathList):
        print(filename)
        with open(label, 'r', encoding='utf-8') as f:
            info = json.load(f)
            info['imagePath'] = filename + '.jpg'

            img_pil = Image.open(file)
            img_pil = numpy.array(img_pil)
            info['imageData'] = str(img_arr_to_b64(img_pil))
            info['imageHeight'] = img_pil.shape[0]
            info['imageWidth'] = img_pil.shape[1]

            dst = os.path.join(dst_label, filename + '.json')
            with open(dst, 'w', encoding="utf-8") as d:
                info = json.dumps(info, indent=4)
                d.write(info)


if __name__ == '__main__':
    dir = r"./imgs"		# 原图所在目录
    label = r"./imgs/050.json"  # 模板json所在目录
    dst = r"./imgs"	# json的输出目录
    walk_dir_and_copy(dir, label, dst)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明月醉窗台

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值