# Ultralytics YOLO 🚀, AGPL-3.0 license
# PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC by University of Oxford
# Documentation: # Documentation: https://docs.ultralytics.com/datasets/detect/voc/
# Example usage: yolo train data=VOC.yaml
# parent
# ├── ultralytics
# └── datasets
# └── VOC ← downloads here (2.8 GB)
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../datasets/VOC
train: # train images (relative to 'path') 16551 images
- images/train
val: # val images (relative to 'path') 4952 images
- images/val
test: # test images (optional)
- images/val
# Classes
names:
0: JYZPS
# Download script/URL (optional) ---------------------------------------------------------------------------------------
download: |
import shutil
import xml.etree.ElementTree as ET
import os
import random
from tqdm import tqdm
def convert_label(xml_file_path, txt_file_path):
def convert_box(size, box):
dw, dh = 1. / size[0], 1. / size[1]
x, y, w, h = (box[0] + box[1]) / 2.0 - 1, (box[2] + box[3]) / 2.0 - 1,\
box[1] - box[0], box[3] - box[2]
return x * dw, y * dh, w * dw, h * dh
in_file = open(xml_file_path)
out_file = open(txt_file_path, 'w')
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
names = list(yaml['names'].values())
for obj in root.iter('object'):
cls = obj.find('name').text
if cls in names:
xmlbox = obj.find('bndbox')
bb = convert_box((w, h), [float(xmlbox.find(x).text) for x in ('xmin', 'xmax', 'ymin', 'ymax')])
cls_id = names.index(cls) # class id
out_file.write(" ".join([str(a) for a in (cls_id, *bb)]) + '\n')
out_file.close()
# 1.path
root_dir = yaml['path'] # need
imgs_dir = os.path.join(root_dir, 'JPEGImages') # need
xmls_dir = os.path.join(root_dir, 'Annotations') # need
txts_dir = os.path.join(root_dir, 'Labels')
images_dir = os.path.join(root_dir, 'images')
labels_dir = os.path.join(root_dir, 'labels')
train_img_dir = os.path.join(images_dir, 'train')
train_txt_dir = os.path.join(labels_dir, 'train')
val_img_dir = os.path.join(images_dir, 'val')
val_txt_dir = os.path.join(labels_dir, 'val')
# 2.dir
shutil.rmtree(txts_dir, ignore_errors=True)
os.mkdir(txts_dir)
shutil.rmtree(images_dir, ignore_errors=True)
os.mkdir(images_dir)
os.mkdir(train_img_dir)
os.mkdir(val_img_dir)
shutil.rmtree(labels_dir, ignore_errors=True)
os.mkdir(labels_dir)
os.mkdir(train_txt_dir)
os.mkdir(val_txt_dir)
# 3.convert xml to txt
imgs_list = os.listdir(imgs_dir)
for img_file in tqdm(imgs_list, desc='convert xml to txt'):
if img_file.split('.')[-1] in ['JPG', 'jpg']:
name, _ = os.path.splitext(img_file)
txt_file = name + '.txt'
xml_file = name + '.xml'
xml_path = os.path.join(xmls_dir, xml_file)
txt_path = os.path.join(txts_dir, txt_file)
convert_label(xml_path, txt_path)
# 4.split train and val
random.seed(0)
random.shuffle(imgs_list)
data_len = len(imgs_list)
train_list, val_list = imgs_list[:int(0.8*data_len)], imgs_list[int(0.8*data_len):]
# 5.copy file
for img_file in tqdm(train_list, desc='copy train file'):
if img_file.split('.')[-1] in ['JPG', 'jpg']:
src_path = os.path.join(imgs_dir, img_file)
dst_path = os.path.join(train_img_dir, img_file)
shutil.copyfile(src=src_path, dst=dst_path)
name, _ = os.path.splitext(img_file)
txt_file = name + '.txt'
src_path = os.path.join(txts_dir, txt_file)
dst_path = os.path.join(train_txt_dir, txt_file)
shutil.copyfile(src=src_path, dst=dst_path)
for img_file in tqdm(val_list, desc='copy val file'):
src_path = os.path.join(imgs_dir, img_file)
dst_path = os.path.join(val_img_dir, img_file)
shutil.copyfile(src=src_path, dst=dst_path)
name, _ = os.path.splitext(img_file)
txt_file = name + '.txt'
src_path = os.path.join(txts_dir, txt_file)
dst_path = os.path.join(val_txt_dir, txt_file)
shutil.copyfile(src=src_path, dst=dst_path)
【深度学习】YOLOv8使用自己的VOC数据集
于 2024-01-18 21:47:12 首次发布