xml2txt

-*- coding: utf-8 -*-

import xml.etree.ElementTree as ET

import os

from os import getcwd

sets = ['train', 'val', 'test']

classes = ["person"] # 改成自己的类别

abs_path = os.getcwd()

print(abs_path)

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('data_prov2/xml/train/%s.xml' % (image_id), encoding='UTF-8')

out_file = open('data_prov2/labels/train/%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('data_prov2/labels/train/'):

os.makedirs('data_prov2/labels/train/')

image_ids = open('data_prov2/dataSet/%s.txt' % (image_set)).read().strip().split()

list_file = open('data_prov2/%s.txt' % (image_set), 'w')

for image_id in image_ids:

list_file.write(abs_path + 'data_prov2/images/train/%s.jpg\n' % (image_id))

convert_annotation(image_id)

list_file.close()

相关推荐
kebijuelun1 天前
ERNIE 5.0:统一自回归多模态与弹性训练
人工智能·算法·语言模型·transformer
Network_Engineer1 天前
从零手写LSTM:从门控原理到PyTorch源码级实现
人工智能·pytorch·lstm
芝士爱知识a1 天前
AlphaGBM 深度解析:下一代基于 AI 与蒙特卡洛的智能期权分析平台
数据结构·人工智能·python·股票·alphagbm·ai 驱动的智能期权分析·期权
weixin_6681 天前
GitHub 2026年AI项目热度分析报告-AI分析-分享
人工智能·github
vlln1 天前
【论文速读】达尔文哥德尔机 (Darwin Gödel Machine): 自进化智能体的开放式演化
人工智能·深度学习·ai agent
Katecat996631 天前
目标检测咖啡果实成熟度检测:RetinaNet-X101模型实现
人工智能·目标检测·目标跟踪
AAD555888991 天前
基于Mask_RCNN的猫科动物目标检测识别模型实现与分析
人工智能·目标检测·计算机视觉
Katecat996631 天前
基于YOLOv8和MAFPN的骆驼目标检测系统实现
人工智能·yolo·目标检测
合力亿捷-小亿1 天前
2026年AI语音机器人测评推荐:复杂噪声环境下语义识别准确率对比分析
人工智能·机器人
子夜江寒1 天前
基于 LSTM 的中文情感分类项目解析
人工智能·分类·lstm