基于深度学习YOLOv10的麻将识别检测系统(YOLOv10+YOLO数据集+UI界面+Python项目源码+模型)
本项目基于YOLOv10目标检测算法开发了一套专业的麻将牌识别检测系统,旨在实现对各类麻将牌的高精度识别与定位。系统能够准确识别42种不同类型的麻将牌,包括万、条、筒、风牌和箭牌等常见麻将类别。项目采用深度学习技术,通过精心构建的数据集进行模型训练,最终实现了在复杂背景下对麻将牌的快速、准确识别。该系统可应用于麻将游戏自动记分、麻将教学辅助、智能麻将机开发等多个领域,具有广泛的应用前景和商业价值。
一、项目介绍
摘要
本项目基于YOLOv10目标检测算法开发了一套专业的麻将牌识别检测系统,旨在实现对各类麻将牌的高精度识别与定位。系统能够准确识别42种不同类型的麻将牌,包括万、条、筒、风牌和箭牌等常见麻将类别。项目采用深度学习技术,通过精心构建的数据集进行模型训练,最终实现了在复杂背景下对麻将牌的快速、准确识别。该系统可应用于麻将游戏自动记分、麻将教学辅助、智能麻将机开发等多个领域,具有广泛的应用前景和商业价值。
项目意义
麻将作为中国传统文化的重要组成部分,在全球范围内拥有数亿爱好者。开发麻将自动识别系统具有多重重要意义:
-
文化传承与数字化:通过AI技术将传统麻将游戏数字化,有助于麻将文化的现代化传承和全球推广。
-
智能游戏开发:为线上麻将游戏提供自动识别功能,提升游戏体验和公平性,减少人为记分错误。
-
教学辅助工具:可作为麻将新手学习的辅助工具,自动识别牌面并提供规则提示。
-
竞技比赛应用:在专业麻将比赛中实现自动记分和牌局分析,提高比赛的专业性和效率。
-
智能硬件集成:为自动麻将机等智能硬件提供核心识别技术,推动传统娱乐设备的智能化升级。
-
计算机视觉研究:为特定领域的目标检测提供实践案例,特别是对相似物体的细粒度识别具有研究价值。
目录
基于深度学习YOLOv10的麻将识别检测系统(YOLOv10+YOLO数据集+UI界面+Python项目源码+模型)_哔哩哔哩_bilibili
基于深度学习YOLOv10的麻将识别检测系统(YOLOv10+YOLO数据集+UI界面+Python项目源码+模型)
二、项目功能展示
系统功能
✅ 图片检测:可对图片进行检测,返回检测框及类别信息。
✅ 视频检测:支持视频文件输入,检测视频中每一帧的情况。
✅ 摄像头实时检测:连接USB 摄像头,实现实时监测。
✅参数实时调节(置信度和IoU阈值)
-
图片检测
该功能允许用户通过单张图片进行目标检测。输入一张图片后,YOLO模型会实时分析图像,识别出其中的目标,并在图像中框出检测到的目标,输出带有目标框的图像。
-
视频检测
视频检测功能允许用户将视频文件作为输入。YOLO模型将逐帧分析视频,并在每一帧中标记出检测到的目标。最终结果可以是带有目标框的视频文件或实时展示,适用于视频监控和分析等场景。
-
摄像头实时检测
该功能支持通过连接摄像头进行实时目标检测。YOLO模型能够在摄像头拍摄的实时视频流中进行目标检测,实时识别并显示检测结果。此功能非常适用于安防监控、无人驾驶、智能交通等应用,提供即时反馈。
核心特点:
- 高精度:基于YOLO模型,提供精确的目标检测能力,适用于不同类型的图像和视频。
- 实时性:特别优化的算法使得实时目标检测成为可能,无论是在视频还是摄像头实时检测中,响应速度都非常快。
- 批量处理:支持高效的批量图像和视频处理,适合大规模数据分析。
三、数据集介绍
数据集概述
本项目构建了一个专业的大规模麻将识别数据集,共包含6731张高质量标注图像,具体划分为:
-
训练集:5565张图像
-
验证集:684张图像
-
测试集:482张图像
数据集涵盖了42类不同的麻将牌,每类麻将牌在不同角度、光照条件和背景下的多种表现形式,确保了模型的泛化能力。
数据集特点
-
类别全面性:包含所有常见麻将牌类型,从1-9的万(B)、条(C)、筒(D),到东南西北风(F/S/W/NW/EW/SW),以及中发白(GD/RD/WW)等箭牌。
-
多样性表现:
-
多种拍摄角度:正视角、侧视角、倾斜角度等
-
不同光照条件:自然光、室内灯光、强光、弱光等
-
复杂背景:木质桌面、布料背景、手持场景等
-
不同排列方式:单张、多张重叠、牌墙等
-
-
高质量标注:所有图像均经过专业标注团队手工标注,确保边界框精确,标注错误率低。
-
数据平衡性:通过数据增强技术确保各类别样本数量相对均衡,避免模型偏见。
数据集配置文件
数据集采用YOLO格式的配置文件,主要包含:
-
train: F:\麻将识别检测数据集\train\images val: F:\麻将识别检测数据集\valid\images test: F:\麻将识别检测数据集\test\images nc: 42 names: ['1B', '1C', '1D', '1F', '1S', '2B', '2C', '2D', '2F', '2S', '3B', '3C', '3D', '3F', '3S', '4B', '4C', '4D', '4F', '4S', '5B', '5C', '5D', '6B', '6C', '6D', '7B', '7C', '7D', '8B', '8C', '8D', '9B', '9C', '9D', 'EW', 'GD', 'NW', 'RD', 'SW', 'WD', 'WW']
数据集制作流程
-
数据采集:
-
使用多款不同品牌、材质的实体麻将牌
-
在不同环境下使用高清摄像头拍摄
-
包含专业比赛场景和日常娱乐场景
-
-
数据清洗:
-
去除模糊、过暗或过亮的无效图像
-
检查并删除重复图像
-
确保各类别样本数量基本平衡
-
-
数据标注:
-
使用LabelImg等专业标注工具
-
由麻将专业人士指导标注,确保类别准确
-
边界框紧密贴合牌面边缘
-
多人交叉验证标注质量
-
-
数据增强:
-
几何变换:旋转、缩放、平移
-
色彩调整:亮度、对比度、饱和度变化
-
添加噪声:高斯噪声、椒盐噪声
-
背景合成:将麻将牌合成到不同背景中
-
-
数据集划分:
-
按约8:1:1比例划分训练集、验证集和测试集
-
确保各子集中各类别分布一致
-
测试集包含特别挑战性的样本(如重叠牌、部分遮挡)
-
-
质量验证:
-
进行多轮人工检查
-
使用初步模型检测标注错误
-
四、项目环境配置
创建虚拟环境
首先新建一个Anaconda环境,每个项目用不同的环境,这样项目中所用的依赖包互不干扰。
终端输入
conda create -n yolov10 python==3.9
激活虚拟环境
conda activate yolov10
安装cpu版本pytorch
pip install torch torchvision torchaudio
pycharm中配置anaconda
安装所需要库
pip install -r requirements.txt
五、模型训练
训练代码
from ultralytics import YOLOv10
model_path = 'yolov10s.pt'
data_path = 'datasets/data.yaml'
if __name__ == '__main__':
model = YOLOv10(model_path)
results = model.train(data=data_path,
epochs=500,
batch=64,
device='0',
workers=0,
project='runs/detect',
name='exp',
)
根据实际情况更换模型 yolov10n.yaml (nano):轻量化模型,适合嵌入式设备,速度快但精度略低。 yolov10s.yaml (small):小模型,适合实时任务。 yolov10m.yaml (medium):中等大小模型,兼顾速度和精度。 yolov10b.yaml (base):基本版模型,适合大部分应用场景。 yolov10l.yaml (large):大型模型,适合对精度要求高的任务。
--batch 64
:每批次64张图像。--epochs 500
:训练500轮。--datasets/data.yaml
:数据集配置文件。--weights yolov10s.pt
:初始化模型权重,yolov10s.pt
是预训练的轻量级YOLO模型。
训练结果
六、核心代码
import sys
import cv2
import numpy as np
from PyQt5.QtWidgets import QApplication, QMessageBox, QFileDialog
from PyQt5.QtCore import QThread, pyqtSignal
from ultralytics import YOLOv10
from UiMain import UiMainWindow
import time
import os
class DetectionThread(QThread):
frame_received = pyqtSignal(np.ndarray, np.ndarray, list) # 原始帧, 检测帧, 检测结果
finished_signal = pyqtSignal() # 线程完成信号
def __init__(self, model, source, conf, iou, parent=None):
super().__init__(parent)
self.model = model
self.source = source
self.conf = conf
self.iou = iou
self.running = True
def run(self):
try:
if isinstance(self.source, int) or self.source.endswith(('.mp4', '.avi', '.mov')): # 视频或摄像头
cap = cv2.VideoCapture(self.source)
while self.running and cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 保存原始帧
original_frame = frame.copy()
# 检测
results = self.model(frame, conf=self.conf, iou=self.iou)
annotated_frame = results[0].plot()
# 提取检测结果
detections = []
for result in results:
for box in result.boxes:
class_id = int(box.cls)
class_name = self.model.names[class_id]
confidence = float(box.conf)
x, y, w, h = box.xywh[0].tolist()
detections.append((class_name, confidence, x, y))
# 发送信号
self.frame_received.emit(
cv2.cvtColor(original_frame, cv2.COLOR_BGR2RGB),
cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB),
detections
)
# 控制帧率
time.sleep(0.03) # 约30fps
cap.release()
else: # 图片
frame = cv2.imread(self.source)
if frame is not None:
original_frame = frame.copy()
results = self.model(frame, conf=self.conf, iou=self.iou)
annotated_frame = results[0].plot()
# 提取检测结果
detections = []
for result in results:
for box in result.boxes:
class_id = int(box.cls)
class_name = self.model.names[class_id]
confidence = float(box.conf)
x, y, w, h = box.xywh[0].tolist()
detections.append((class_name, confidence, x, y))
self.frame_received.emit(
cv2.cvtColor(original_frame, cv2.COLOR_BGR2RGB),
cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB),
detections
)
except Exception as e:
print(f"Detection error: {e}")
finally:
self.finished_signal.emit()
def stop(self):
self.running = False
class MainWindow(UiMainWindow):
def __init__(self):
super().__init__()
# 初始化模型
self.model = None
self.detection_thread = None
self.current_image = None
self.current_result = None
self.video_writer = None
self.is_camera_running = False
self.is_video_running = False
self.last_detection_result = None # 新增:保存最后一次检测结果
# 连接按钮信号
self.image_btn.clicked.connect(self.detect_image)
self.video_btn.clicked.connect(self.detect_video)
self.camera_btn.clicked.connect(self.detect_camera)
self.stop_btn.clicked.connect(self.stop_detection)
self.save_btn.clicked.connect(self.save_result)
# 初始化模型
self.load_model()
def load_model(self):
try:
model_name = self.model_combo.currentText()
self.model = YOLOv10(f"{model_name}.pt") # 自动下载或加载本地模型
self.update_status(f"模型 {model_name} 加载成功")
except Exception as e:
QMessageBox.critical(self, "错误", f"模型加载失败: {str(e)}")
self.update_status("模型加载失败")
def detect_image(self):
if self.detection_thread and self.detection_thread.isRunning():
QMessageBox.warning(self, "警告", "请先停止当前检测任务")
return
file_path, _ = QFileDialog.getOpenFileName(
self, "选择图片", "", "图片文件 (*.jpg *.jpeg *.png *.bmp)")
if file_path:
self.clear_results()
self.current_image = cv2.imread(file_path)
self.current_image = cv2.cvtColor(self.current_image, cv2.COLOR_BGR2RGB)
self.display_image(self.original_image_label, self.current_image)
# 创建检测线程
conf = self.confidence_spinbox.value()
iou = self.iou_spinbox.value()
self.detection_thread = DetectionThread(self.model, file_path, conf, iou)
self.detection_thread.frame_received.connect(self.on_frame_received)
self.detection_thread.finished_signal.connect(self.on_detection_finished)
self.detection_thread.start()
self.update_status(f"正在检测图片: {os.path.basename(file_path)}")
def detect_video(self):
if self.detection_thread and self.detection_thread.isRunning():
QMessageBox.warning(self, "警告", "请先停止当前检测任务")
return
file_path, _ = QFileDialog.getOpenFileName(
self, "选择视频", "", "视频文件 (*.mp4 *.avi *.mov)")
if file_path:
self.clear_results()
self.is_video_running = True
# 初始化视频写入器
cap = cv2.VideoCapture(file_path)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
cap.release()
# 创建保存路径
save_dir = "results"
os.makedirs(save_dir, exist_ok=True)
timestamp = time.strftime("%Y%m%d_%H%M%S")
save_path = os.path.join(save_dir, f"result_{timestamp}.mp4")
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
self.video_writer = cv2.VideoWriter(save_path, fourcc, fps, (frame_width, frame_height))
# 创建检测线程
conf = self.confidence_spinbox.value()
iou = self.iou_spinbox.value()
self.detection_thread = DetectionThread(self.model, file_path, conf, iou)
self.detection_thread.frame_received.connect(self.on_frame_received)
self.detection_thread.finished_signal.connect(self.on_detection_finished)
self.detection_thread.start()
self.update_status(f"正在检测视频: {os.path.basename(file_path)}")
def detect_camera(self):
if self.detection_thread and self.detection_thread.isRunning():
QMessageBox.warning(self, "警告", "请先停止当前检测任务")
return
self.clear_results()
self.is_camera_running = True
# 创建检测线程 (默认使用摄像头0)
conf = self.confidence_spinbox.value()
iou = self.iou_spinbox.value()
self.detection_thread = DetectionThread(self.model, 0, conf, iou)
self.detection_thread.frame_received.connect(self.on_frame_received)
self.detection_thread.finished_signal.connect(self.on_detection_finished)
self.detection_thread.start()
self.update_status("正在从摄像头检测...")
def stop_detection(self):
if self.detection_thread and self.detection_thread.isRunning():
self.detection_thread.stop()
self.detection_thread.quit()
self.detection_thread.wait()
if self.video_writer:
self.video_writer.release()
self.video_writer = None
self.is_camera_running = False
self.is_video_running = False
self.update_status("检测已停止")
def on_frame_received(self, original_frame, result_frame, detections):
# 更新原始图像和结果图像
self.display_image(self.original_image_label, original_frame)
self.display_image(self.result_image_label, result_frame)
# 保存当前结果帧用于后续保存
self.last_detection_result = result_frame # 新增:保存检测结果
# 更新表格
self.clear_results()
for class_name, confidence, x, y in detections:
self.add_detection_result(class_name, confidence, x, y)
# 保存视频帧
if self.video_writer:
self.video_writer.write(cv2.cvtColor(result_frame, cv2.COLOR_RGB2BGR))
def on_detection_finished(self):
if self.video_writer:
self.video_writer.release()
self.video_writer = None
self.update_status("视频检测完成,结果已保存")
elif self.is_camera_running:
self.update_status("摄像头检测已停止")
else:
self.update_status("图片检测完成")
def save_result(self):
if not hasattr(self, 'last_detection_result') or self.last_detection_result is None:
QMessageBox.warning(self, "警告", "没有可保存的检测结果")
return
save_dir = "results"
os.makedirs(save_dir, exist_ok=True)
timestamp = time.strftime("%Y%m%d_%H%M%S")
if self.is_camera_running or self.is_video_running:
# 保存当前帧为图片
save_path = os.path.join(save_dir, f"snapshot_{timestamp}.jpg")
cv2.imwrite(save_path, cv2.cvtColor(self.last_detection_result, cv2.COLOR_RGB2BGR))
self.update_status(f"截图已保存: {save_path}")
else:
# 保存图片检测结果
save_path = os.path.join(save_dir, f"result_{timestamp}.jpg")
cv2.imwrite(save_path, cv2.cvtColor(self.last_detection_result, cv2.COLOR_RGB2BGR))
self.update_status(f"检测结果已保存: {save_path}")
def closeEvent(self, event):
self.stop_detection()
event.accept()
if __name__ == "__main__":
app = QApplication(sys.argv)
# 设置应用程序样式
app.setStyle("Fusion")
# 创建并显示主窗口
window = MainWindow()
window.show()
sys.exit(app.exec_())
七、项目源码
基于深度学习YOLOv10的麻将识别检测系统(YOLOv10+YOLO数据集+UI界面+Python项目源码+模型)_哔哩哔哩_bilibili
基于深度学习YOLOv10的麻将识别检测系统(YOLOv10+YOLO数据集+UI界面+Python项目源码+模型)

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)