钢材表面缺陷检测,yolov8/opencv 使用yolov8训练
钢材表面缺陷检测yolov8/opencv 使用yolov8训练数据集图片4312张已经训练好了也可以自己重新训练使用opencvthinter构建的gui 需要下载相关库如opencvultralytics等 六分类‘crazing’, ‘inclusion’, ‘patches’, ‘pitted_surface’, ‘rolled-in_scale’, ‘scratches’对应中文为 龟裂夹杂物斑块点蚀表面轧入鳞片划痕。含源码、数据集训练好的权重源码注释非常详细 可显示 GUI 窗口支持图片视频实时摄像头检测识别使用YOLOv8进行钢材表面缺陷检测并使用OpenCV和Tkinter构建一个GUI。这个项目包括训练好的权重、数据集、源码和详细的注释。项目结构深色版本steel_defect_detection/├── dataset/│ ├── images/│ ├── labels/├── weights/│ └── best.pt├── src/│ ├── main.py│ ├── utils.py├── requirements.txt└── README.md安装依赖首先确保你已经安装了必要的库。创建一个requirements.txt文件内容如下深色版本opencv-pythonnumpypillowtkinterultralytics然后使用以下命令安装依赖bash深色版本pip install -r requirements.txt2. 数据集和训练好的权重确保你的数据集和训练好的权重已经准备好并放在相应的文件夹中。源码3.1 main.py这是主程序文件负责启动GUI并处理用户输入。python深色版本import tkinter as tkfrom tkinter import filedialog, messageboximport cv2import numpy as npfrom PIL import Image, ImageTkfrom ultralytics import YOLOfrom utils import predict_image, predict_video, predict_camera加载模型model YOLO(‘weights/best.pt’)创建主窗口root tk.Tk()root.title(“钢材表面缺陷检测”)root.geometry(“800x600”)创建画布canvas tk.Canvas(root, width800, height500)canvas.pack()创建按钮def open_image():file_path filedialog.askopenfilename(filetypes[(“Image files”, “.jpg;.png”)])if file_path:image cv2.imread(file_path)result_image predict_image(model, image)show_image(result_image)def open_video():file_path filedialog.askopenfilename(filetypes[(“Video files”, “.mp4;.avi”)])if file_path:predict_video(model, file_path)def open_camera():predict_camera(model)btn_image tk.Button(root, text“打开图片”, commandopen_image)btn_image.pack(sidetk.LEFT, padx10, pady10)btn_video tk.Button(root, text“打开视频”, commandopen_video)btn_video.pack(sidetk.LEFT, padx10, pady10)btn_camera tk.Button(root, text“打开摄像头”, commandopen_camera)btn_camera.pack(sidetk.LEFT, padx10, pady10)显示图像def show_image(image):image cv2.cvtColor(image, cv2.COLOR_BGR2RGB)image Image.fromarray(image)image ImageTk.PhotoImage(image)canvas.create_image(0, 0, anchortk.NW, imageimage)canvas.image image运行主循环root.mainloop()3.2 utils.py这个文件包含了预测图像、视频和摄像头的函数。python深色版本import cv2import numpy as npfrom ultralytics import YOLO定义类别名称class_names [‘crazing’, ‘inclusion’, ‘patches’, ‘pitted_surface’, ‘rolled-in_scale’, ‘scratches’]class_names_chinese [‘龟裂’, ‘夹杂物’, ‘斑块’, ‘点蚀表面’, ‘轧入鳞片’, ‘划痕’]预测图像def predict_image(model, image):results model(image)for result in results:boxes result.boxesfor box in boxes:x1, y1, x2, y2 map(int, box.xyxy[0])conf box.conf[0].item()cls int(box.cls[0].item())label f’{class_names_chinese[cls]} {conf:.2f}’cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)return image预测视频def predict_video(model, video_path):cap cv2.VideoCapture(video_path)while cap.isOpened():ret, frame cap.read()if not ret:breakresult_frame predict_image(model, frame)cv2.imshow(‘Steel Defect Detection’, result_frame)if cv2.waitKey(1) 0xFF ord(‘q’):breakcap.release()cv2.destroyAllWindows()预测摄像头def predict_camera(model):cap cv2.VideoCapture(0)while True:ret, frame cap.read()if not ret:breakresult_frame predict_image(model, frame)cv2.imshow(‘Steel Defect Detection’, result_frame)if cv2.waitKey(1) 0xFF ord(‘q’):breakcap.release()cv2.destroyAllWindows()4. 运行项目确保你的数据集和训练好的权重已经放在相应的文件夹中。在项目根目录下运行以下命令启动GUIbash深色版本python src/main.py5. 功能说明打开图片点击“打开图片”按钮选择一张图片进行缺陷检测结果显示在GUI窗口中。打开视频点击“打开视频”按钮选择一个视频文件进行缺陷检测结果显示在单独的OpenCV窗口中。打开摄像头点击“打开摄像头”按钮使用摄像头进行实时缺陷检测结果显示在单独的OpenCV窗口中。6. 详细注释main.py导入库导入必要的库包括Tkinter、OpenCV、NumPy、Pillow和YOLOv8。加载模型使用YOLO类加载训练好的权重。创建主窗口使用Tkinter创建主窗口设置窗口标题和大小。创建画布在主窗口中创建一个画布用于显示检测结果。创建按钮创建三个按钮分别用于打开图片、视频和摄像头。显示图像定义一个函数show_image用于将检测结果图像显示在画布上。运行主循环启动Tkinter的主循环。utils.py定义类别名称定义英文和中文的类别名称。预测图像定义一个函数predict_image用于对单张图像进行缺陷检测并在图像上绘制检测框和标签。预测视频定义一个函数predict_video用于对视频文件进行缺陷检测并在OpenCV窗口中显示结果。预测摄像头定义一个函数predict_camera用于使用摄像头进行实时缺陷检测并在OpenCV窗口中显示结果。