一、如何将xml文件转化为txt文件:
- 首先存在编译器IDE
- 下载东北大学钢材损耗数据集
- 在数据集文件夹中创建py脚本,如图
- 编写代码,此代码为最终可运行版本。
- import xml.etree.ElementTree as ET
import os
from os import getcwd
from tqdm import tqdm
classes = ["crazing", "inclusion", "patches", "pitted_surface", "rolled-in_scale", "scratches"]
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 = './ANNOTATIONS/%s.xml' % (image_id)
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')
if __name__ == "__main__":
wd = getcwd()
print(wd)
if not os.path.exists('./labels/'):
os.makedirs('./labels/')
image_ids = os.listdir('ANNOTATIONS')
for image_id in tqdm(image_ids):
convert_annotation(image_id.split('.')[0]) - 运行脚本,
产生label文件夹,
产生txt文件,至此,转化工作完成。
下面是对过程中遇到的一些问题的自认为的片面解答
本代码参考了代码实战:YOLOv5实现钢材表面缺陷检测 - 掘金 (juejin.cn),在原本的代码中,我们并没有xml为名的文件夹,语句为打开文件夹寻找xml格式(相对路径),我们的xml格式文件放在了ANNOTATIONS文件夹中,故将其改为
修改完之后会出现ERROR
这个是因为我当时存放py程序的位置不在NEU—DET中,在编辑器中将py文件拖拽到NEU文件夹就可以解决,解决后图为
再次运行,产生
定位为:
通过ChatGPT告知,可能会对字母大小写敏感,
我修改了大小写之后,成功运行
至此,程序可以运行,txt文件应该是这种样子的:
二、配置环境
存在YOLO-master的requirements.TXT文件无法pip安装,如图(例)
会报错:找不到相应的文件
解决方法:将requirements文件改名(换个名字就行),然后采用csdn论坛上的error2解决方法,成功(或者直接pip也行)
PyTorch安装 pip即可,我认为个人的电脑没有必要安装anconda。挂上全局梯子或者采用清华的镜像网站即可。
未来程序的修改将以yolov5——train.py代码【注释、详解、使用教程】-CSDN博客 为基础,进行修改调整,下次数据训练前需要把之前的权重函数文件删去,我现在非常怀疑并没有训练默认的而是训练自己的模型,到时候运行一下detect.py文件即可
关于:
ImportError: Bad git executable.
The git executable must be specified in one of the following ways:
- be included in your $PATH
- be set via $GIT_PYTHON_GIT_EXECUTABLE
- explicitly set via git.refresh()
All git commands will error until this is rectified.
This initial warning can be silenced or aggravated in the future by setting the
$GIT_PYTHON_REFRESH environment variable. Use one of the following values:
- quiet|q|silence|s|none|n|0: for no warning or exception
- warn|w|warning|1: for a printed warning
- error|e|raise|r|2: for a raised exception
Example:
export GIT_PYTHON_REFRESH=quiet
采用了CSDN论坛上的方法
加入
即可。
三、训练和测试
采用csdn论坛中windows+Pycharm+yolov5 配置环境以及本地训练自己的模型-CSDN博客的方法,但是现在遇到了训练自己的数据集,但是程序还是训练的默认自带的数据集。可能是因为某些地方博主没有给明。
三个重点要修改的地方:改为自己数据集的绝对地址,采用相对地址可能会导致数据集不在yolo文件夹中而报错。
同时,yaml文件里也要是绝对地址比较好
YOLOV5训练数据出现:no labels found
将框内改成自己的文件名就行(我这个是已经改过的,因为我的文件名就是这个,只是变了大写,另外 这个只要相应的文件夹名字就行 不用绝对地址)
四、结果
这不可能有什么问题了,这些改改就行
训练
测试结果
看上去好像还可以······