【目标检测】VOC格式xml标注转换为DOTAv1格式txt标注

  1. voc2dota
python 复制代码
import os
import xml.etree.ElementTree as ET

def convert_voc_to_dota(xml_folder, output_folder):
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    
    for xml_file in os.listdir(xml_folder):
        if xml_file.endswith('.xml'):
            tree = ET.parse(os.path.join(xml_folder, xml_file))
            root = tree.getroot()
            
            # 初始化DOTA格式的字符串
            dota_annotations = []
            
            for obj in root.iter('object'):
                robndbox = obj.find('robndbox')
                category = obj.find('name').text
                difficult = obj.find('difficult').text
                
                # 获取顶点坐标,并转换为DOTA格式(使用空格分隔)
                coords = [
                    robndbox.find('x_left_top').text, robndbox.find('y_left_top').text,
                    robndbox.find('x_right_top').text, robndbox.find('y_right_top').text,
                    robndbox.find('x_right_bottom').text, robndbox.find('y_right_bottom').text,
                    robndbox.find('x_left_bottom').text, robndbox.find('y_left_bottom').text
                ]
                dota_format = ' '.join(coords + [category, difficult])
                dota_annotations.append(dota_format)
            
            # 写入转换后的信息到TXT文件
            output_file_path = os.path.join(output_folder, xml_file.replace('.xml', '.txt'))
            with open(output_file_path, 'w') as f:
                for annotation in dota_annotations:
                    f.write("%s\n" % annotation)

# 调用函数,传入XML文件夹路径和输出文件夹路径
xml_folder = 'path/to/xml/folder'
output_folder = 'path/to/output/folder'
convert_voc_to_dota(xml_folder, output_folder)
  1. 可视化dota数据集
python 复制代码
import cv2
import numpy as np
import os
 
 
def draw_rotated_box(img, box, label):
    """在图像上绘制旋转的边界框和标签。"""
    points = np.int0(box)
    cv2.drawContours(img, [points], 0, (0, 255, 0), 2)  # 绘制旋转框
    cv2.putText(img, label, tuple(points[0]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)  # 添加文本标签
 
 
def visualize_dota_annotations(image_folder, annotation_folder, output_folder):
    """批量处理图像和DOTA标注文件,绘制旋转边界框和标签"""
    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
 
    # 遍历图像文件
    for img_filename in os.listdir(image_folder):
        img_path = os.path.join(image_folder, img_filename)
        if os.path.isfile(img_path) and img_filename.endswith(('.jpg', '.png')):
            annot_filename = os.path.splitext(img_filename)[0] + '.txt'
            annot_path = os.path.join(annotation_folder, annot_filename)
            output_img_path = os.path.join(output_folder, img_filename)
 
            img = cv2.imread(img_path)
            if img is None:
                continue
 
            if os.path.isfile(annot_path):
                with open(annot_path, 'r') as f:
                    lines = f.readlines()  # Skip imagesource and gsd lines
                    for line in lines:
                        parts = line.strip().split(' ')
                        if len(parts) < 9:
                            continue
                        box = np.array([float(part) for part in parts[:8]]).reshape(4, 2)
                        label = parts[8]
                        draw_rotated_box(img, box, label)
 
            cv2.imwrite(output_img_path, img)
 
# 路径配置
image_folder = 'images'
annotation_folder = 'dota'
output_folder = 'visual'
 
visualize_dota_annotations(image_folder, annotation_folder, output_folder)
相关推荐
飞猫的边缘AI几秒前
边缘AI时事:从豆包手机二代和Rokid×WorkBuddy联名AI眼镜看AI智能体进终端的必然与边界
人工智能·ai智能体·rokid眼镜·workbuddy·豆包手机二代
艾醒(AiXing-w)1 分钟前
LangChain 1.0 智能体开发(三):Agent 记忆管理——从短期对话到跨会话长期记忆
数据库·人工智能·langchain
知几蜗牛2 分钟前
部署大模型别先选GPU,先回答你愿意承担多少运维
人工智能
知几蜗牛3 分钟前
AI写了80万行Rust,最值得学的却是它花十倍精力读代码
人工智能
天云数据4 分钟前
OPC保姆级指南:被优化的第四个月,我在图书馆里想好了开家公司
人工智能
知几蜗牛4 分钟前
语音AI为什么总抢话?用VAD和打断机制做对实时对话
人工智能
fellow995 分钟前
V100 的上下文极限:vLLM 卡 131K,llama.cpp 冲 230K
人工智能·自然语言处理
AgentMaster6 分钟前
数据资产化落地难题:5款数据中台系统架构对比与实施记录
大数据·人工智能·算法
AIGCmagic社区11 分钟前
LightNav-0:激发VLM空间智能,迈向通用具身导航
人工智能·具身智能·ai多模态
知几蜗牛13 分钟前
AI每次提交都查漏洞,真正的升级是把证明链放进评审
人工智能