目标检测DOTA数据集提取感兴趣类别数据

DOTA数据集

DOTA数据集包含2806张航空图像,尺寸大约从800x800到4000x4000不等,包含15个类别共计188282个实例。其标注方式为四点确定的任意形状和方向的四边形(区别于传统的对边平行bbox)。类别分别为:plane, ship, storage tank, baseball dia- mond, tennis court, swimming pool, ground track field, har- bor, bridge, large vehicle, small vehicle, helicopter, round- about, soccer ball field , basketball court。

提取感兴趣类别数据

我们需求可能只感兴趣某一个或几个类别,这时候我们需要剔除掉不包含我们感兴趣类别的数据。下面,以船只ship为例,为大家介绍提取感兴趣类别数据的代码:

复制代码
import os
from shutil import copyfile

def filterTxt(srcTxtPah, dstTxtPath, selected_class):
    selected_class_num = 0
    #  r:读取文件,若文件不存在则会报错
    with open(srcTxtPah, "r") as rf:
        for line in rf.readlines():
            if(selected_class in line):
                selected_class_num += 1
                #  a:写入文件,若文件不存在则会先创建再写入,但不会覆盖原文件,而是追加在文件末尾
                with open(dstTxtPath,"a") as af:
                    af.write(line)  # 自带文件关闭功能,不需要再写f.close()
    rf.close()
    return selected_class_num
    
#  DOTA数据的txt文件夹
txtFolder = r"I:\Remote_Sensing_Data\DOTA_Dataset\train\labelTxt-v1.0\labelTxt"
#  DOTA数据的image文件夹
imgFolder = r"I:\Remote_Sensing_Data\DOTA_Dataset\train\images\images"
#  要复制到的image文件夹
copy_imageFolder = r"I:\ship_detect\Data\DOTA_ship\train\images"
#  要复制到的txt文件夹
copy_txtFolder = r"I:\ship_detect\Data\DOTA_ship\train\labelTxt-v1.0"
#  感兴趣类别
selected_class = "ship"

txtNameList = os.listdir(txtFolder)
for i in range(len(txtNameList)):
    #  判断当前文件是否为txt文件
    if(os.path.splitext(txtNameList[i])[1] == ".txt"):
        txt_path = txtFolder + "\\" + txtNameList[i]
        #  设置文件对象
        f = open(txt_path, "r")
        #  读取一行文件,包括换行符
        line = f.readline()
        while line:
            #  若该类是selected_class,则将对应图像复制粘贴,并停止循环
            if(selected_class in line):
                #  获取txt的索引,不带扩展名的文件名
                txt_index = os.path.splitext(txtNameList[i])[0]
                #  获取对应图像文件的地址
                src = imgFolder + "\\" + txt_index + ".png"
                dst = copy_imageFolder + "\\" + txt_index + ".png"
                #  复制图像文件至指定位置
                copyfile(src, dst)
                #  筛选txt文件中的selected_class信息并写至指定位置
                selected_class_num = filterTxt(txt_path, copy_txtFolder + "\\" + txt_index + ".txt", selected_class)
                print(txt_index,".png have", selected_class_num, selected_class)
                break
            #  若第一行不是selected_class,继续向下读,直到读取完文件
            else:
                line = f.readline() 
f.close() #关闭文件

输出:

复制代码
P0001 .png have 17 ship
P0011 .png have 1 ship
P0020 .png have 1 ship
P0129 .png have 138 ship
......
P2769 .png have 15 ship
P2770 .png have 33 ship
P2792 .png have 601 ship

这样就实现了将含有船只的数据集单独挑选出来了。

可视化边界框

我们将船只数据集单独挑选出来后,可以可视化一下边界框。DOTA提供的是OBB有向边界框,我们也可以转换成HBB水平边界框。

复制代码
from PIL import Image, ImageDraw

imgPath = r"I:\ship_detect\Data\DOTA_ship\train\images\P0340.png"
txtPath = r"I:\ship_detect\Data\DOTA_ship\train\labelTxt-v1.0\P0340.txt"
savePath = "obb.jpg"
drawType = "obb"

img =Image.open(imgPath)
draw =ImageDraw.Draw(img)
with open(txtPath, "r") as f:
    for line in f.readlines():
        #  去掉列表中每一个元素的换行符
        line = line.strip('\n')  
        line = line.split(" ")
        #print(line)
        if(drawType == "obb"):
            #  绘制OBB有向边界框
            polygon = []
            for i in range(8):
                polygon.append(int(line[i]))
            polygon = tuple(polygon)
            draw.polygon(polygon, outline = 'red')
        elif(drawType == "hbb"):
            #  绘制HBB水平边界框
            xmin = min(int(line[0]), int(line[2]), int(line[4]), int(line[6]))
            xmax = max(int(line[0]), int(line[2]), int(line[4]), int(line[6]))
            ymin = min(int(line[1]), int(line[3]), int(line[5]), int(line[7]))
            ymax = max(int(line[1]), int(line[3]), int(line[5]), int(line[7]))
            draw.rectangle(
                    [xmin, ymin, xmax, ymax],
                    outline = 'red')
img.save(savePath, quality = 95)

OBB

HBB

来源:应用推广部

供稿:技术研发部

编辑:方梅

相关推荐
政安晨1 小时前
政安晨【零基础玩转开源AI项目】- AutoGPT:全球首个自主AI Agent从入门到实战(致敬OpenClaw的小回顾)
人工智能·ai·autogpt·全球首个agent框架·致敬openclaw之作·参考价值·ai开源agent框架
Shawn_Shawn6 小时前
mcp学习笔记(一)-mcp核心概念梳理
人工智能·llm·mcp
33三 三like8 小时前
《基于知识图谱和智能推荐的养老志愿服务系统》开发日志
人工智能·知识图谱
芝士爱知识a8 小时前
【工具推荐】2026公考App横向评测:粉笔、华图与智蛙面试App功能对比
人工智能·软件推荐·ai教育·结构化面试·公考app·智蛙面试app·公考上岸
腾讯云开发者9 小时前
港科大熊辉|AI时代的职场新坐标——为什么你应该去“数据稀疏“的地方?
人工智能
工程师老罗9 小时前
YoloV1数据集格式转换,VOC XML→YOLOv1张量
xml·人工智能·yolo
yLDeveloper9 小时前
从模型评估、梯度难题到科学初始化:一步步解析深度学习的训练问题
深度学习
Coder_Boy_10 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
啊森要自信10 小时前
CANN ops-cv:面向计算机视觉的 AI 硬件端高效算子库核心架构与开发逻辑
人工智能·计算机视觉·架构·cann