数据标注篇—从labelimg中提取目标框的坐标

1、提取图片的名字

import random
import os
import xml.etree.ElementTree as ET

# 需要修改  标签的名字
XML_FILE_PATH = "./labels/"
SAVE_BASE_PATH = "./saves/"

temp_xml = os.listdir(XML_FILE_PATH)
total_xml = []
for xml in temp_xml:
    if xml.endswith(".xml"):
        total_xml.append(xml)

num = len(total_xml)
list = range(num)
tv = int(num)
tvs = random.sample(list, tv)

if not os.path.exists(SAVE_BASE_PATH):
    os.makedirs(SAVE_BASE_PATH)
ftvs = open(os.path.join(SAVE_BASE_PATH, 'images.txt'), 'w')

for i in list:
    name = total_xml[i][:-4]+'\n'
    if i in tvs:
        ftvs.write(name)

ftvs.close()

2、提前目标框的坐标

import xml.etree.ElementTree as ET
import os


# 需要修改 图片的所放的文件名称
sets = [("images")]
# 需要修改 图片里面的类别标签
classes = ["area"]
IMAGE_PATH = "./saves/{}.txt"
DATA_TXT = "./saves/"
# 需要修改 图片写入的路径 以及图片名称后缀
IMAGE_WRITE_PATH = "images/{}.jpg"
# 需要修改 图片所对应的标签的路径
OPEN_XML_PATH = "labels/{}.xml"

def convert_annotation(image_id, list_file):
    in_file = open(OPEN_XML_PATH.format(image_id))
    tree = ET.parse(in_file)
    root = tree.getroot()
    list_file.write(IMAGE_WRITE_PATH.format(image_id))
    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 = (int(xmlbox.find('xmin').text), int(xmlbox.find('ymin').text), int(xmlbox.find('xmax').text), int(xmlbox.find('ymax').text))
        list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id))

    list_file.write('\n')


for image_set in sets:
    image_ids = open(IMAGE_PATH.format(image_set)).read().strip().split()
    save_data_path = '/'.join(DATA_TXT.split('/')[:-1])
    if not os.path.exists(save_data_path):
        os.makedirs(save_data_path)
    list_file = open(os.path.join(DATA_TXT, "images_final.txt"), 'w')
    for image_id in image_ids:
        convert_annotation(image_id, list_file)
    list_file.close()

结果如下

images.txt
在这里插入图片描述
images_final.txt
在这里插入图片描述

GitHub 加速计划 / la / labelImg
22.31 K
6.24 K
下载
🎉 超级实用!LabelImg,图像标注神器,现在加入Label Studio社区,享受多模态数据标注新体验!🚀 简单易用,支持XML、YOLO和CreateML格式,适用于ImageNet等项目。不再单独维护,立即尝试Label Studio,安装一键到位,更灵活,功能更强大!👇 安装即刻开始:pip3 install labelImg,或访问<https://github.com/heartexlabs/label-studio> 获取源码构建。一起探索数据标注的新边界!👨‍💻👩‍💻【此简介由AI生成】
最近提交(Master分支:2 个月前 )
b33f965b Adds information about Label Studio community to welcome LabelImg users 2 年前
2d5537ba 2 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐