当前位置:网站首页>labelme voc数据格式转yolo txt数据格式
labelme voc数据格式转yolo txt数据格式
2022-07-20 13:37:00 【Christo3】
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
def convert(size, box):
# size=(width, height) b=(xmin, xmax, ymin, ymax)
# x_center = (xmax+xmin)/2 y_center = (ymax+ymin)/2
# x = x_center / width y = y_center / height
# w = (xmax-xmin) / width h = (ymax-ymin) / height
x_center = (box[0] + box[1]) / 2.0
y_center = (box[2] + box[3]) / 2.0
x = x_center / size[0]
y = y_center / size[1]
w = (box[1] - box[0]) / size[0]
h = (box[3] - box[2]) / size[1]
# print(x, y, w, h)
return (x, y, w, h)
def convert_annotation(xml_files_path, save_txt_files_path, classes):
xml_files = os.listdir(xml_files_path)
# print(xml_files)
for xml_name in xml_files:
# print(xml_name)
xml_file = os.path.join(xml_files_path, xml_name)
out_txt_path = os.path.join(save_txt_files_path, xml_name.split('.')[0] + '.txt')
out_txt_f = open(out_txt_path, 'w')
tree = ET.parse(xml_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
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))
# b=(xmin, xmax, ymin, ymax)
# print(w, h, b)
bb = convert((w, h), b)
out_txt_f.write(str(cls) + " " + " ".join([str(a) for a in bb]) + '\n')
if __name__ == "__main__":
# 把forklift_pallet的voc的xml标签文件转化为yolo的txt标签文件
# 1、需要转化的类别
classes = [ ]
# 2、voc格式的xml标签文件路径
xml_files1 = r'E:\\original-data'
# 3、转化为yolo格式的txt标签文件存储路径
save_txt_files1 = r'E:\\original-data'
convert_annotation(xml_files1, save_txt_files1, classes)
边栏推荐
猜你喜欢
APP自动化测试-2. Appium录制测试用例
Google 为造芯再掀“抢人大战”,英特尔 17 年老将加入
Practice upgrade! Chuangyu security trusteeship helps you solve the problem of directional blasting defense
基于CLIP的色情图片识别;油管最新ML课程大合集;交互式编写shell管道;机器人仓库环境增量感知数据集;最新AI论文 | ShowMeAI资讯日报
Android kotlin uses arouter componentized routing and datastore to save data instead of SharedPreferences
How to write the docker cleaning cache script
App automated test-2 Appium recording test cases
App automated test -3 Appium element positioning and waiting
The fathers of programming languages are too bored to retire and choose to return to the workplace
7天交付沈阳方舱医院项目,这就是鸿雁速度
随机推荐
Hj17 coordinate movement
淘宝/天猫按分类搜索淘宝直播接口 API 使用说明
MySQL基础(多表查询、事务)
Android kotlin uses arouter componentized routing and datastore to save data instead of SharedPreferences
全网追杀“钱包刺客”
多进程中fork
3D point cloud course (V) - in depth learning
docker清理缓存脚本怎么写
乐扣乐扣澄清欠税事件:不存在欠税,将一如既往合规经营
MySQL Foundation (multi table query, transaction)
软件测试技术之可用性测试之WhatsApp Web
Scala 高阶(七):集合内容汇总(上篇)
The electricity load of many provinces has reached a record high, and efforts have been made to ensure the safety of energy supply in summer
MySQL基础篇(概念、常用指令)
JS determines whether each key in the object has a value
How to implement JS template compilation
探究路径寻找问题BFS结点的判重方法
败走IPO的年轻人
面试题
如何关闭页面之前清空LocalStorage