赞
踩
voc格式
- 文件联级如下所述:
- VOCdevkit
- ---VOC2007
- ---Annotations
- ---ImageSets
- ---JPEGImages
yolo格式
- voc_to_yolo.py
-
- from tqdm import tqdm
- import shutil
- from pathlib import Path
- import xml.etree.ElementTree as ET
-
- def convert_label(path, lb_path, year, image_id, names):
- 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(path / f'VOC{year}/Annotations/{image_id}.xml')
- out_file = open(lb_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)
-
- 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')
- else:
- print("category error: ", cls)
-
- year = "2007"
- image_sets = ["train", "val"]
- path = Path("H:\\work\\daodan_move\\ultralytics-main\\ultralytics\\datasets\\VOCdevkit\\")
- class_names = ["call","dislike","fist","four","like","mute","ok","one","palm","1","2","3","4","5","6","7","8","9","10"]
-
- for image_set in image_sets:
- imgs_path = path / 'images' / f'{image_set}'
- lbs_path = path / 'labels' / f'{image_set}'
- imgs_path.mkdir(exist_ok=True, parents=True)
- lbs_path.mkdir(exist_ok=True, parents=True)
-
- with open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt') as f:
- image_ids = f.read().strip().split()
- for id in tqdm(image_ids, desc=f'{image_set}'):
- f = path / f'VOC{year}/JPEGImages/{id}.jpg' # old img path
- lb_path = (lbs_path / f.name).with_suffix('.txt') # new label path
- # f.rename(imgs_path / f.name) # move image
- shutil.copyfile(f, imgs_path / f.name) # copy image
- convert_label(path, lb_path, year, id, class_names) # convert labels to YOLO format
coco格式
- ── VOCdevkit
- ├── images
- │ ├── train # 存放训练集图片
- │ └── val # 存放验证集图片
- └── labels
- ├── train # 存放训练集标注文件
- └── val # 存放验证集标注文件
- import json
- import os
- import shutil
- from tqdm import tqdm
-
- coco_path = "F:/datasets/Apple_Detection_Swift-YOLO_192"
- output_path = "F:/vsCode/ultralytics/datasets/Apple"
-
- os.makedirs(os.path.join(output_path, "images", "train"), exist_ok=True)
- os.makedirs(os.path.join(output_path, "images", "val"), exist_ok=True)
- os.makedirs(os.path.join(output_path, "labels", "train"), exist_ok=True)
- os.makedirs(os.path.join(output_path, "labels", "val"), exist_ok=True)
-
- with open(os.path.join(coco_path, "train", "_annotations.coco.json"), "r") as f:
- train_annotations = json.load(f)
-
- with open(os.path.join(coco_path, "valid", "_annotations.coco.json"), "r") as f:
- val_annotations = json.load(f)
-
- # Iterate over the training images
- for image in tqdm(train_annotations["images"]):
- width, height = image["width"], image["height"]
- scale_x = 1.0 / width
- scale_y = 1.0 / height
-
- label = ""
- for annotation in train_annotations["annotations"]:
- if annotation["image_id"] == image["id"]:
- # Convert the annotation to YOLO format
- x, y, w, h = annotation["bbox"]
- x_center = x + w / 2.0
- y_center = y + h / 2.0
- x_center *= scale_x
- y_center *= scale_y
- w *= scale_x
- h *= scale_y
- class_id = annotation["category_id"]
- label += "{} {} {} {} {}\n".format(class_id, x_center, y_center, w, h)
-
- # Save the image and label
- shutil.copy(os.path.join(coco_path, "train", image["file_name"]), os.path.join(output_path, "images", "train", image["file_name"]))
- with open(os.path.join(output_path, "labels", "train", image["file_name"].replace(".jpg", ".txt")), "w") as f:
- f.write(label)
-
- # Iterate over the validation images
- for image in tqdm(val_annotations["images"]):
- width, height = image["width"], image["height"]
- scale_x = 1.0 / width
- scale_y = 1.0 / height
-
- label = ""
- for annotation in val_annotations["annotations"]:
- if annotation["image_id"] == image["id"]:
- # Convert the annotation to YOLO format
- x, y, w, h = annotation["bbox"]
- x_center = x + w / 2.0
- y_center = y + h / 2.0
- x_center *= scale_x
- y_center *= scale_y
- w *= scale_x
- h *= scale_y
- class_id = annotation["category_id"]
- label += "{} {} {} {} {}\n".format(class_id, x_center, y_center, w, h)
-
- # Save the image and label
- shutil.copy(os.path.join(coco_path, "valid", image["file_name"]), os.path.join(output_path, "images", "val", image["file_name"]))
- with open(os.path.join(output_path, "labels", "val", image["file_name"].replace(".jpg", ".txt")), "w") as f:
- f.write(label)
将voc.yaml复制为voc_self.yaml,然后修改如下:
- # Ultralytics YOLO 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/895867推荐阅读
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。