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


    一、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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    二、天池数据集下载

    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
    
    • 1

    具体代码如下:

    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 = '12'
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
  • 相关阅读:
    计算机毕业设计选题推荐-掌心办公微信小程序/安卓APP-项目实战
    域名的理解
    荐书丨《哥德尔、艾舍尔、巴赫书:集异璧之大成》:机器人与音乐的次元壁破了
    解决Spring Cloud整合Nacos与Gateway的探险之旅
    手机号码携号转网查询保障用户权益、信息透明、优化用户体验
    【Python】os模块路径处理
    分发糖果(贪心算法)
    Spring事务里required里多线程调用required_new方法到底符不符合预期
    Django-ORM 单表查询
    C++面经总结1——类相关
  • 原文地址:https://blog.csdn.net/zhuxiaoyang2000/article/details/126756305