学习计算机视觉,标注工具会与你形影不离,因此我建议将其安装在独立的环境中,便于查找。
# 查看当前已有的虚拟环境
conda env list
# 创建
conda create - n env_name python=x.x # env_name: 虚拟环境的名称 python=...:指定python的版本
#激活虚拟环境
conda activate env_name
所示的就是我创建的虚拟环境啦:
pip install labelimg -i https://pypi.tuna.tsinghua.edu.cn/simple
代码测试过了,直接复制用就好了
import os.path
import xml.etree.ElementTree as ET
#1. 将这个地方改成自己类别的列表
class_names = ['plate']
# 2. 将路径修改
xmlpath = r'.....' # 原xml路径
txtpath = r'......' # 转换后txt文件存放路径
files = []
if not os.path.exists(txtpath):
os.makedirs(txtpath)
for root, dirs, files in os.walk(xmlpath):
None
number = len(files)
print(number)
i = 0
while i < number:
name = files[i][0:-4]
xml_name = name + ".xml"
txt_name = name + ".txt"
xml_file_name = xmlpath + xml_name
txt_file_name = txtpath + txt_name
xml_file = open(xml_file_name)
tree = ET.parse(xml_file)
root = tree.getroot()
# filename = root.find('name').text
# image_name = root.find('filename').text
w = int(root.find('size').find('width').text)
h = int(root.find('size').find('height').text)
f_txt = open(txt_file_name, 'w+')
content = ""
first = True
for obj in root.iter('object'):
name = obj.find('name').text
class_num = class_names.index(name)
# class_num = 0
xmlbox = obj.find('bndbox')
x1 = int(xmlbox.find('xmin').text)
x2 = int(xmlbox.find('xmax').text)
y1 = int(xmlbox.find('ymin').text)
y2 = int(xmlbox.find('ymax').text)
if first:
content += str(class_num) + " " + \
str((x1 + x2) / 2 / w) + " " + str((y1 + y2) / 2 / h) + " " + \
str((x2 - x1) / w) + " " + str((y2 - y1) / h)
first = False
else:
content += "\n" + \
str(class_num) + " " + \
str((x1 + x2) / 2 / w) + " " + str((y1 + y2) / 2 / h) + " " + \
str((x2 - x1) / w) + " " + str((y2 - y1) / h)
# print(str(i / (number - 1) * 100) + "%\n")
print(content)
f_txt.write(content)
f_txt.close()
xml_file.close()
i += 1
import os
import random
random.seed(0)
# 1. 将路径修改为自己的
xmlfilepath=r'E:\yolov7-main\VOC2007\Annotations'
saveBasePath=r'E:\yolov7-main\VOC2007\ImageSets\Main/'
#----------------------------------------------------------------------#
# 想要增加测试集修改trainval_percent
# train_percent不需要修改
#----------------------------------------------------------------------#
trainval_percent = 1
train_percent = 0.8
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("traub suze",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()
按照train.txt,val.txt,将图片和txt文件,分别复制到labels和images中
1 . 路径名称
2. .jpg 或者.png 修改成自己对应的就可以了
import os
import shutil
from tqdm import tqdm
SPLIT_PATH = r"E:\yolov7-main\VOC2007\ImageSets\Main"
IMGS_PATH = r"E:\yolov7-main\VOC2007\JPEGImages"
TXTS_PATH = r"E:\yolov7-main\VOC2007\txts"
TO_IMGS_PATH = r'E:\yolov7-main\yolov7-main\lesion\images'
TO_TXTS_PATH =r'E:\yolov7-main\yolov7-main\lesion\labels'
data_split = ['train.txt', 'val.txt']
to_split = ['train2007', 'val2007']
for index, split in enumerate(data_split):
split_path = os.path.join(SPLIT_PATH, split)
to_imgs_path = os.path.join(TO_IMGS_PATH, to_split[index])
if not os.path.exists(to_imgs_path):
os.makedirs(to_imgs_path)
to_txts_path = os.path.join(TO_TXTS_PATH, to_split[index])
if not os.path.exists(to_txts_path):
os.makedirs(to_txts_path)
f = open(split_path, 'r')
count = 1
for line in tqdm(f.readlines(), desc="{} is copying".format(to_split[index])):
# 复制图片
src_img_path = os.path.join(IMGS_PATH, line.strip() + '.png')
dst_img_path = os.path.join(to_imgs_path, line.strip() + '.png')
if os.path.exists(src_img_path):
shutil.copyfile(src_img_path, dst_img_path)
else:
print("error file: {}".format(src_img_path))
# 复制txt标注文件
src_txt_path = os.path.join(TXTS_PATH, line.strip() + '.txt')
dst_txt_path = os.path.join(to_txts_path, line.strip() + '.txt')
if os.path.exists(src_txt_path):
shutil.copyfile(src_txt_path, dst_txt_path)
else:
print("error file: {}".format(src_txt_path))