自建数据集系列:从labelImg格式->txt格式(YOLO格式、ICDAR2015格式)

前言

xml格式虽然在检测领域是比较常用,但是吧也并非绝对

xml转YOLO格式

xml格式

<?xml version="1.0" ?><annotation>
	<folder>JPEGImages</folder>
	<filename>000000.jpg</filename>
	<path>E:\dataset\camo\JPEGImages\000000.jpg</path>
	<source>
		<database>Unknown</database>
	</source>
	<size>
		<width>500</width>
		<height>282</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>person</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>170</xmin>
			<ymin>112</ymin>
			<xmax>223</xmax>
			<ymax>232</ymax>
		</bndbox>
	</object>
</annotation>

txt格式

0 0.391 0.6063829787234042 0.106 0.425531914893617
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import os

sets = ['train', 'val', 'test']
# classes = ["a", "b"]   # 改成自己的类别
classes = ['person']  # class names
abs_path = os.getcwd()


def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = (box[0] + box[1]) / 2.0 - 1
    y = (box[2] + box[3]) / 2.0 - 1
    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


def convert_annotation(image_id):
    in_file = open(abs_path + '/Annotations/%s.xml' % (image_id), encoding='UTF-8')
    out_file = open(abs_path + '/labels/%s.txt' % (image_id), 'w')
    tree = ET.parse(in_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
        # 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))
        b1, b2, b3, b4 = b
        # 标注越界修正
        if b2 > w:
            b2 = w
        if b4 > h:
            b4 = h
        b = (b1, b2, b3, b4)
        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')


for image_set in sets:
    if not os.path.exists(abs_path + '/labels/'):
        os.makedirs(abs_path + '/labels/')

    image_ids = open(abs_path + '/ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
    list_file = open(abs_path + '/%s.txt' % (image_set), 'w')
    for image_id in image_ids:
        list_file.write(abs_path + '/JPEGImages/%s.jpg\n' % (image_id))
        convert_annotation(image_id)
    list_file.close()

xml转ICDAR2015格式

ICDAR格式主要用于文字检测识别领域,多为四点框
在这里插入图片描述

原始xml:labelImg的标注

<annotation>
	<folder>数据集jpg</folder>
	<filename>81.jpg</filename>
	<path>C:\Users\cam_robot\Desktop\28船\船名字符数据\数据集jpg\81.jpg</path>
	<source>
		<database>Unknown</database>
	</source>
	<size>
		<width>1267</width>
		<height>765</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>44106</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>197</xmin>
			<ymin>488</ymin>
			<xmax>312</xmax>
			<ymax>521</ymax>
		</bndbox>
	</object>
	<object>
		<name>CHINA</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>606</xmin>
			<ymin>514</ymin>
			<xmax>671</xmax>
			<ymax>541</ymax>
		</bndbox>
	</object>
	<object>
		<name>COAST</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>679</xmin>
			<ymin>515</ymin>
			<xmax>747</xmax>
			<ymax>543</ymax>
		</bndbox>
	</object>
	<object>
		<name>GUARD</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>757</xmin>
			<ymin>518</ymin>
			<xmax>824</xmax>
			<ymax>546</ymax>
		</bndbox>
	</object>
</annotation>

目标结果:icdar2015的txt形式

197.0,488.0,312.0,488.0,312.0,521.0,197.0,521.0,44106
606.0,514.0,671.0,514.0,671.0,541.0,606.0,541.0,CHINA
679.0,515.0,747.0,515.0,747.0,543.0,679.0,543.0,COAST
757.0,518.0,824.0,518.0,824.0,546.0,757.0,546.0,GUARD

转换脚本:xml2txt.py

import xml.etree.ElementTree as ET
import os

def dealXml(xmlPath):
    tree = ET.parse(xmlPath)
    root = tree.getroot()  #获取根节点,此处是<Annotion>的节点
    filename = root.find('filename').text  #通过find节点再text获取文本time1.jpg
    
    lists = []
    for obj in root.findall('object'):  #获取所有名为'object'的直接子节点
        lineList = []
        for attr in list(obj): #list出Object的所有直接子节点
            if 'bndbox'in attr.tag:   #判断节点标签
                x1 = float(attr.find('xmin').text) 
                y1 = float(attr.find('ymin').text)
                x2 = float(attr.find('xmax').text)
                y2 = float(attr.find('ymin').text)
                x3 = float(attr.find('xmax').text)
                y3 = float(attr.find('ymax').text)
                x4 = float(attr.find('xmin').text)
                y4 = float(attr.find('ymax').text)

                lineList = [x1,y1,x2,y2,x3,y3,x4,y4] + lineList
            if attr.tag=="name":
                label = attr.text
                lineList.append(label)
        lists.append(lineList)
    return lists

def saveTxt(xmlPath):
    lists = dealXml(xmlPath)
    if len(lists)>0:
        txtPath = os.path.splitext(xmlPath)[0]+".txt"
        with open(txtPath,mode='w', encoding='UTF-8') as f:
            for lineList in lists:
                for item in lineList:
                    if item!= lineList[-1]:
                        f.write(str(item))
                        f.write(",")
                    else:
                        f.write(str(item))
                        f.write("\n")

if __name__=='__main__':
    path = r"C:\Users\cam_robot\Desktop\船名数据集(SEU401)\LabelImgs"
    for root, dirs, files in os.walk(path, topdown=False):
        for file in files:
            portion = os.path.splitext(file)
            if portion[1]==".xml":
                saveTxt(os.path.join(path,file))

🔰 汇总 🔰

🔷1.从labelImg格式->txt格式(YOLO格式、ICDAR2015格式)

2.从二值mask->labelme格式->coco格式

3.从labelme格式->VOC格式+从二值mask->VOC格式

4.从RGB->二值mask->coco格式

5.实例分割mask->语义分割mask->扩增mask

6.COCO格式->YOLO格式

双模图片数据与对应标注文件的命名对齐

xml标注文件的节点、属性、文本的修正

cocoJson数据集统计分析

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空•物语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值