SpacetySliceTools/tools/DotaOperator.py

103 lines
3.3 KiB
Python
Raw Normal View History

2025-09-22 02:07:45 +00:00
import os
import argparse
import numpy as np
"""
Dota数据集标注
每条标注数据应包含
+ 8 个数值表示目标的四个角点的坐标x1, y1, x2, y2, x3, y3, x4, y4按顺时针或逆时针顺序排列无需进行归一化处理
+倒数第二列为标注的目标类别名称jun_ship等
+最后一列为识别目标的难易程度difficulty
提供数据集标注
"""
class DotaObj(object):
def __init__(self, x1, y1, x2, y2, x3, y3, x4, y4,clsname,difficulty):
self.x1=x1
self.y1=y1
self.x2=x2
self.y2=y2
self.x3=x3
self.y3=y3
self.x4=x4
self.y4=y4
self.clsname=clsname
self.difficulty=difficulty
def __str__(self):
return "{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}".format(
self.x1,self.y1,
self.x2,self.y2,
self.x3,self.y3,
self.x4,self.y4,
self.clsname,self.difficulty
)
def getCenter(self):
CenterX=np.mean([self.x1,self.x2,self.x3,self.x4])
CenterY=np.mean([self.y1,self.y2,self.y3,self.y4])
return [CenterX,CenterY]
def createDota(x1, y1, x2, y2, x3, y3, x4, y4,clsname,difficulty):
return DotaObj(x1, y1, x2, y2, x3, y3, x4, y4,clsname,difficulty) # 8+2
def readDotaFile(dotafilepath,filterlabels=None):
content=None
with open(dotafilepath,'r',encoding="utf-8") as fp:
content=fp.read()
contentlines=content.split("\n")
# 逐行分解
result=[]
for linestr in contentlines:
if linestr=="":
continue
linestr=linestr.replace("\t"," ")
linestr=linestr.replace(" "," ")
linemetas=linestr.split(" ")
if(len(linemetas)>=10):
x1=float(linemetas[0])
y1=float(linemetas[1])
x2=float(linemetas[2])
y2=float(linemetas[3])
x3=float(linemetas[4])
y3=float(linemetas[5])
x4=float(linemetas[6])
y4=float(linemetas[7])
clsname=linemetas[8]
difficulty=linemetas[9]
if filterlabels is None or clsname.strip() in filterlabels:
result.append(createDota(x1, y1, x2, y2, x3, y3, x4, y4, clsname,difficulty))
elif (len(linemetas)>=8):
x1=float(linemetas[0])
y1=float(linemetas[1])
x2=float(linemetas[2])
y2=float(linemetas[3])
x3=float(linemetas[4])
y3=float(linemetas[5])
x4=float(linemetas[6])
y4=float(linemetas[7])
clsname="MLC"
difficulty="0"
if filterlabels is None or clsname.strip() in filterlabels:
result.append(createDota(x1, y1, x2, y2, x3, y3, x4, y4, clsname,difficulty))
else:
print("parse result: ", linestr)
return result
def writerDotaFile(dotalist,filepath):
with open(filepath,'a',encoding="utf-8") as fp:
for dota in dotalist:
if isinstance(dota,DotaObj):
fp.write("{}\n".format(str(dota)))
else:
fp.write("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}\n".format(
dota[0],dota[1],dota[2],dota[3],dota[4],dota[5],dota[6],dota[7],dota[8],dota[9]
))
print("write label result: ", filepath)