赞
踩
坑:需要将数据集中的图片转换为jpg格式
没有细致研究
转换图片格式脚本 pngToJpg.py
from PIL import Image import os import shutil if __name__ == '__main__': path = './jpg_images' save_path = './images' if not os.path.exists(save_path): os.makedirs(save_path) files = os.listdir(path) for name in files: save_filepath = os.path.join(save_path, name[:-4]+'.jpg') filepath = os.path.join(path,name) if name[-4:] == '.png': img = Image.open(filepath) img = img.convert('RGB') img.save(save_filepath, quality=95) else: shutil.copy(filepath,save_filepath)
# -*- coding: utf-8 -*- import xml.etree.ElementTree as ET import os from os import getcwd sets = ['train', 'val', 'test'] classes = ["helmet", "head", "person"] # 改成自己的类别 abs_path = os.getcwd() print(abs_path) def convert(size, box): dw = 1. / (size[0]) dh = 1. / (size[1]) x = (box[0] + box[1]) / 2.0 - 1 y = (box[2] + box[3]) / 2.0 - 1 w = box[1] - box[0] h = box[3] - box[2] x = x * dw w = w * dw y = y * dh h = h * dh return x, y, w, h def convert_annotation(image_id): in_file = open('annotations/%s.xml' % (image_id), encoding='UTF-8') out_file = open('labels/%s.txt' % (image_id), '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'): difficult = obj.find('difficult').text # 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 = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)) b1, b2, b3, b4 = b # 标注越界修正 if b2 > w: b2 = w if b4 > h: b4 = h b = (b1, b2, b3, b4) bb = convert((w, h), b) out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') wd = getcwd() for image_set in sets: if not os.path.exists('labels/'): os.makedirs('labels/') image_ids = open('dataSet/%s.txt' % (image_set)).read().strip().split() if not os.path.exists('dataSet_path/'): os.makedirs('dataSet_path/') list_file = open('dataSet_path/%s.txt' % (image_set), 'w') for image_id in image_ids: list_file.write(abs_path + '\images\%s.jpg\n' % (image_id)) convert_annotation(image_id) list_file.close()
# coding:utf-8 import os import random import argparse parser = argparse.ArgumentParser() # xml文件的地址,根据自己的数据进行修改 xml一般存放在Annotations下,注意以下为相对路径 parser.add_argument('--xml_path', default='annotations', type=str, help='input xml label path') # 数据集的划分,地址选择自己数据下的dataSet,注意以下为相对路径 parser.add_argument('--txt_path', default='dataSet', type=str, help='output txt label path') opt = parser.parse_args() trainval_percent = 1.0 # 训练集和验证集所占比例。 这里没有划分测试集 train_percent = 0.9 # 训练集所占比例,可自己进行调整 xmlfilepath = opt.xml_path txtsavepath = opt.txt_path total_xml = os.listdir(xmlfilepath) if not os.path.exists(txtsavepath): os.makedirs(txtsavepath) num = len(total_xml) list_index = range(num) tv = int(num * trainval_percent) tr = int(tv * train_percent) trainval = random.sample(list_index, tv) train = random.sample(trainval, tr) file_trainval = open(txtsavepath + '/trainval.txt', 'w') file_test = open(txtsavepath + '/test.txt', 'w') file_train = open(txtsavepath + '/train.txt', 'w') file_val = open(txtsavepath + '/val.txt', 'w') for i in list_index: name = total_xml[i][:-4] + '\n' if i in trainval: file_trainval.write(name) if i in train: file_train.write(name) else: file_val.write(name) else: file_test.write(name) file_trainval.close() file_train.close() file_val.close() file_test.close()
path: ../mydata
train: # train images (relative to 'path') 16551 images
- dataSet_path/train.txt
val: # val images (relative to 'path') 4952 images
- dataSet_path/val.txt
nc: 3
# Classes
names:
0: helmet
1: head
2: person
import os
os.environ["GIT_PYTHON_REFRESH"] = "quiet"
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。