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)