使用以下命令安装最新版的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
官网中的布匹瑕疵检测数据集仅包括第一轮的数据集,而2019广东工业智造创新大赛【赛场一】季军解决方案 全套代码 则提供了第二轮的代码,下载地址如下:数据集-百度网盘(密码:jp7d)。共16个文件,目录结构如下:
下载完成后,在项目根目录下创建 train_data 文件夹,将 guangdong1_round2_train2_20191004_Annotations.zip 和 train2_images_1.zip 解压缩后,放入该文件夹。目录结构如下:
从 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 = '12'