引言
在当今社会,吸烟行为对公共健康的影响不容忽视。基于深度学习的吸烟行为检测系统可以有效地监控和分析吸烟行为,从而为公共健康管理提供重要的数据支持。本文将详细介绍一个基于YOLO(You Only Look Once)深度学习模型的吸烟行为检测系统的构建过程,涵盖数据集准备、模型训练、用户界面设计及其实现代码。
项目流程
目录
首先,确保您的项目目录结构如下所示:
smoking_detection/
├── dataset/
│ ├── images/
│ │ ├── train/
│ │ ├── val/
│ │ └── test/
│ ├── labels/
│ │ ├── train/
│ │ ├── val/
│ │ └── test/
│ └── smoking_dataset.yaml
├── yolov5/
│ ├── train.py
│ ├── detect.py
│ ├── utils/
│ └── ... # 其他YOLO文件
├── smoking_detection_ui.py
└── best.pt # 训练好的模型
1. 数据集准备
在 dataset/
文件夹中,准备您的图像和标签。每个图像应具有对应的YOLO格式标签文件。您可以使用以下示例创建 smoking_dataset.yaml
文件:
# smoking_dataset.yaml
train: ./images/train
val: ./images/val
nc: 2
names: ['smoking', 'not_smoking']
2. YOLO模型训练
在 yolov5
文件夹下,使用以下代码训练YOLO模型。首先,确保已安装YOLOv5并准备好数据集。接下来,运行以下命令:
python train.py --img 640 --batch 16 --epochs 50 --data ../dataset/smoking_dataset.yaml --weights yolov5s.pt
3. 用户界面设计
下面是完整的用户界面代码,使用Tkinter库构建,支持上传图像和实时摄像头检测。
# smoking_detection_ui.py
import tkinter as tk
from tkinter import filedialog, messagebox
import torch
import cv2
import numpy as np
from PIL import Image, ImageTk
class SmokingDetectionApp:
def __init__(self, root, model_path):
self.root = root
self.root.title("吸烟行为检测系统")
self.model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path, force_reload=True)
# 创建画布
self.canvas = tk.Canvas(root, width=640, height=480)
self.canvas.pack()
# 创建按钮
self.upload_button = tk.Button(root, text="上传图片", command=self.upload_image)
self.upload_button.pack(side=tk.LEFT)
self.camera_button = tk.Button(root, text="摄像头检测", command=self.detect_from_camera)
self.camera_button.pack(side=tk.LEFT)
# 创建结果标签
self.result_label = tk.Label(root, text="检测结果:")
self.result_label.pack()
def upload_image(self):
file_path = filedialog.askopenfilename()
if file_path:
img = Image.open(file_path)
img = img.resize((640, 480))
self.img_tk = ImageTk.PhotoImage(img)
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.img_tk)
# YOLO模型检测
self.perform_detection(img)
def detect_from_camera(self):
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# YOLO模型检测
results = self.model(frame)
# 将检测结果绘制在帧上
self.draw_boxes(frame, results)
# 将BGR转为RGB以适应PIL图像
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img_pil = Image.fromarray(frame)
img_tk = ImageTk.PhotoImage(img_pil)
self.canvas.create_image(0, 0, anchor=tk.NW, image=img_tk)
# 更新界面
self.root.update()
cap.release()
def perform_detection(self, img):
results = self.model(np.array(img))
detection_results = results.pandas().xyxy[0]
self.result_label.config(text=f"检测结果:\n{detection_results}")
def draw_boxes(self, frame, results):
for i in range(len(results.xyxy[0])):
x1, y1, x2, y2, conf, cls = results.xyxy[0][i]
label = f"{self.model.names[int(cls)]} {conf:.2f}"
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
cv2.putText(frame, label, (int(x1), int(y1)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# 启动应用
if __name__ == "__main__":
root = tk.Tk()
app = SmokingDetectionApp(root, model_path='best.pt') # 请根据您的模型路径修改
root.mainloop()
4. 评估模型
使用以下代码评估模型性能,可以通过混淆矩阵、精度、召回率和F1分数等指标进行评估。确保在项目中导入sklearn
和seaborn
库:
pip install seaborn scikit-learn
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
# 假设y_true为真实标签,y_pred为模型预测标签
def plot_confusion_matrix(y_true, y_pred):
cm = confusion_matrix(y_true, y_pred)
sns.heatmap(cm, annot=True, fmt='d')
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()
5. 完整训练流程
在训练完成后,您可以使用以下命令进行模型测试:
python val.py --data ../dataset/smoking_dataset.yaml --weights best.pt
确保根据您的数据集和模型路径进行相应调整。
项目概述
该项目的目标是构建一个能够实时检测吸烟行为的系统。系统将使用YOLOv5、YOLOv6、YOLOv7、YOLOv8、YOLOv10等深度学习模型进行目标检测,采用Tkinter库构建图形用户界面(GUI),支持用户上传图像或使用摄像头进行实时检测。
环境准备
1.1 安装Python及相关库
确保安装了Python 3.x,并通过以下命令安装必要的库:
pip install torch torchvision torchaudio
pip install opencv-python
pip install numpy
pip install matplotlib
pip install tqdm
pip install Pillow
pip install tkinter
1.2 下载YOLO模型
在命令行中执行以下命令下载YOLO模型:
git clone https://github.com/ultralytics/yolov5.git
cd yolov5
pip install -r requirements.txt
数据集准备
2.1 数据集格式
为了训练吸烟行为检测模型,需要准备带标签的图像数据集。数据集应包括吸烟和非吸烟两类样本。每类图像应具有相应的YOLO格式标签文件。
数据集结构应如下所示:
dataset/
├── images/
│ ├── train/
│ ├── val/
│ └── test/
├── labels/
│ ├── train/
│ ├── val/
│ └── test/
└── smoking_dataset.yaml
2.2 创建YAML配置文件
在项目目录下创建 smoking_dataset.yaml
文件,内容如下:
train: ./images/train
val: ./images/val
nc: 2
names: ['smoking', 'not_smoking']
2.3 数据集示例
以下是吸烟和非吸烟图像的示例:
-
吸烟样本:
-
非吸烟样本:
确保在实际项目中使用真实的图像。
模型训练
3.1 训练模型
在yolov5
目录下,使用以下命令训练模型:
python train.py --img 640 --batch 16 --epochs 50 --data smoking_dataset.yaml --weights yolov5s.pt
--img
:输入图像的大小。--batch
:每个训练批次的样本数。--epochs
:训练的总轮数。--data
:配置文件路径。--weights
:预训练模型的路径。
3.2 训练日志
在训练过程中,控制台将输出训练日志,包括损失函数、精度等信息。确保根据这些信息调整超参数以优化模型性能。
用户界面设计
4.1 UI设计
本项目使用Tkinter库创建用户界面。用户可以通过界面上传图片或直接使用摄像头进行实时检测。
4.2 UI代码实现
以下是完整的UI代码实现:
import tkinter as tk
from tkinter import filedialog, messagebox
import torch
import cv2
import numpy as np
from PIL import Image, ImageTk
class SmokingDetectionApp:
def __init__(self, root, model_path):
self.root = root
self.root.title("吸烟行为检测系统")
self.model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path, force_reload=True)
# 创建画布
self.canvas = tk.Canvas(root, width=640, height=480)
self.canvas.pack()
# 创建按钮
self.upload_button = tk.Button(root, text="上传图片", command=self.upload_image)
self.upload_button.pack(side=tk.LEFT)
self.camera_button = tk.Button(root, text="摄像头检测", command=self.detect_from_camera)
self.camera_button.pack(side=tk.LEFT)
# 创建结果标签
self.result_label = tk.Label(root, text="检测结果:")
self.result_label.pack()
def upload_image(self):
file_path = filedialog.askopenfilename()
if file_path:
img = Image.open(file_path)
img = img.resize((640, 480))
self.img_tk = ImageTk.PhotoImage(img)
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.img_tk)
# YOLO模型检测
self.perform_detection(img)
def detect_from_camera(self):
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# YOLO模型检测
results = self.model(frame)
# 将检测结果绘制在帧上
self.draw_boxes(frame, results)
# 将BGR转为RGB以适应PIL图像
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img_pil = Image.fromarray(frame)
img_tk = ImageTk.PhotoImage(img_pil)
self.canvas.create_image(0, 0, anchor=tk.NW, image=img_tk)
# 更新界面
self.root.update()
cap.release()
def perform_detection(self, img):
results = self.model(np.array(img))
detection_results = results.pandas().xyxy[0]
self.result_label.config(text=f"检测结果:\n{detection_results}")
def draw_boxes(self, frame, results):
for i in range(len(results.xyxy[0])):
x1, y1, x2, y2, conf, cls = results.xyxy[0][i]
label = f"{self.model.names[int(cls)]} {conf:.2f}"
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
cv2.putText(frame, label, (int(x1), int(y1)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# 启动应用
if __name__ == "__main__":
root = tk.Tk()
app = SmokingDetectionApp(root, model_path='best.pt') # 请根据您的模型路径修改
root.mainloop()
完整代码实现
5.1 完整项目结构
以下是项目的完整文件结构示例:
smoking_detection/
├── dataset/
│ ├── images/
│ │ ├── train/
│ │ ├── val/
│ │ └── test/
│ ├── labels/
│ │ ├── train/
│ │ ├── val/
│ │ └── test/
│ └── smoking_dataset.yaml
├── yolov5/
│ ├── train.py
│ ├── detect.py
│ ├── utils/
│ └── ... # 其他YOLO文件
├── smoking_detection_ui.py
└── best.pt # 训练好的模型
5.2 训练过程
确保在训练过程中监控模型性能,使用以下命令评估模型效果:
python val.py --data smoking_dataset.yaml --weights best.pt
模型测试与评估
6.1 测试模型
在训练完成后,使用不同的测试图像和视频评估模型性能。可以使用混淆矩阵、精度、召回率和F1分数等指标评估模型的检测能力。
6.2 性能评估
通过绘制混淆矩阵来可视化模型的性能。可以使用以下代码生成混淆矩阵:
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
# 假设y_true为真实标签,y_pred为模型预测标签
cm = confusion_matrix(y_true, y_pred)
sns.heatmap(cm, annot=True, fmt='d')
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()