该项目使用yolov5进行目标检测,并且可以选择使用手机或者电脑摄像头,以及是否使用GPU加速推理。 博客中有提供包含界面和非包含界面代码地址,如有需要可自行前往下载。
界面相关: 视频效果:
b站地址
yolov5主要是参考这位巨佬的代码,如有需要可前往自行下载(无界面版)。
下面分享一下主要的界面代码。
import sys, cv2
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtGui,QtWidgets
import sys, cv2, time
from untitled import Ui_Form
import cv2 as cv
import numpy as np
import time
import cv2
import numpy as np
from PIL import Image
from yolo import YOLO
class Mywindow(QtWidgets.QWidget,Ui_Form): #这个窗口继承了用QtDesignner 绘制的窗口
def __init__(self):
super(Mywindow,self).__init__()
self.setupUi(self)
self.use_palette()
# 设置定时器
self._timer = QTimer(self)
self._timer.timeout.connect(self.play)
#单选按钮分组
self.bg1=QButtonGroup(self)
self.bg1.addButton(self.radioButton_3,1)
self.bg1.addButton(self.radioButton_4,2)
self.radioButton_3.setChecked(True)
self.bg2 = QButtonGroup(self)
self.bg2.addButton(self.radioButton_5, 3)
self.bg2.addButton(self.radioButton_6, 4)
self.radioButton_5.setChecked(True)
self.yolo = YOLO()
self.bg1.buttonClicked.connect(self.gpu_cpu)
self.bg2.buttonClicked.connect(self.phone_computer)
self.pushButton.clicked.connect(self.pic)
self.pushButton_2.clicked.connect(self.video)
self.pushButton_3.clicked.connect(self.camera)
def camera(self):
if self.radioButton_5.isChecked():
self.ved = cv2.VideoCapture(0)
elif self.radioButton_6.isChecked():
http=self.lineEdit.text()
print(http)
self.ved = cv2.VideoCapture(http)
self._timer.start(1)
def video(self):
v, videoType = QFileDialog.getOpenFileName(self,
"选择视频",
"",
" *.mp4;;*.avi;;All Files (*)")
self.ved = cv2.VideoCapture(v)
qq, bb = self.ved.read()
print(bb)
if qq == False:
msg_box = QMessageBox(QMessageBox.Warning, 'Warning', '请选择视频!')
msg_box.exec_()
return
self._timer.start(1)
def play(self):
# 读取视频
r, self.frame = self.ved.read()
if not r:
return
img=Image.fromarray(self.frame[:,:,::-1])
t1=time.time()
img = self.yolo.detect_image(img, False, False)
t2=time.time()
fps=1/(t2-t1)
img=np.array(img)[...,::-1]
img2 = img.copy()
cv2.putText(img2, "FPS: %.2f"%fps, (0, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
img2=self.img_resize(img2)
img2=self.cv_qt(img2)
# 将图像显示到窗口上
self.label.setPixmap(QPixmap.fromImage(img2))
def gpu_cpu(self):
if self.radioButton_3.isChecked():
self.yolo=YOLO(use_gpu=True)
else:
self.yolo = YOLO(use_gpu=False)
def phone_computer(self):
pass
def pic(self):
img, picType = QFileDialog.getOpenFileName(self,
"选择图片",
"",
" *.jpg;;*.png;;All Files (*)")
try:
src=Image.open(img)
except:
return
t1=time.time()
src=self.yolo.detect_image(src,False,False)
t2=time.time()
fps=round(1/(t2-t1),3)
src=np.array(src)[:,:,::-1]
src2=src.copy()
cv2.putText(src2, 'FPS: ' + str(fps), (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 2.5, (0, 0, 255), 3)
src2=self.img_resize(src2)
src2=self.cv_qt(src2)
# 将图像显示到窗口上
self.label.setPixmap(QPixmap.fromImage(src2))
# 改变图片宽高,使其适应窗口大小函数
def img_resize(self, img):
# 获取窗口的宽高
h = self.label.height()
w = self.label.width()
if img.shape[0] >= img.shape[1]:
rate = h / img.shape[0]
# 缩放尺寸
src = cv.resize(img, (int(img.shape[1] * rate), int(img.shape[0] * rate)))
else:
rate = w / img.shape[1]
# 缩放尺寸
src = cv.resize(img, (int(img.shape[1] * rate), int(img.shape[0] * rate)))
return src
# 设置背景图片函数
def use_palette(self):
self.setWindowTitle("YOLOV5目标检测")
window_pale = QtGui.QPalette()
window_pale.setBrush(self.backgroundRole(),
QtGui.QBrush(QtGui.QPixmap(r"./back2.jpg")))
self.setPalette(window_pale)
# 将opencv格式图片转成pyqt5格式图片
def cv_qt(self, src):
if len(src.shape) == 2:
src = np.expand_dims(src, 2)
src = src.repeat(3, axis=2)
h, w, d = src.shape
bytesperline = d * w
# self.src=cv.cvtColor(self.src,cv.COLOR_BGR2RGB)
qt_image = QImage(src.data, w, h, bytesperline, QImage.Format_RGB888).rgbSwapped()
return qt_image
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Mywindow()
win.show()
sys.exit(app.exec_())
整体项目下载地址(含界面版)
|