上一篇文章推荐了DarkLabel标注软件,承诺会附上配套的代码,本文主要分享的是格式转换的几个脚本。
先附上脚本地址: https://github.com/pprp/SimpleCVReproduction/tree/master/DarkLabel
先来了解一下为何DarkLabel能生成这么多格式的数据集,来看看DarkLabel的格式:
frame(从0开始计), 数量, id(从0开始), box(x1,y1,x2,y2), class=null
0,4,0,450,194,558,276,null,1,408,147,469,206,null,2,374,199,435,307,null,3,153,213,218,314,null
1,4,0,450,194,558,276,null,1,408,147,469,206,null,2,374,199,435,307,null,3,153,213,218,314,null
2,4,0,450,194,558,276,null,1,408,147,469,206,null,2,374,199,435,307,null,3,153,213,218,314,null
每一帧,每张图片上的目标都可以提取到,并且每个目标有bbox、分配了一个ID、class
这些信息都可以满足目标检测、ReID、跟踪数据集。
ps:说明一下,以下脚本都是笔者自己写的,专用于单类的检测、跟踪、重识别的代码,如果有需要多类的,还需要自己修改多类部分的代码。
1. DarkLabel转Detection
这里笔者写了一个脚本转成VOC2007中的xml格式的标注,代码如下:
import cv2
import os
import shutil
import tqdm
import sys
root_path = r"I:\Dataset\VideoAnnotation"
def print_flush(str):
print(str, end='\r')
sys.stdout.flush()
def genXML(xml_dir, outname, bboxes, width, height):
xml_file = open((xml_dir + '/' + outname + '.xml'), 'w')
xml_file.write('<annotation>\n')
xml_file.write(' <folder>VOC2007</folder>\n')
xml_file.write(' <filename>' + outname + '.jpg' + '</filename>\n')
xml_file.write(' <size>\n')
xml_file.write(' <width>' + str(width) + '</width>\n')
xml_file.write(' <height>' + str(height) + '</height>\n')
xml_file.write(' <depth>3</depth>\n')
xml_file.write(' </size>\n')
for bbox in bboxes:
x1, y1, x2, y2 = bbox
xml_file.write(' <object>\n')
xml_file.write(' <name>' + 'cow' + '</name>\n')
xml_file.write(' <pose>Unspecified</pose>\n')
xml_file.write(' <truncated>0</truncated>\n')
xml_file.write(' <difficult>0</difficult>\n')
xml_file.write(' <bndbox>\n')
xml_file.write(' <xmin>' + str(x1) + '</xmin>\n')
xml_file.write(' <ymin>' + str(y1) + '</ymin>\n')
xml_file.write(' <xmax>' + str(x2) + '</xmax>\n')
xml_file.write(' <ymax>' + str(y2) + '</ymax>\n')
xml_file.write(' </bndbox>\n')
xml_file.write(' </object>\n')
xml_file.write('</annotation>')
def gen_empty_xml(xml_dir, outname, width, height):
xml_file = open((xml_dir + '/' + outname + '.xml'), 'w')
xml_file.write('<annotation>\n')
xml_file.write(' <folder>VOC2007</folder>\n')
xml_file.write(' <filename>' + outname + '.png' + '</filename>\n')
xml_file.write(' <size>\n')
xml_file.write