赞
踩
使用yolov5训练非机动车驾驶状态的模型,检测内容为驾驶非机动车的人和车一体,单独的人或者单独的非机动车不检测,示例如下图所示:
因为网上的数据集,包括coco数据集等,均没有将驾驶状态的非机动车做标注,通常都是独立的人、非机动车目标,所以这里需要重新收集数据再进行重新标注,该示例训练数据集由自己收集、标注,标注内容包括三个类:电动车(摩托车也包含在内)、三轮车、自行车;数据大部分来源于网上的监控视角(主要做监控检测),数据量5000+张,目标框标注超过20000+,数据经过精心筛选,非常适合监控下的非机动车驾驶检测,示例如下所示:
数据标注情况:
相关数据集和代码提供百度云,需要的朋友可自行下载。
链接:https://pan.baidu.com/s/1k-f61kiOiMA8yf-tqgV4GA?pwd=28hw
提取码:28hw
这里默认已经配置好了yolov5运行所需要的环境;
首先准备一个文件夹,文件夹中内容如下,记住该文件夹绝对路径:
首先第一步划分数据集,使用如下代码:
import os import random trainval_percent = 0.1 train_percent = 0.9 xmlfilepath = '上面的文件夹路径/Annotations' txtsavepath = '上面的文件夹路径/images' total_xml = os.listdir(xmlfilepath) 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) ftrainval = open('上面的文件夹路径/ImageSets/trainval.txt', 'w') ftest = open('上面的文件夹路径/ImageSets/test.txt', 'w') ftrain = open('上面的文件夹路径/ImageSets/train.txt', 'w') fval = open('上面的文件夹路径/ImageSets/val.txt', 'w') for i in list: name = total_xml[i][:-4] + '\n' if i in trainval: ftrainval.write(name) if i in train: ftest.write(name) else: fval.write(name) else: ftrain.write(name) ftrainval.close() ftrain.close() fval.close() ftest.close()
第二步将xml转换yolo格式,使用如下代码:
import xml.etree.ElementTree as ET import pickle import os from os import listdir, getcwd from os.path import join sets = ['train', 'test', 'val'] classes = ['electric','bicycle','tricycle'] # 3类标注文件 def convert(size, box): dw = 1. / size[0] dh = 1. / size[1] x = (box[0] + box[1]) / 2.0 y = (box[2] + box[3]) / 2.0 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') # print(in_file) 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 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)) bb = convert((w, h), b) out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') wd = getcwd() print(wd) for image_set in sets: if not os.path.exists('上面的文件夹路径/labels/'): os.makedirs('上面的文件夹路径/labels/') image_ids = open('上面的文件夹路径/ImageSets/%s.txt' % (image_set)).read().strip().split() list_file = open('上面的文件夹路径/%s.txt' % (image_set), 'w') for image_id in image_ids: # print(image_id) list_file.write('上面的文件夹路径/images/%s.jpg\n' % (image_id)) try: convert_annotation(image_id) except: print(image_id) list_file.close()
第三步在yolov5/data中创建一个Nonvehicle.yaml文件,文件内容如下:
# YOLOv5 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/343221?site
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。