【数据集处理三】地_空背景下红外图像弱小飞机目标检测跟踪数据集标签格式转换

原本数据集中标签为点标记且为txt文本,将电标记转变为3*3的标记框并转化为xml格式。(data1中用8*8的框更合适)

代码如下:

import os
import csv
channel = 1
#空标签

def Annotations0_write():

    rename = str(index)
    # print (rename)#打印文件名
    with open(os.path.join(foldername, rename + '.xml'), 'w') as f:
        f.write(f"""
    {foldername}
    {rename}.bmp
    D:/Research direction/data set/foldername/rename.bmp
	
		Unknown
	
    
        {256}
        {256}
        {channel}
    
    0\n""")
        f.write("")

###############################################
#一个标签
def Annotations1_write(xmid1,ymid1):

    rename = str(index)
    # print (rename)#打印文件名
    with open(os.path.join(foldername, rename + '.xml'), 'w') as f:
        f.write(f"""
    {foldername}
    {rename}.bmp
     D:/Research direction/data set/foldername/rename.bmp
	
		Unknown
	
    
        {256}
        {256}
        {channel}
    
    0\n""")
        f.write(f"""    
            drone
            Unspecified
            0
            0
            
                  {float(xmid1)-1.5}
                  {float(ymid1)-1.5}
                  {float(xmid1)+1.5}
                  {float(ymid1)+1.5}
            
        \n""")
        f.write("")

#两个标签
def Annotations2_write(xmid1,ymid1,xmid2,ymid2):

    rename = str(index)
    # print (rename)#打印文件名
    with open(os.path.join(foldername, rename + '.xml'), 'w') as f:
        f.write(f"""
    {foldername}
    {rename}.bmp
     D:/Research direction/data set/foldername/rename.bmp
	
		Unknown
	
    
        {256}
        {256}
        {channel}
    
    0\n""")
        f.write(f"""    
            drone
            Unspecified
            0
            0
            
                    {float(xmid1) - 1.5}
                    {float(ymid1) - 1.5}
                    {float(xmid1) + 1.5}
                    {float(ymid1) + 1.5}
            
        
        
             drone
             Unspecified
             0
             0
             
                  {float(xmid2) - 1.5}
                  {float(ymid2) - 1.5}
                  {float(xmid2) + 1.5}
                  {float(ymid2) + 1.5}
              
        \n""")
        f.write("")

#读取第i+2行(i=0,表示从第二行开始读)
def read(index):
    for line in lines[index:index + 1:]:
        a = line.split()
        objnum = int(a[1])
        # print(line)

        if objnum == 1:
            xmid1 = a[3]
            ymid1 = a[4]
            Annotations1_write(xmid1,ymid1)
        elif objnum == 0:
            Annotations0_write()
        elif objnum == 2:
            xmid1 = a[3]
            ymid1 = a[4]
            xmid2 = a[6]
            ymid2 = a[7]
            Annotations2_write(xmid1,ymid1,xmid2,ymid2)

        else:
            print('出错了')



#########
with open ('data22.txt') as data:
# filename =  'data1.txt'
# data =open (filename)
  folder=data.readline() #读取第一行
  folder1=folder.split() #看做字符串
  foldername=folder1[1]  #文件夹名称
  num=int(folder1[2])#帧数
#print (foldername)
  path = foldername
  # print(num)
# 创建文件夹
  if not os.path.exists(path):
      os.mkdir(path)

  index=0
  a=folder1#给一个a原始值
  #外面一个while循环
  lines = data.readlines()
  while index != num:

     # if index != num:
     read(index)
     index = index + 1

你可能感兴趣的:(目标检测,人工智能,计算机视觉)