使用PyQt5搭建yoloV5目标检测平台
Posted 吉果果、
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用PyQt5搭建yoloV5目标检测平台相关的知识,希望对你有一定的参考价值。
使用PyQt5搭建yoloV5目标检测平台
一、资源包准备:
1、python3.7
3、Anaconda
4、cuda-10.2.89
5、Visual Studio 2019
6、PyQt5-YOLOv5-master源码
二、环境搭建
1、PyQt5安装与部署:
1、安装PyQt5:
(1)、打开Anaconda Prompt终端,创建本项目虚拟环境,命名为PyQt5:
conda create -n PyQt5 python=3.8
(2)、激活并进入PYQT5虚拟环境:
conda activate PyQT5
此时可以看到终端提示的前面括号内已经变成了自己的环境名称:
(3)、在PyQt5的虚拟环境下安装PyQt5:
pip install PyQt5
(4)、安装Qt Designer图形界面开发工具:
pip install PyQt5-tools
(5)、记住安装的路径:
我的路径是:D:\\Users\\asus\\anaconda3\\Lib\\site-packages
(6)、配置环境变量:
为了让windows系统能够正确识别 PyQt5-tools 的常用命令,还需要把PyQt5-tools的安装目录添加到系统环境变量Path中,
右击此电脑——>属性——>高级系统设置——>环境变量——>系统变量——>Path——>新建;将上述PyQt5-tools的安装目录添加到系统环境变量path中
(7)、测试安装是否成功:
编写测试代码(test.py)如下:测试 PyQt5 环境是否安装成功,
import sys
from PyQt5 import QtWidgets, QtCore
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
widget.resize(360,360)
widget.setWindowTitle("hello, pyqt5")
widget.show()
sys.exit(app.exec_())
在PyQt5虚拟环境中运行如下命令:
python test.py
如果没有报错,弹出如下图所示的界面,则说明 PyQt5 环境安装成功。
2、yoloV5运行环境搭建:
(参考本人另一篇博客:《Windows下的YoLoV5目标检测模型部署》)
三、下载源码并运行
下载PyQt5-YOLOv5-master源码,报读网盘地址:
https://pan.baidu.com/s/1b61Z46QgXHy9mEYzuKuLAg 提取码:6666
下载完成后解压,在Anaconda Prompt终端中激活进入PyQt5虚拟环境并切换至解压后的PyQt5-YOLOv5-master目录:
在项目目录下运行:
python yolo_win.py
此时可以看到PyQt5生成的界面如下图:
图中左上方四个按钮分别为:模型选择、测试文件选择、打开摄像头、开始检测
四个按钮的下方拉杆可以设置检测置信度
下方左边区域显示输入的原始数据,中间区域显示输出的检测结果图像,最右边区域输出检测结果及统计的数量。
调用摄像头检测时先停止检测,再点击打开摄像头按钮,再点击开始检测。程序yolo_win.py中默认调用笔记本自带摄像头,如果需要调用其他外设摄像头可以自行修改:
资料参考:https://blog.csdn.net/weixin_41735859/article/details/120507779
Pyqt搭建YOLOV5目标检测界面
Pyqt搭建YOLOV5目标检测界面(超详细+源代码)
实现效果如下所示,可以检测图片、视频以及摄像头实时检测。
具体细节实现可以参考上一篇博客:Pyqt搭建YOLOV3目标检测界面(超详细+源代码)
使用的yolov5版本为https://github.com/ultralytics/yolov5
这里直接贴出具体代码。
方法1:共两个文件,
ui_yolov5.py
、detect_qt5.py
,然后把yolov5的代码下载下来,直接把这两个文件拷贝到yolov5根目录,下载yolov5官方的yolov5s.pt权重,放置根目录,然后运行ui_yolov5.py
即可。
方法2:整个yolov5以及两个文件都已上传在github,点这里 。无法访问github的关注公众号:万能的小陈,回复
qtv5
即可获取下载链接。(包含所有代码以及权重文件),只需要配置一下环境,配置环境可以参考这里,如果环境配置困难的或者失败的,在公众号后台回复pyqt5
即可获取完整环境。
文件1:ui_yolov5.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @author : ChenAng
# @file : ui_yolov5.py
# @Time : 2021/8/27 10:13
import time
import os
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
import cv2
import sys
from PyQt5.QtWidgets import *
from detect_qt5 import main_detect,my_lodelmodel
'''摄像头和视频实时检测界面'''
class Ui_MainWindow(QWidget):
def __init__(self, parent=None):
super(Ui_MainWindow, self).__init__(parent)
# self.face_recong = face.Recognition()
self.timer_camera1 = QtCore.QTimer()
self.timer_camera2 = QtCore.QTimer()
self.timer_camera3 = QtCore.QTimer()
self.timer_camera4 = QtCore.QTimer()
self.cap = cv2.VideoCapture()
self.CAM_NUM = 0
# self.slot_init()
self.__flag_work = 0
self.x = 0
self.count = 0
self.setWindowTitle("yolov5检测")
self.setWindowIcon(QIcon(os.getcwd() + '\\\\data\\\\source_image\\\\Detective.ico'))
# self.resize(300, 150) # 宽×高
window_pale = QtGui.QPalette()
window_pale.setBrush(self.backgroundRole(), QtGui.QBrush(
QtGui.QPixmap(os.getcwd() + '\\\\data\\\\source_image\\\\backgroud.jpg')))
self.setPalette(window_pale)
self.setFixedSize(1600, 900)
self.my_model = my_lodelmodel()
self.button_open_camera = QPushButton(self)
self.button_open_camera.setText(u'打开摄像头')
self.button_open_camera.setStyleSheet('''
QPushButton
{text-align : center;
background-color : white;
font: bold;
border-color: gray;
border-width: 2px;
border-radius: 10px;
padding: 6px;
height : 14px;
border-style: outset;
font : 14px;}
QPushButton:pressed
{text-align : center;
background-color : light gray;
font: bold;
border-color: gray;
border-width: 2px;
border-radius: 10px;
padding: 6px;
height : 14px;
border-style: outset;
font : 14px;}
''')
self.button_open_camera.move(10, 40)
self.button_open_camera.clicked.connect(self.button_open_camera_click)
#self.button_open_camera.clicked.connect(self.button_open_camera_click1)
# btn.clicked.connect(self.openimage)
self.btn1 = QPushButton(self)
self.btn1.setText("检测摄像头")
self.btn1.setStyleSheet('''
QPushButton
{text-align : center;
background-color : white;
font: bold;
border-color: gray;
border-width: 2px;
border-radius: 10px;
padding: 6px;
height : 14px;
border-style: outset;
font : 14px;}
QPushButton:pressed
{text-align : center;
background-color : light gray;
font: bold;
border-color: gray;
border-width: 2px;
border-radius: 10px;
padding: 6px;
height : 14px;
border-style: outset;
font : 14px;}
''')
self.btn1.move(10, 80)
self.btn1.clicked.connect(self.button_open_camera_click1)
# print("QPushButton构建")
self.open_video = QPushButton(self)
self.open_video.setText("打开视频")
self.open_video.setStyleSheet('''
QPushButton
{text-align : center;
background-color : white;
font: bold;
border-color: gray;
border-width: 2px;
border-radius: 10px;
padding: 6px;
height : 14px;
border-style: outset;
font : 14px;}
QPushButton:pressed
{text-align : center;
background-color : light gray;
font: bold;
border-color: gray;
border-width: 2px;
border-radius: 10px;
padding: 6px;
height : 14px;
border-style: outset;
font : 14px;}
''')
self.open_video.move(10, 160)
self.open_video.clicked.connect(self.open_video_button)
print("QPushButton构建")
self.btn1 = QPushButton(self)
self.btn1.setText("检测视频文件")
self.btn1.setStyleSheet('''
QPushButton
{text-align : center;
background-color : white;
font: bold;
border-color: gray;
border-width: 2px;
border-radius: 10px;
padding: 6px;
height : 14px;
border-style: outset;
font : 14px;}
QPushButton:pressed
{text-align : center;
background-color : light gray;
font: bold;
border-color: gray;
border-width: 2px;
border-radius: 10px;
padding: 6px;
height : 14px;
border-style: outset;
font : 14px;}
''')
self.btn1.move(10, 200)
self.btn1.clicked.connect(self.detect_video)
print("QPushButton构建")
# btn1.clicked.connect(self.detect())
# btn1.clicked.connect(self.button1_test)
#btn1.clicked.connect(self.detect())
# btn1.clicked.connect(self.button1_test)
btn2 = QPushButton(self)
btn2.setText("返回上一界面")
btn2.setStyleSheet('''
QPushButton
{text-align : center;
background-color : white;
font: bold;
border-color: gray;
border-width: 2px;
border-radius: 10px;
padding: 6px;
height : 14px;
border-style: outset;
font : 14px;}
QPushButton:pressed
{text-align : center;
background-color : light gray;
font: bold;
border-color: gray;
border-width: 2px;
border-radius: 10px;
padding: 6px;
height : 14px;
border-style: outset;
font : 14px;}
''')
btn2.move(10, 240)
btn2.clicked.connect(self.back_lastui)
# 信息显示
self.label_show_camera = QLabel(self)
self.label_move = QLabel()
self.label_move.setFixedSize(100, 100)
# self.label_move.setText(" 11 待检测图片")
self.label_show_camera.setFixedSize(700, 500)
self.label_show_camera.setAutoFillBackground(True)
self.label_show_camera.move(110,80)
self.label_show_camera.setStyleSheet("QLabel{background:#F5F5DC;}"
"QLabel{color:rgb(300,300,300,120);font-size:10px;font-weight:bold;font-family:宋体;}"
)
self.label_show_camera1 = QLabel(self)
self.label_show_camera1.setFixedSize(700, 500)
self.label_show_camera1.setAutoFillBackground(True)
self.label_show_camera1.move(850, 80)
self.label_show_camera1.setStyleSheet("QLabel{background:#F5F5DC;}"
"QLabel{color:rgb(300,300,300,120);font-size:10px;font-weight:bold;font-family:宋体;}"
)
self.timer_camera1.timeout.connect(self.show_camera)
self.timer_camera2.timeout.connect(self.show_camera1)
# self.timer_camera3.timeout.connect(self.show_camera2)
self.timer_camera4.timeout.connect(self.show_camera2)
self.timer_camera4.timeout.connect(self.show_camera3)
self.clicked = False
# self.setWindowTitle(u'摄像头')
self.frame_s=3
'''
# 设置背景图片
palette1 = QPalette()
palette1.setBrush(self.backgroundRole(), QBrush(QPixmap('background.jpg')))
self.setPalette(palette1)
'''
def back_lastui(self):
self.timer_camera1.stop()
self.cap.release()
self.label_show_camera.clear()
self.timer_camera2.stop()
self.label_show_camera1.clear()
cam_t.close()
ui_p.show()
'''摄像头'''
def button_open_camera_click(self):
if self.timer_camera1.isActive() == False:
flag = self.cap.open(self.CAM_NUM)
if flag == False:
msg = QtWidgets.QMessageBox.warning(self, u"Warning", u"请检测相机与电脑是否连接正确",
buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
self.timer_camera1.start(30)
self.button_open_camera.setText(u'关闭摄像头')
else:
self.timer_camera1.stop()
self.cap.release()
self.label_show_camera.clear()
self.timer_camera2.stop()
self.label_show_camera1.clear()
self.button_open_camera.setText(u'打开摄像头')
def show_camera(self): #摄像头左边
flag, self.image = self.cap.read()
dir_path=os.getcwd()
camera_source =dir_path+ "\\\\data\\\\test\\\\2.jpg"
cv2.imwrite(camera_source, self.image)
width = self.image.shape[1]
height = self.image.shape[0]
# 设置新的图片分辨率框架
width_new = 700
height_new = 500
# 判断图片的长宽比率
if width / height >= width_new / height_new:
show = cv2.resize(self.image, (width_new, int(height * width_new / width)))
else:
show = cv2.resize(self.image, (int(width * height_new / height), height_new))
show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0],3 * show.shape[1], QtGui.QImage.Format_RGB888)
self.label_show_camera.setPixmap(QtGui.QPixmap.fromImage(showImage))
def button_open_camera_click1(self):
if self.timer_camera2.isActive() == False:
flag = self.cap.open(self.CAM_NUM)
if flag == False:
msg = QtWidgets.QMessageBox.warning(self, u"Warning", u"请检测相机与电脑是否连接正确",
buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
self.timer_camera2.start(30)
self.button_open_camera.setText(u'关闭摄像头')
else:
self.timer_camera2.stop()
self.cap.release()
self.label_show_camera1.clear()
self.button_open_camera.setText(u'打开摄像头')
def show_camera1(self):
flag, self.image = self.cap.read()
dir_path = os.getcwd()
camera_source = dir_path + "\\\\data\\\\test\\\\2.jpg"
cv2.imwrite(camera_source, self.image)
im0, label = main_detect(self.my_model, camera_source)
if label=='debug':
print("labelkong")
width = im0.shape[1]
height = im0.shape[0]
# 设置新的图片分辨率框架
width_new = 700
height_new = 500
# 判断图片的长宽比率
if width / height >= width_new / height_new:
show = cv2.resize(im0, (width_new, int(height * width_new / width)))
else:
show = cv2.resize(im0, (int(width * height_new / height), height_new))
im0 = cv2.cvtColor(show, cv2.COLOR_RGB2BGR)
# print("debug2")
showImage = QtGui.QImage(im0, im0.shape[1], im0.shape[0], 3 * im0.shape[1], QtGui.QImage.Format_RGB888)
self.label_show_camera1.setPixmap(QtGui.QPixmap.fromImage(showImage))
'''视频检测'''
def open_video_button(self):
if self.timer_camera4.isActive() == False:
imgName, imgType = QFileDialog.getOpenFileName(self, "打开视频", "", "*.mp4;;*.AVI;;*.rmvb;;All Files(*)")
self.cap_video = cv2.VideoCapture(imgName)
flag = self.cap_video.isOpened()
if flag == False:
msg = QtWidgets.QMessageBox.warning(self, u"Warning", u"请检测相机与电脑是否连接正确",
buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
# self.timer_camera3.start(30)
self.show_camera2()
self.open_video.setText(u'关闭视频')
else:
# self.timer_camera3.stop()
self.cap_video.release()
self.label_show_camera.clear()
self.timer_camera4.stop()
self.frame_s=3
self.label_show_camera1.clear()
self.open_video.setText(u'打开视频')
def detect_video(self):
if self.timer_camera4.isActive() == False:
flag = self.cap_video.isOpened()
if flag == False:
msg = QtWidgets.QMessageBox.warning(self, u"Warning", u"请检测相机与电脑是否连接正确",
buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
self.timer_camera4.start(30)
else:
self.timer_camera4.stop()
self.cap_video.release()
self.label_show_camera1.clear()
def show_camera2(self): #显示视频的左边
#抽帧
length = int(self.cap_video.get(cv2.CAP_PROP_FRAME_COUNT)) #抽帧
print(self.frame_s,length) #抽帧
flag, self.image1 = self.cap_video.read() #image1是视频的
if flag == True:
if self.frame_s%3==0: #抽帧
dir_path=os.getcwd()
# print("dir_path",dir_path)
camera_source =dir_path+ "\\\\data\\\\test\\\\video.jpg"
cv2.imwrite(camera_source, self.image1)
width=self.image1.shape[1]
height=self.image1.shape[0]
# 设置新的图片分辨率框架
width_new = 700
height_new = 500
# 判断图片的长宽比率
if width / height >= width_new / height_new:
show = cv2.resize(self.image1, (width_new, int(height * width_new / width)))
else:
show = cv2.resize(self.image1, (int(width * height_new / height), height_new))
show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0],3 * show.shape[1], QtGui.QImage.Format_RGB888)
self.label_show_camera.setPixmap(QtGui.QPixmap.fromImage(showImage))
else:
self.cap_video.release()
self.label_show_camera.clear()
self.timer_camera4.stop()
self.label_show_camera1.clear()
self.open_video.setText(u'打开视频')
def 以上是关于使用PyQt5搭建yoloV5目标检测平台的主要内容,如果未能解决你的问题,请参考以下文章
睿智的目标检测56——Pytorch搭建YoloV5目标检测平台
睿智的目标检测55——Keras搭建YoloV5目标检测平台