1.yolo格式的标注文件
7 0.534896 0.599074 0.025 0.205556
2 0.376042 0.903241 0.0145833 0.0694444
2 0.475781 0.0291667 0.0161458 0.0583333
2 0.409115 0.87963 0.0161458 0.0685185
2 0.472917 0.555556 0.0145833 0.0703704
2 0.348958 0.914815 0.0145833 0.0611111
0 0.29974 0.835648 0.00572917 0.0157407
2 0.411719 0.568056 0.0171875 0.0638889
2 0.4125 0.141204 0.0145833 0.0657407
2 0.378906 0.418056 0.0161458 0.075
2.voc格式的标注文件
<annotation>
<folder>VOC2007</folder>
<filename>DJI_0113-2-2040.jpg</filename>
<size>
<width>1920</width>
<height>1080</height>
<depth>3</depth>
</size>
<object>
<name>7</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>1003</xmin>
<ymin>536</ymin>
<xmax>1051</xmax>
<ymax>758</ymax>
</bndbox>
</object>
<object>
<name>2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>708</xmin>
<ymin>938</ymin>
<xmax>736</xmax>
<ymax>1013</ymax>
</bndbox>
</object>
<object>
<name>2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>897</xmin>
<ymin>0</ymin>
<xmax>928</xmax>
<ymax>63</ymax>
</bndbox>
</object>
<object>
<name>2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>770</xmin>
<ymin>913</ymin>
<xmax>801</xmax>
<ymax>987</ymax>
</bndbox>
</object>
<object>
<name>2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>894</xmin>
<ymin>562</ymin>
<xmax>922</xmax>
<ymax>638</ymax>
</bndbox>
</object>
<object>
<name>2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>656</xmin>
<ymin>955</ymin>
<xmax>684</xmax>
<ymax>1021</ymax>
</bndbox>
</object>
<object>
<name>0</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>570</xmin>
<ymin>893</ymin>
<xmax>581</xmax>
<ymax>910</ymax>
</bndbox>
</object>
<object>
<name>2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>774</xmin>
<ymin>579</ymin>
<xmax>807</xmax>
<ymax>648</ymax>
</bndbox>
</object>
<object>
<name>2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>778</xmin>
<ymin>117</ymin>
<xmax>806</xmax>
<ymax>188</ymax>
</bndbox>
</object>
<object>
<name>2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>711</xmin>
<ymin>411</ymin>
<xmax>742</xmax>
<ymax>492</ymax>
</bndbox>
</object>
</annotation>
3.转换代码
#! /usr/bin/python
# -*- coding:UTF-8 -*-
import os, sys
import glob
from PIL import Image
# 图像存储位置
src_img_dir = " " # 添加你的路径
# 图像的 ground truth 的 txt 文件存放位置
src_txt_dir = " "
src_xml_dir = " "
img_Lists = glob.glob(src_img_dir + '/*.jpg')
img_basenames = [] # e.g. 100.jpg
for item in img_Lists:
img_basenames.append(os.path.basename(item))
img_names = [] # e.g. 100
for item in img_basenames:
temp1, temp2 = os.path.splitext(item)
img_names.append(temp1)
for img in img_names:
im = Image.open((src_img_dir + '/' + img + '.jpg'))
width, height = im.size
# open the crospronding txt file
gt = open(src_txt_dir + '/' + img + '.txt').read().splitlines()
# write in xml file
xml_file = open((src_xml_dir + '/' + img + '.xml'), 'w')
xml_file.write('<annotation>\n')
xml_file.write(' <folder>VOC2007</folder>\n')
xml_file.write(' <filename>' + str(img) + '.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')
# write the region of image on xml file
for img_each_label in gt:
spt = img_each_label.split(' ') #这里如果txt里面是以逗号‘,’隔开的,那么就改为spt = img_each_label.split(',')。
label = int(float(spt[0].strip()))
center_x = round(float(str(spt[1]).strip()) * width)
center_y = round(float(str(spt[2]).strip()) * height)
bbox_width = round(float(str(spt[3]).strip()) * width)
bbox_height = round(float(str(spt[4]).strip()) * height)
xmin = str(int(center_x - bbox_width / 2 ))
ymin = str(int(center_y - bbox_height / 2))
xmax = str(int(center_x + bbox_width / 2))
ymax = str(int(center_y + bbox_height / 2))
xml_file.write(' <object>\n')
xml_file.write(' <name>' + str(label)+ '</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(xmin) + '</xmin>\n')
xml_file.write(' <ymin>' + str(ymin) + '</ymin>\n')
xml_file.write(' <xmax>' + str(xmax) + '</xmax>\n')
xml_file.write(' <ymax>' + str(ymax) + '</ymax>\n')
xml_file.write(' </bndbox>\n')
xml_file.write(' </object>\n')
xml_file.write('</annotation>')