1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| import os import random import shutil import xml.etree.ElementTree as ET
class_names = ['',''] voc_dir = '.' yolo_dir = '.'
os.makedirs(os.path.join(yolo_dir, 'images', 'train'), exist_ok=True) os.makedirs(os.path.join(yolo_dir, 'images', 'val'), exist_ok=True) os.makedirs(os.path.join(yolo_dir, 'images', 'test'), exist_ok=True) os.makedirs(os.path.join(yolo_dir, 'labels', 'train'), exist_ok=True) os.makedirs(os.path.join(yolo_dir, 'labels', 'val'), exist_ok=True) os.makedirs(os.path.join(yolo_dir, 'labels', 'test'), exist_ok=True)
with open(os.path.join(voc_dir, 'ImageSets', 'Main', 'train.txt')) as f: train_list = f.read().splitlines() with open(os.path.join(voc_dir, 'ImageSets', 'Main', 'val.txt')) as f: val_list = f.read().splitlines() with open(os.path.join(voc_dir, 'ImageSets', 'Main', 'test.txt')) as f: test_list = f.read().splitlines()
for split_name, split_list in [('train', train_list), ('val', val_list), ('test', test_list)]: for image_name in split_list: src_image_path = os.path.join(voc_dir, 'JPEGImages', f'{image_name}.png') dst_image_path = os.path.join(yolo_dir, 'images', split_name, f'{image_name}.jpg') shutil.copy(src_image_path, dst_image_path) xml_path = os.path.join(voc_dir, 'Annotations', f'{image_name}.xml') tree = ET.parse(xml_path) root = tree.getroot()
size_elem = root.find('size') width = int(size_elem.find('width').text) height = int(size_elem.find('height').text)
with open(os.path.join(yolo_dir, 'labels', split_name, f'{image_name}.txt'), 'w') as f: for obj_elem in root.findall('object'): class_name = obj_elem.find('name').text if class_name not in class_names: continue
bbox_elem = obj_elem.find('bndbox') xmin = int(float(bbox_elem.find('xmin').text)) ymin = int(float(bbox_elem.find('ymin').text)) xmax = int(float(bbox_elem.find('xmax').text)) ymax = int(float(bbox_elem.find('ymax').text))
x_center = (xmin + xmax) / 2 / width y_center = (ymin + ymax) / 2 / height w = (xmax - xmin) / width h = (ymax - ymin) / height
f.write(f"{class_names.index(class_name)} {x_center:.6f} {y_center:.6f} {w:.6f} {h:.6f}\n")
|