使用labelimg标注的xml文件转化为yolov5所需的txt文件

1 篇文章 0 订阅
1 篇文章 0 订阅

yolov5所需的格式如下图:
第一位是类别的索引,,,然后是归一化后的中心点x,y,宽高w,h。
coco
转化代码如下:
GT_PATH路径下放所有的xml文件

# -*- coding: utf-8 -*-
"""
Time    : 2022/5/14 17:18
Author  : cong
"""
import sys
import os
import glob
import xml.etree.ElementTree as ET

names = ['hatch', 'cargo', 'aeroplane']
GT_PATH = 'datasets/coco128/labels/train2017/'
#print(GT_PATH)
os.chdir(GT_PATH)
xml_list = glob.glob('*.xml')
if not os.path.exists("backup"):
    os.makedirs("backup")
for tmp_file in xml_list:
    #print(tmp_file)
    # 1. create new file (VOC format)

    with open(tmp_file.replace(".xml", ".txt"), "a") as new_f:

        root = ET.parse(tmp_file).getroot()
        size = root.find('size')
        for obj in root.findall('object'):
          obj_name = obj.find('name').text
          obj_index = names.index(obj_name)
          bndbox = obj.find('bndbox')
          image_w = int(size.find('width').text)
          image_h = int(size.find('height').text)
          x_min = int(bndbox.find('xmin').text)
          x_max = int(bndbox.find('xmax').text)
          y_min = int(bndbox.find('ymin').text)
          y_max = int(bndbox.find('ymax').text)
          x = ((x_min + x_max)/2)/image_w
          y = ((y_min + y_max)/2)/image_h
          w = (x_max - x_min) /image_w
          h = (y_max - y_min) /image_h
          new_f.write("%d %s %s %s %s\n" % (obj_index, x, y, w, h))
    # 2. move old file (xml format) to backup
    os.rename(tmp_file, os.path.join("backup", tmp_file))
print("Conversion completed!")


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值