【目标检测】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)
相关推荐
广州正荣7 分钟前
Scrapy-Redis分布式爬虫架构的可扩展性与容错性增强:基于微服务与容器化的解决方案
人工智能·爬虫·科技
加油搞钱加油搞钱9 分钟前
鹰盾加密器基于AI的视频个性化压缩技术深度解析:从智能分析到无损压缩实践
人工智能·音视频·视频加密·鹰盾加密·鹰盾播放器
Baihai_IDP11 分钟前
OCR 识别质量如何影响 RAG 系统的性能?有何解决办法?
人工智能·llm·aigc
gsls20080811 分钟前
使用xdocreport导出word
前端·python·word
新智元12 分钟前
20 人团队提前实现 DeepSeek 构想,AI 算力变天?直击大模型算力成本痛点
人工智能·openai
硬核隔壁老王18 分钟前
从零开始搭建RAG系统系列(十):RAG系统性能优化技巧-生成模块优化 (Optimizing Generator)
人工智能·程序员·llm
机器之心23 分钟前
刚刚,OpenAI正式发布o3-pro!奥特曼激动更新博客:温和的奇点
人工智能·ai编程
硬核隔壁老王26 分钟前
从零开始搭建RAG系统系列(九):RAG系统性能优化技巧-检索模块优化 (Optimizing Retriever)
人工智能·程序员·llm
LLM大模型27 分钟前
LangChain篇-消息管理与聊天历史存储
人工智能·程序员·llm
通义灵码35 分钟前
通义灵码 AI IDE 上线!智能体+MCP 从手动调用工具过渡到“AI 主动调度资源”
ide·人工智能·阿里云·通义灵码