目录
目标检测——基于 YOLOv8 实现钢材表面缺陷检测
- 【参考博客】:代码实战:YOLOv5实现钢材表面缺陷检测 - CV技术指南(公众号) - 博客园 (cnblogs.com)
- 【如何使用 YOLOv8】:用 YOLOv8 解决图像检测_Re.Gin的博客-CSDN博客
数据集
下载
-
链接:https://pan.baidu.com/s/1Mg-JZTYTlP8jZKHKk9aYNw?pwd=5sz4
提取码:5sz4 -
含yaml 数据集配置文件、已转换好的 labels 标注文件
结构
datasets
|--NEU-DET
|--annotations
|--xx1.xml
|--images
|--train
|--xx1.png
|--val
|--xx2.png
|--labels
|--train
|--xx1.txt
|--val
|--xx2.txt
转换
- 从官网获取的数据集的标注文件是 VOC(xml 格式)的,我们需要把它转换成 YOLO(txt 格式)的.
- 代码:如果数据集格式是严格按照作者博客【用 YOLOv8 解决图像检测】,代码中的路径无需修改;若数据集格式与作者约定的不同,请自行修改路径
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 = 'datasets/NEU-DET/annotations/%s.xml' % (image_id)
out_file = open('datasets/NEU-DET/labels/train/%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('datasets/NEU-DET/labels/'):
os.makedirs('datasets/NEU-DET/labels/')
image_ids = os.listdir('datasets/NEU-DET/images/train')
for image_id in tqdm(image_ids):
convert_annotation(image_id.split('.')[0])
模型
- 这是作者训练出的模型:
链接:https://pan.baidu.com/s/1CXjCHtsUMlJgdFrFNEoCMw?pwd=qiaz
提取码:qiaz