【目标检测】旋转目标检测COCO格式标注转DOTAv1格式

DOTAv1数据集格式:

'imagesource':imagesource

'gsd':gsd

x1, y1, x2, y2, x3, y3, x4, y4, category, difficult

x1, y1, x2, y2, x3, y3, x4, y4, category, difficult

...
imagesource: 图片来源

gsd: 分辨率

x1, y1, x2, y2, x3, y3, x4, y4:四边形的四个顶点的坐标 顶点按顺时针顺序排列,第一个起点为左上第一个点

category:实例类别

difficult:表示该实例是否难以检测(1表示困难,0表示不困难)

COCO转DOTA:

python 复制代码
import json
import cv2
import numpy as np
import os

def calculate_rotated_bbox(poly):
    """将多边形坐标转换为旋转边界框"""
    contour = np.array(poly).reshape((-1, 1, 2)).astype(np.float32)
    rect = cv2.minAreaRect(contour)
    box = cv2.boxPoints(rect)
    return np.int0(box)

def coco_to_dota(coco_annotation_path, dota_annotation_folder, imagesource="Unknown", gsd="Unknown"):
    """将COCO格式的标注转换为DOTA格式,包括imagesource和gsd信息"""
    # 类别ID到名称的映射
    category_map = {
        1: 'Class1',
        2: 'Class2',
    }

    # 确保输出目录存在
    if not os.path.exists(dota_annotation_folder):
        os.makedirs(dota_annotation_folder)

    # 读取COCO格式的JSON文件
    with open(coco_annotation_path, 'r') as f:
        coco_data = json.load(f)

    # 遍历每个图像的标注
    for image in coco_data['images']:
        image_id = image['id']
        image_filename = image['file_name']
        dota_filename = os.path.splitext(image_filename)[0] + '.txt'  # 去掉原始扩展名,添加.txt
        dota_filepath = os.path.join(dota_annotation_folder, dota_filename)

        with open(dota_filepath, 'w') as dota_file:
            # 写入imagesource和gsd信息
            # dota_file.write(f"'imagesource':{imagesource}\n'gsd':{gsd}\n")

            # 找到当前图像的所有标注
            for annotation in filter(lambda x: x['image_id'] == image_id, coco_data['annotations']):
                if 'segmentation' in annotation:
                    for seg in annotation['segmentation']:
                        if type(seg[0]) is list:  # 检查是否是多边形格式
                            seg = seg[0]
                        box = calculate_rotated_bbox(seg)
                        # 从映射中获取类别名称
                        category_name = category_map.get(annotation['category_id'], 'Unknown')
                        # 格式化DOTA标注
                        box_str = ' '.join(map(str, box.flatten().tolist()))
                        dota_annotation = f"{box_str} {category_name} 0\n"
                        dota_file.write(dota_annotation)


# 调用函数,转换COCO到DOTA
coco_annotation_path = 'instances.json'
dota_annotation_folder = 'dota'
coco_to_dota(coco_annotation_path, dota_annotation_folder)

标注可视化:

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)
相关推荐
Loo国昌1 天前
RAG 第一阶段:前沿技术剖析与环境搭建
人工智能·后端·语言模型·架构
ZKNOW甄知科技1 天前
2025 甄知科技年度报告
运维·人工智能·低代码·ci/cd·自动化·数据库架构·敏捷流程
Keep_Trying_Go1 天前
基于无监督backbone无需训练的类别无关目标统计CountingDINO算法详解
人工智能·python·算法·多模态·目标统计
爱思德学术1 天前
中国计算机学会(CCF)推荐学术会议-C(软件工程/系统软件/程序设计语言):IEEE COMPSAC 2026
人工智能·区块链·软件工程
拖拖7651 天前
打破固定输出的边界:深入解读 Pointer Networks (Ptr-Nets)
人工智能
sy134108191951 天前
AI服务器设备中ISO系列信号隔离器应用场景
运维·服务器·人工智能
nvd111 天前
FastMCP 开发指南: 5分钟入门
人工智能·python
wp123_11 天前
反激应用1:1贴片耦合电感选择:Coilcraft LPD3015-473MR vs 国产兼容 TONEVEE CDD3015-473M
人工智能·制造
不错就是对1 天前
【agent-lightning】 - 2_使用 Agent-lightning 训练第一个智能体
人工智能·深度学习·神经网络·自然语言处理·chatgpt·transformer·vllm
zhengfei6111 天前
AI渗透工具—Shannon完全自主的AI渗透测试工具
人工智能·深度学习·web安全·知识图谱·测试覆盖率·安全性测试·威胁分析