当前位置:网站首页>Labelme VOC data format to Yolo TXT data format
Labelme VOC data format to Yolo TXT data format
2022-07-21 09:02: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__":
# hold forklift_pallet Of voc Of xml The label file is converted to yolo Of txt Label file
# 1、 Categories to be converted
classes = [ ]
# 2、voc Format xml Tag file path
xml_files1 = r'E:\\original-data'
# 3、 Turn into yolo Format txt Label file storage path
save_txt_files1 = r'E:\\original-data'
convert_annotation(xml_files1, save_txt_files1, classes)
边栏推荐
- Research on the method of weight judgment of BFS node in path finding problem
- The fathers of programming languages are too bored to retire and choose to return to the workplace
- PHP penetration test files contain vulnerabilities and methods of exploitation
- 四.uni-app组件[视图组件、基本内容(官方自带例如表单类)、UI组件库、组件库的坑]
- 三伏天的食物
- An application example of backtracking method -- prime number ring problem
- uniapp中登录和支付整理
- Win system operation and maintenance
- Win系统运维
- js 判断对象中的每个键是否有值
猜你喜欢
论文写作全攻略|一篇学术科研论文该怎么写
Looking back from the eight queens problem (violent enumeration)
这款IDE插件3.0让你成为公司最懂安全的程序员
App automated test -1 Appium installation and configuration
7天交付沈阳方舱医院项目,这就是鸿雁速度
Deliver Shenyang fangcang hospital project in 7 days, which is Hongyan speed
Fraud detection using CSP
ウクアージ / 小黑
三伏天的食物
中国石墨上市:市值近12亿港元 实现鹤岗民营企业上市零突破
随机推荐
VB.NET插件开发案例
ICML 2022 | 解决CNN固有缺陷, CCNN凭借单一架构,实现多项SOTA
斯坦福、Meta AI新研究:实现AGI之路,数据剪枝比我们想象得更重要
Taobao / tmall: description of API return value of bestseller list
ICML2022奖项公布:15篇杰出论文,复旦、厦大、上交大研究入选
Dependency injection
硬核Fiddler抓包工具大型攻略(二)Fiddler进阶篇
Scala advanced (VII): collection content summary (Part 1)
排序与检索(归并/快排/二分)
今日来介绍关于淘宝相似商品搜索的API接口
Klocwork部署的安全最佳实践
Arrow builder
程序员老王给自己的孩子取名字
暑假打工 2 个月,让我明白了 Keepalived 高可用的三种路由方案
APP自动化测试-3. Appium元素定位与等待
Latex笔记
基于CLIP的色情图片识别;油管最新ML课程大合集;交互式编写shell管道;机器人仓库环境增量感知数据集;最新AI论文 | ShowMeAI资讯日报
EfCore 表关系一对一,使用案例
Is the higher the yield of financial products, the better?
敏实集团IPO被终止:曾拟募资65亿 秦荣华曾遭法院处罚