import os, sys
import glob
from PIL import Image
# 图像存储位置
src_img_dir = "datasets/x-ray/train/JPEGImages"
# 图像的 ground truth 的 txt 文件存放位置
src_txt_dir = "datasets/x-ray/train/Annotations/txt"
src_xml_dir = "datasets/x-ray/train/Annotations"
img_Lists = glob.glob(src_img_dir + '/*.jpg')
img_basenames = [] # e.g. 100.jpg
for item in img_Lists:
img_basenames.append(os.path.basename(item))
img_names = [] # e.g. 100
for item in img_basenames:
temp1, temp2 = os.path.splitext(item)
img_names.append(temp1)
for img in img_names:
im = Image.open((src_img_dir + '/' + img + '.jpg'))
width, height = im.size
# open the crospronding txt file
gt = open(src_txt_dir + '/' + img + '.txt', 'r', encoding='UTF-8').read().splitlines()
# gt = open(src_txt_dir + '/gt_' + img + '.txt').read().splitlines()
# write in xml file
# os.mknod(src_xml_dir + '/' + img + '.xml')
xml_file = open((src_xml_dir + '/' + img + '.xml'), 'w', encoding='UTF-8')
xml_file.write('\n')
xml_file.write(' JPEGImages\n')
xml_file.write(' ' + str(img) + '.jpg' + '\n')
xml_file.write(' \n')
xml_file.write(' ' + str(width) + '\n')
xml_file.write(' ' + str(height) + '\n')
xml_file.write(' 3\n')
xml_file.write(' \n')
# write the region of image on xml file
i = 1
for img_each_label in gt:
spt = img_each_label.split(' ') # 这里如果txt里面是以逗号‘,’隔开的,那么就改为spt = img_each_label.split(',')。
# print(spt)
# spt打印['Images/s1.jpg', '391', '243', '428', '355', '426', '249', '468', '367', '']
imax = len(spt)
for i in range(0, imax - 1 - 4, 4):
# print(str(spt[i + 1]))
xml_file.write(' \n')
xml_file.write(' lighter\n')
xml_file.write(' Unspecified\n')
xml_file.write(' 0\n')
xml_file.write(' 0\n')
xml_file.write(' \n')
xml_file.write(' ' + str(spt[i + 2]) + '\n')
xml_file.write(' ' + str(spt[i + 3]) + '\n')
xml_file.write(' ' + str(spt[i + 4]) + '\n')
xml_file.write(' ' + str(spt[i + 5]) + '\n')
xml_file.write(' \n')
xml_file.write(' \n')
xml_file.write('')
以上代码是txt的标注文件转成voc格式数据的代码示例,有需要的朋友可以自取。
收藏
点赞
0
个赞
请登录后评论
TOP
切换版块
互相学习,刚好看到的
多谢大佬指点~~~~
昨天刚好看到这段代码,转voc的,不是手打空格,用的\t
https://github.com/DLLXW/data-science-competition/blob/main/%E5%A4%A9%E6%B1%A0/2021%E5%B9%BF%E4%B8%9C%E5%B7%A5%E4%B8%9A%E6%99%BA%E9%80%A0%E5%88%9B%E6%96%B0%E5%A4%A7%E8%B5%9B/convert_to_voc.py
如果是不规则标注,又是txt格式的话,txt转voc代码还挺重要的,不过就是要修改下了
不过看起来,这是手动空格?
点赞,感谢分享
好吧,没人参考,还是自己随便看下吧。o(* ̄︶ ̄*)o