以YOLOv5为基准实现布匹缺陷检测(Fabric Defect Detection)

本文档详细介绍了如何基于YOLOv5框架,结合天池数据集进行布匹缺陷检测的实践。首先,通过命令安装YOLOv5,然后下载并处理数据集,包括图片分类、标签转换和调整图片大小。接着,配置数据和模型参数,并进行模型训练。最后,提供了一些参考资料以供参考。
摘要由CSDN通过智能技术生成

一、YOLOv5 安装

使用以下命令安装最新版的YOLOv5

# 下载代码
git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
# 创建 python 环境
conda create -n yolov5 python=3.8
conda activate yolov5
# 安装依赖项
pip install -r requirements.txt  # install
# 安装显卡版本(RTX 30xx 系列)
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch

二、天池数据集下载

1. 下载数据集

官网中的布匹瑕疵检测数据集仅包括第一轮的数据集,而2019广东工业智造创新大赛【赛场一】季军解决方案 全套代码 则提供了第二轮的代码,下载地址如下:数据集-百度网盘(密码:jp7d)。共16个文件,目录结构如下:
在这里插入图片描述
下载完成后,在项目根目录下创建 train_data 文件夹,将 guangdong1_round2_train2_20191004_Annotations.zip 和 train2_images_1.zip 解压缩后,放入该文件夹。目录结构如下:
在这里插入图片描述

2. 图片分类与标签转换 convertTrainLabel.py

DefectDetection 下载 convertTrainLabel.py,将其放入根目录,运行如下代码,将数据集进行转换。

python convertTrainLabel.py

具体代码如下:

import numpy as np # linear algebra
import os
import json
from tqdm.auto import tqdm
import shutil as sh
import cv2

josn_path = "./train_data/guangdong1_round2_train2_20191004_Annotations/Annotations/anno_train.json"
image_path = "./train_data/guangdong1_round2_train2_20191004_images/defect/"

name_list = []
image_h_list = []
image_w_list = []
c_list = []
w_list = []
h_list = []
x_center_list = []
y_center_list = []

with open(josn_path, 'r') as f:
    temps = tqdm(json.loads(f.read()))
    for temp in temps:
        # image_w = temp["image_width"]
        # image_h = temp["image_height"]
        name = temp["name"].split('.')[0]
        path = os.path.join(image_path, name, temp["name"])
        # print('path: ',path)
        im = cv2.imread(path)
        sp = im.shape
        image_h, image_w = sp[0], sp[1]
        # print("image_h, image_w: ", image_h, image_w)
        # print("defect_name: ",temp["defect_name"])
        #bboxs
        x_l, y_l, x_r, y_r = temp["bbox"]
        # print(temp["name"], temp["bbox"])
        if temp["defect_name"]=="沾污":
            defect_name = '0'
        elif temp["defect_name"]=="错花":
            defect_name = '1'
        elif temp["defect_name"] == "水印":
            defect_name = '2'
        elif temp["defect_name"] == "花毛":
            defect_name = '3'
        elif temp["defect_name"] == "缝头":
            defect_name = '4'
        elif temp["defect_name"] == "缝头印":
            defect_name = '5'
        elif temp["defect_name"] == "虫粘":
            defect_name = '6'
        elif temp["defect_name"] == "破洞":
            defect_name = '7'
        elif temp["defect_name"] == "褶子":
            defect_name = '8'
        elif temp["defect_name"] == "织疵":
            defect_name = '9'
        elif temp["defect_name"] == "漏印":
            defect_name = '10'
        elif temp["defect_name"] == "蜡斑":
            defect_name = '11'
        elif temp["defect_name"] == "色差":
            defect_name = <
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值