当前位置:网站首页>The XML file generated by labelimg is converted to VOC format
The XML file generated by labelimg is converted to VOC format
2022-07-03 03:09:00 【AphilGuo】
Personal records
import os
import random
import xml.etree.ElementTree as ET
import numpy as np
from utils.utils import get_classes
#--------------------------------------------------------------------------------------------------------------------------------#
# annotation_mode Used to specify the contents of the file to be calculated at run time
# annotation_mode by 0 Represents the entire label processing process , Including getting VOCdevkit/VOC2007/ImageSets Inside txt And for training 2007_train.txt、2007_val.txt
# annotation_mode by 1 On behalf of VOCdevkit/VOC2007/ImageSets Inside txt
# annotation_mode by 2 Stands for... For training 2007_train.txt、2007_val.txt
#--------------------------------------------------------------------------------------------------------------------------------#
annotation_mode = 0
#-------------------------------------------------------------------#
# It has to be changed , Used to generate 2007_train.txt、2007_val.txt Target information for
# And the... Used for training and prediction classes_path It's OK to be consistent
# If generated 2007_train.txt There is no target information in it
# So it's because classes Not set correctly
# Only in annotation_mode by 0 and 2 In force
#-------------------------------------------------------------------#
classes_path = 'model_data/voc_classes.txt'
#--------------------------------------------------------------------------------------------------------------------------------#
# trainval_percent Is used to specify the ( Training set + Verification set ) Ratio to test set , By default ( Training set + Verification set ): Test set = 9:1
# train_percent Is used to specify the ( Training set + Verification set ) The ratio of training set to verification set in , By default Training set : Verification set = 9:1
# Only in annotation_mode by 0 and 1 In force
#--------------------------------------------------------------------------------------------------------------------------------#
trainval_percent = 0.9
train_percent = 0.9
#-------------------------------------------------------#
# Point to VOC The folder where the dataset is located
# By default, it points to... In the root directory VOC Data sets
#-------------------------------------------------------#
VOCdevkit_path = 'VOCdevkit'
VOCdevkit_sets = [('2007', 'train'), ('2007', 'val')]
classes, _ = get_classes(classes_path)
#-------------------------------------------------------#
# Count the number of targets
#-------------------------------------------------------#
photo_nums = np.zeros(len(VOCdevkit_sets))
nums = np.zeros(len(classes))
def convert_annotation(year, image_id, list_file):
in_file = open(os.path.join(VOCdevkit_path, 'VOC%s/Annotations/%s.xml'%(year, image_id)), encoding='utf-8')
tree=ET.parse(in_file)
root = tree.getroot()
for obj in root.iter('object'):
difficult = 0
if obj.find('difficult')!=None:
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult)==1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (int(float(xmlbox.find('xmin').text)), int(float(xmlbox.find('ymin').text)), int(float(xmlbox.find('xmax').text)), int(float(xmlbox.find('ymax').text)))
list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id))
nums[classes.index(cls)] = nums[classes.index(cls)] + 1
if __name__ == "__main__":
random.seed(0)
if " " in os.path.abspath(VOCdevkit_path):
raise ValueError(" No space is allowed in the folder path and picture name where the dataset is stored , Otherwise, normal model training will be affected , Please pay attention to the modification .")
if annotation_mode == 0 or annotation_mode == 1:
print("Generate txt in ImageSets.")
xmlfilepath = os.path.join(VOCdevkit_path, 'VOC2007/Annotations')
saveBasePath = os.path.join(VOCdevkit_path, 'VOC2007/ImageSets/Main')
temp_xml = os.listdir(xmlfilepath)
total_xml = []
for xml in temp_xml:
if xml.endswith(".xml"):
total_xml.append(xml)
num = len(total_xml)
list = range(num)
tv = int(num*trainval_percent)
tr = int(tv*train_percent)
trainval= random.sample(list,tv)
train = random.sample(trainval,tr)
print("train and val size",tv)
print("train size",tr)
ftrainval = open(os.path.join(saveBasePath,'trainval.txt'), 'w')
ftest = open(os.path.join(saveBasePath,'test.txt'), 'w')
ftrain = open(os.path.join(saveBasePath,'train.txt'), 'w')
fval = open(os.path.join(saveBasePath,'val.txt'), 'w')
for i in list:
name=total_xml[i][:-4]+'\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftrain.write(name)
else:
fval.write(name)
else:
ftest.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()
print("Generate txt in ImageSets done.")
if annotation_mode == 0 or annotation_mode == 2:
print("Generate 2007_train.txt and 2007_val.txt for train.")
type_index = 0
for year, image_set in VOCdevkit_sets:
image_ids = open(os.path.join(VOCdevkit_path, 'VOC%s/ImageSets/Main/%s.txt'%(year, image_set)), encoding='utf-8').read().strip().split()
list_file = open('%s_%s.txt'%(year, image_set), 'w', encoding='utf-8')
for image_id in image_ids:
list_file.write('%s/VOC%s/JPEGImages/%s.jpg'%(os.path.abspath(VOCdevkit_path), year, image_id))
convert_annotation(year, image_id, list_file)
list_file.write('\n')
photo_nums[type_index] = len(image_ids)
type_index += 1
list_file.close()
print("Generate 2007_train.txt and 2007_val.txt for train done.")
def printTable(List1, List2):
for i in range(len(List1[0])):
print("|", end=' ')
for j in range(len(List1)):
print(List1[j][i].rjust(int(List2[j])), end=' ')
print("|", end=' ')
print()
str_nums = [str(int(x)) for x in nums]
tableData = [
classes, str_nums
]
colWidths = [0]*len(tableData)
len1 = 0
for i in range(len(tableData)):
for j in range(len(tableData[i])):
if len(tableData[i][j]) > colWidths[i]:
colWidths[i] = len(tableData[i][j])
printTable(tableData, colWidths)
if photo_nums[0] <= 500:
print(" The number of training sets is less than 500, It belongs to a small amount of data , Please pay attention to setting a larger training generation (Epoch) To meet sufficient gradient descent times (Step).")
if np.sum(nums) == 0:
print(" No goals were achieved in the dataset , Please pay attention to the modification classes_path Corresponding to their own data set , And make sure the label name is correct , Otherwise, the training will have no effect !")
print(" No goals were achieved in the dataset , Please pay attention to the modification classes_path Corresponding to their own data set , And make sure the label name is correct , Otherwise, the training will have no effect !")
print(" No goals were achieved in the dataset , Please pay attention to the modification classes_path Corresponding to their own data set , And make sure the label name is correct , Otherwise, the training will have no effect !")
print("( Important things are to be repeated for 3 times ).")
边栏推荐
- How to implement append in tensor
- I2C 子系统(四):I2C debug
- VS 2019 配置tensorRT生成engine
- TCP handshake three times and wave four times. Why does TCP need handshake three times and wave four times? TCP connection establishes a failure processing mechanism
- Notifydatasetchanged not applicable to recyclerview - notifydatasetchanged not working on recyclerview
- Sous - système I2C (IV): débogage I2C
- [C language] MD5 encryption for account password
- Agile certification (professional scrum Master) simulation exercise-2
- 模型转换onnx2engine
- What happens between entering the URL and displaying the page?
猜你喜欢

Vs 2019 configuration du moteur de génération de tensorrt

Pytest (6) -fixture (Firmware)

用docker 连接mysql的过程

3D drawing example

idea 加载不了应用市场解决办法(亲测)
![ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc](/img/cb/145937a27ef08050a370d5a255215a.jpg)
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc

C language beginner level - pointer explanation - paoding jieniu chapter

Pytorch配置

Docker install MySQL

Force deduction ----- the minimum path cost in the grid
随机推荐
docker安装redis
[C language] MD5 encryption for account password
Basic information of Promethus (I)
idea 加载不了应用市场解决办法(亲测)
C语言初阶-指针详解-庖丁解牛篇
[shutter] monitor the transparency gradient of the scrolling action control component (remove the blank of the top status bar | frame layout component | transparency component | monitor the scrolling
Andwhere multiple or query ORM conditions in yii2
Pytest (6) -fixture (Firmware)
labelimg生成的xml文件转换为voc格式
VS 2019 配置tensorRT生成engine
Can netstat still play like this?
45 lectures on MySQL [index]
迅雷chrome扩展插件造成服务器返回的数据js解析页面数据异常
C#通用接口调用
ComponentScan和ComponentScans的区别
The left value and the right finger explain better
Creation and destruction of function stack frame
Notifydatasetchanged not applicable to recyclerview - notifydatasetchanged not working on recyclerview
What does it mean when lambda is not entered?
The difference between componentscan and componentscans