代码拉取完成,页面将自动刷新
import xml.etree.ElementTree as ET
import os
from os import getcwd
os_remove_path = 'E:/Code/Pycharm/015_yolo-for-k210' # 需要根据实际路径进行修改
sets = [('MyImage', 'train'), ('MyImage', 'val'), ('MyImage', 'test')] # 使用自定义数据集
# sets=[('VOCdevkit', 'train'), ('VOCdevkit', 'val'), ('VOCdevkit', 'test')]#使用VOCdevkit数据集
classes = ["patch_board", "shoe", "bottle", "box", "pen", "udisk", "book", "mask", "line", "base", "sock"]
difficulty_set = 1
# 将得到的标注框由左上角右下角的形式变换为中心点和宽高形式,并归一化
def convert(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return x, y, w, h
# voc格式转化为yolo格式,并保存
def convert_annotation(year, image_id):
in_file = open('Train_Image/%s/Annotations/%s.xml' % (year, image_id))
out_file = open('Train_Image/%s/labels/%s.txt' % (year, image_id), 'w')
is_empty = 0 # 默认设置为0,表示文件标注为空
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
if root.find('object') is None:
print("删除以下错误xml文件,请重新生成train.txt val.txt test.txt")
in_file.close()
os.remove(os_remove_path + '/Train_Image/%s/Annotations/%s.xml' % (year, image_id))
print(os_remove_path + '/Train_Image/%s/Annotations/%s.xml' % (year, image_id))
for obj in root.iter('object'):
cls = obj.find('name').text
if cls not in classes: # 如果不包含,直接退出
continue
if obj.find('difficult') is not None: # 优化代码,增加容错性,直接排除不包含difficulty的成分
difficult = obj.find('difficult').text
if int(difficult) == difficulty_set:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
if float(xmlbox.find('xmin').text) < 0 or float(xmlbox.find('xmax').text) < 0:
continue
is_empty = 1
bb = convert((w, h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
if is_empty == 0:
print("删除以下错误xml文件,请重新生成train.txt val.txt test.txt")
in_file.close()
os.remove(os_remove_path + '/Train_Image/%s/Annotations/%s.xml' % (year, image_id))
print(os_remove_path + '/Train_Image/%s/Annotations/%s.xml' % (year, image_id))
if __name__ == '__main__':
wd = getcwd()
for year, image_set in sets:
if not os.path.exists('Train_Image/%s/labels/' % year):
os.makedirs('Train_Image/%s/labels/' % year)
# image_ids = open('Train_Image/%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()
image_ids = open('Train_Image/%s/ImageSets/Main/%s.txt' % (year, image_set)).read().strip().split()
# 在项目主目录中生成训练集、验证集、测试集图片文件绝对路径
list_file = open('%s_%s.txt' % (year, image_set), 'w')
for image_id in image_ids:
list_file.write('%s\\Train_Image\\%s\\JPEGImages\\%s.jpg\n' % (wd, year, image_id))
convert_annotation(year, image_id)
list_file.close()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。