YOLOv5学习实战二:XML文件转化为TXT文件并且归一化用于yolov5训练 python代码

from pathlib import Path
import os
# 进行归一化操作
def convert(size, box): # size:(原图w,原图h) , box:(xmin,xmax,ymin,ymax)
    dw = 1./size[0]     # 1/w
    dh = 1./size[1]     # 1/h
    x = (box[0] + box[1])/2.0   # 物体在图中的中心点x坐标
    y = (box[2] + box[3])/2.0   # 物体在图中的中心点y坐标
    w = box[1] - box[0]         # 物体实际像素宽度
    h = box[3] - box[2]         # 物体实际像素高度
    x = x*dw    # 物体中心点x的坐标比(相当于 x/原图w)
    w = w*dw    # 物体宽度的宽度比(相当于 w/原图w)
    y = y*dh    # 物体中心点y的坐标比(相当于 y/原图h)
    h = h*dh    # 物体宽度的宽度比(相当于 h/原图h)
    return (x, y, w, h)    # 返回 相对于原图的物体中心点的x坐标比,y坐标比,宽度比,高度比,取值范围[0-1]
try:
  import xml.etree.cElementTree as ET
except ImportError:
  import xml.etree.ElementTree as ET
import sys
path = Path('/home/use4test/yl/PCB_DATASET/Annotations/Spurious_copper_z')
file_list=[]
for files in path.rglob('*.xml'):
    file_list.append(files)#存进file_list

for  file in file_list:#循环文件
    tree = ET.parse(file)  #打开xml文档
    root = tree.getroot()  #获得root节点
    # 获得图片的尺寸大小
    size = root.find('size')
    # 获得宽
    w = int(size.find('width').text)
    # 获得高
    h = int(size.find('height').text)
    print("*"*10)
    filename = root.find('filename').text
    filename = filename[:-4]
    path1 = '/home/use4test/yl/PCB_DATASET/Annotations/Spurious_copper_z/' + filename + '.txt'
    for object in root.findall('object'):#找到root节点下的所有object节点,写入坐标信息
      name = object.find('name').text   #子节点下节点name的值
      bndbox = object.find('bndbox')    #子节点下属性bndbox的值
      # 找到bndbox 对象
      xmlbox = bndbox
      # 获取对应的bndbox的数组 = ['xmin','xmax','ymin','ymax']
      b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
           float(xmlbox.find('ymax').text))

      # w = 宽, h = 高, b= bndbox的数组 = ['xmin','xmax','ymin','ymax']
      bb = convert((w, h),b)
      # bb 对应的是归一化后的(x,y,w,h)
      # 生成 calss x y w h 在label文件中
      if name == ("spurious_copper"):  #
          class_id = 5

      with open(path1,"a") as f:  # "a"用于追加内容
          f.write(str(class_id) + " " + " ".join([str(a) for a in bb]) + '\n')
    f.close()
    os.remove(file)
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值