目标检测数据集格式转换yolo→voc
目标检测数据集格式转换(yolo→voc)
拿到的数据集标注格式为yolo,不过空格很奇怪:
标签 坐标1 坐标2 坐标3 坐标4 (标签后面一个空格 坐标之间两个空格)
现成的转换代码不能用,自己写一个
import os
import glob
from PIL import Image
from tqdm import tqdm
voc_annotations = r"E:\Master\dataset_person\dataset_person\dataset2\xml_labels/" #存放的xml文件地址
yolo_txt = r"E:\Master\dataset_person\dataset_person\dataset2\labels\train2017/" #yolo数据集标签文件地址
img_path = r"E:\Master\dataset_person\dataset_person\dataset2\images\train2017/" #yolo数据集图片地址
labels = ['people'] #darknet数据集的类别
# 图像存储位置
src_img_dir = img_path
# 图像的txt文件存放位置
src_txt_dir = yolo_txt
src_xml_dir = voc_annotations
img_Lists = glob.glob(src_img_dir '/*.jpg')
img_basenames = []
for item in img_Lists:
img_basenames.append(os.path.basename(item))
img_names = []
for item in img_basenames:
temp1, temp2 = os.path.splitext(item)
img_names.append(temp1)
for img in tqdm(img_names):
im = Image.open((src_img_dir '/' img '.jpg'))
width, height = im.size
# 打开txt文件
gt = open(src_txt_dir '/' img '.txt').read().splitlines()
# print(gt)
if gt:
# 将主干部分写入xml文件中
xml_file = open((src_xml_dir '/' img '.xml'), 'w')
xml_file.write('<annotation>\n')
xml_file.write(' <folder>VOC2007</folder>\n')
xml_file.write(' <filename>' str(img) '.jpg' '</filename>\n')
xml_file.write(' <size>\n')
xml_file.write(' <width>' str(width) '</width>\n')
xml_file.write(' <height>' str(height) '</height>\n')
xml_file.write(' <depth>3</depth>\n')
xml_file.write(' </size>\n')
# write the region of image on xml file
for img_each_label in gt: # txt 文件中的每一行
spt = img_each_label.split(' ') # 这里如果txt里面是以逗号‘,’隔开的,那么就改为spt = img_each_label.split(',')。
#print(f'spt:{spt}')
spt1 = spt[0].split(' ')
#print(f'spt1:{spt1}')
for i in range(1, 4):
spt1.append(spt[i])
#print(f'spt1:{spt1}')
spt = spt1
# print(f'spt:{spt}')
xml_file.write(' <object>\n')
xml_file.write(' <name>' str(labels[int(spt[0])]) '</name>\n')
xml_file.write(' <pose>Unspecified</pose>\n')
xml_file.write(' <truncated>0</truncated>\n')
xml_file.write(' <difficult>0</difficult>\n')
xml_file.write(' <bndbox>\n')
#print(spt)
center_x = round(float(spt[1].strip()) * width)
#print(spt[2])
center_y = round(float(spt[2].strip()) * height)
bbox_width = round(float(spt[3].strip()) * width)
bbox_height = round(float(spt[4].strip()) * height)
xmin = str(int(center_x - bbox_width / 2))
ymin = str(int(center_y - bbox_height / 2))
xmax = str(int(center_x bbox_width / 2))
ymax = str(int(center_y bbox_height / 2))
xml_file.write(' <xmin>' xmin '</xmin>\n')
xml_file.write(' <ymin>' ymin '</ymin>\n')
xml_file.write(' <xmax>' xmax '</xmax>\n')
xml_file.write(' <ymax>' ymax '</ymax>\n')
xml_file.write(' </bndbox>\n')
xml_file.write(' </object>\n')
xml_file.write('</annotation>')
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgcekhh
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13