基于YOLOV5的手势识别检测系统
基于YOLOV5的手势识别检测系统项目介绍软件PycharmAnaconda环境python3.8 opencv_python PyQt5文件1.完整程序文件.py等2.UI界面源文件、图标.ui、.qrc、.py等3.测试图片、视频文件.jpeg、.mp4、.avi等功能 系统实现了对于10种手势的识别检测功能10种手势为’A’, ‘7’,‘D’, ‘I’, ‘L’, ‘V’, ‘W’, Y, ‘I love you’, ‘5’包括通过选择图片、视频进行实时识别检测速度快、识别精度较高。实现一个基于 YOLOv5 的手势识别检测系统。以下是详细的步骤数据准备收集和准备手势数据集。环境部署安装必要的库。模型训练使用 YOLOv5 训练目标检测模型。评估模型评估训练好的模型性能。PyQt5 GUI 开发创建一个简单的 GUI 来加载和运行模型进行实时预测。**提示文章代码仅供参考**数据准备假设你已经有一个包含 10 种手势的数据集并且标注格式为 YOLO 格式的 TXT 文件。数据集结构示例dataset/ ├── images/ │ ├── train/ │ │ ├── image1.jpg │ │ ├── image2.jpg │ │ └── ... │ ├── test/ │ │ ├── image3.jpg │ │ ├── image4.jpg │ │ └── ... │ └── valid/ │ ├── image5.jpg │ ├── image6.jpg │ └── ... ├── labels/ │ ├── train/ │ │ ├── image1.txt │ │ ├── image2.txt │ │ └── ... │ ├── test/ │ │ ├── image3.txt │ │ ├── image4.txt │ │ └── ... │ └── valid/ │ ├── image5.txt │ ├── image6.txt │ └── ... └── dataset.yamldataset.yaml内容如下train:./images/trainval:./images/validtest:./images/testnc:10names:[A,7,D,I,L,V,W,Y,I_love_you,5]每个图像对应的标签文件是一个文本文件每行表示一个边界框格式为class_id x_center y_center width height环境部署说明确保你已经安装了必要的库如上所述。安装依赖# 创建虚拟环境可选conda create-ngesture_recognition_envpython3.8conda activate gesture_recognition_env# 安装PyTorchpipinstalltorch1.9torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu111# 安装其他依赖pipinstallopencv-python pyqt5 ultralytics scikit-learn pandas matplotlib seaborn onnxruntime xml.etree.ElementTree模型训练权重和指标可视化展示我们将使用 YOLOv5 进行目标检测任务。下载 YOLOv5 仓库gitclone https://github.com/ultralytics/yolov5cdyolov5 pipinstall-rrequirements.txt训练 YOLOv5[titleTraining YOLOv5 for Gesture Recognition]importosfrompathlibimportPath# Define pathsdataset_pathpath/to/datasetweights_pathruns/train/exp/weights/best.pt# Create dataset.yamlyaml_contentf train:{Path(dataset_path)/images/train}val:{Path(dataset_path)/images/valid}test:{Path(dataset_path)/images/test}nc: 10 names: [A, 7, D, I, L, V, W, Y, I_love_you, 5] withopen(Path(dataset_path)/dataset.yaml,w)asf:f.write(yaml_content)# Train YOLOv5!python train.py--img640--batch16--epochs100--data{Path(dataset_path)/dataset.yaml}--cfg yolov5s.yaml--weights yolov5s.pt--name exp请将path/to/dataset替换为实际的数据集路径。模型评估我们将使用 YOLOv5 提供的评估功能来评估训练好的模型性能。评估 YOLOv5 模型[titleEvaluating YOLOv5 Model for Gesture Recognition]frompathlibimportPathfromultralytics.yolo.engine.trainerimportTrainer# Load the trained modelmodel_pathruns/train/exp/weights/best.pt# Evaluate the modeltrainerTrainer(overrides{task:detect,mode:val,data:path/to/dataset/dataset.yaml,weights:model_path})resultstrainer.val()# Print evaluation resultsmetricsresults.metricsprint(metrics)请将path/to/dataset替换为实际的数据集路径。使用说明配置路径将path/to/dataset设置为存放数据集的目录路径。确保runs/train/exp/weights/best.pt是训练好的 YOLOv5 模型权重路径。运行脚本在终端中运行train_yolov5.py脚本来训练模型。在终端中运行evaluate_yolov5.py来评估模型性能。注意事项确保所有必要的工具箱已安装特别是 PyTorch 和 ultralytics。根据需要调整参数如epochs和imgsz。PyQt5 GUI 开发我们将使用 PyQt5 创建一个简单的 GUI 来加载和运行 YOLOv5 模型进行实时预测。主窗口代码main_window.py[titlePyQt5 Main Window for Gesture Recognition]importsysimportcv2importnumpyasnpfromPyQt5.QtWidgetsimportQApplication,QMainWindow,QLabel,QPushButton,QVBoxLayout,QWidget,QFileDialogfromPyQt5.QtGuiimportQImage,QPixmapfromPyQt5.QtCoreimportQt,QTimerfromultralyticsimportYOLOclassMainWindow(QMainWindow):def__init__(self):super().__init__()self.setWindowTitle(Gesture Recognition System)self.setGeometry(100,100,800,600)self.modelYOLO(runs/train/exp/weights/best.pt)self.initUI()definitUI(self):self.central_widgetQWidget()self.setCentralWidget(self.central_widget)self.layoutQVBoxLayout()self.image_labelQLabel(self)self.image_label.setAlignment(Qt.AlignCenter)self.layout.addWidget(self.image_label)self.load_image_buttonQPushButton(Load Image,self)self.load_image_button.clicked.connect(self.load_image)self.layout.addWidget(self.load_image_button)self.load_video_buttonQPushButton(Load Video,self)self.load_video_button.clicked.connect(self.load_video)self.layout.addWidget(self.load_video_button)self.start_detection_buttonQPushButton(Start Detection,self)self.start_detection_button.clicked.connect(self.start_detection)self.layout.addWidget(self.start_detection_button)self.stop_detection_buttonQPushButton(Stop Detection,self)self.stop_detection_button.clicked.connect(self.stop_detection)self.layout.addWidget(self.stop_detection_button)self.central_widget.setLayout(self.layout)self.capNoneself.timerQTimer()self.timer.timeout.connect(self.update_frame)defload_image(self):optionsQFileDialog.Options()file_name,_QFileDialog.getOpenFileName(self,QFileDialog.getOpenFileName(),,Images (*.png *.xpm *.jpg *.jpeg);;All Files (*),optionsoptions)iffile_name:self.image_pathfile_name self.display_image(file_name)defdisplay_image(self,path):pixmapQPixmap(path)scaled_pixmappixmap.scaled(self.image_label.width(),self.image_label.height(),Qt.KeepAspectRatio)self.image_label.setPixmap(scaled_pixmap)defload_video(self):optionsQFileDialog.Options()file_name,_QFileDialog.getOpenFileName(self,QFileDialog.getOpenFileName(),,Videos (*.mp4 *.avi);;All Files (*),optionsoptions)iffile_name:self.video_pathfile_name self.capcv2.VideoCapture(self.video_path)self.start_detection()defstart_detection(self):ifself.capisnotNoneandnotself.timer.isActive():self.timer.start(30)# Update frame every 30 msdefstop_detection(self):ifself.timer.isActive():self.timer.stop()self.cap.release()self.image_label.clear()defupdate_frame(self):ret,frameself.cap.read()ifret:processed_frameself.process_frame(frame)rgb_imagecv2.cvtColor(processed_frame,cv2.COLOR_BGR2RGB)h,w,chrgb_image.shape bytes_per_linech*w qt_imageQImage(rgb_image.data,w,h,bytes_per_line,QImage.Format_RGB888)pixmapQPixmap.fromImage(qt_image)scaled_pixmappixmap.scaled(self.image_label.width(),self.image_label.height(),Qt.KeepAspectRatio)self.image_label.setPixmap(scaled_pixmap)else:self.stop_detection()defprocess_frame(self,frame):resultsself.model(frame)forresultinresults:boxesresult.boxes.cpu().numpy()forboxinboxes:rbox.xyxy[0].astype(int)clsint(box.cls[0])confbox.conf[0]labelself.model.names[cls]textf{label}:{conf:.2f}color(0,255,0)# Green color for bounding boxcv2.rectangle(frame,(r[0],r[1]),(r[2],r[3]),color,2)cv2.putText(frame,text,(r[0],r[1]-10),cv2.FONT_HERSHEY_SIMPLEX,0.9,color,2)returnframeif__name____main__:appQApplication(sys.argv)windowMainWindow()window.show()sys.exit(app.exec_())使用说明配置路径将path/to/dataset设置为存放数据集的目录路径。确保runs/train/exp/weights/best.pt是训练好的 YOLOv5 模型权重路径。运行脚本在终端中运行train_yolov5.py脚本来训练模型。在终端中运行evaluate_yolov5.py来评估模型性能。在终端中运行main_window.py来启动 GUI 应用程序。点击“Load Image”按钮加载图像。点击“Load Video”按钮加载视频。点击“Start Detection”按钮开始检测。点击“Stop Detection”按钮停止检测。注意事项确保所有必要的工具箱已安装特别是 PyTorch 和 PyQt5。根据需要调整参数如epochs和imgsz。示例假设你的数据文件夹结构如下dataset/ ├── images/ │ ├── train/ │ │ ├── image1.jpg │ │ ├── image2.jpg │ │ └── ... │ ├── test/ │ │ ├── image3.jpg │ │ ├── image4.jpg │ │ └── ... │ └── valid/ │ ├── image5.jpg │ ├── image6.jpg │ └── ... ├── labels/ │ ├── train/ │ │ ├── image1.txt │ │ ├── image2.txt │ │ └── ... │ ├── test/ │ │ ├── image3.txt │ │ ├── image4.txt │ │ └── ... │ └── valid/ │ ├── image5.txt │ ├── image6.txt │ └── ... └── dataset.yaml并且每个.txt文件中都有正确的 YOLO 标签。运行main_window.py后你可以通过点击按钮来加载图像或视频并进行手势识别检测。总结通过上述步骤我们可以构建一个完整的基于 YOLOv5 的手势识别检测系统包括数据集准备、环境部署、模型训练、指标可视化展示、评估和 PyQt5 GUI 开发。以下是所有相关的代码文件训练 YOLOv5 脚本(train_yolov5.py)评估 YOLOv5 模型脚本(evaluate_yolov5.py)PyQt5 主窗口代码(main_window.py)
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2498591.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!