如何在此特定代码中加快帧处理速度。如果没有GUI,它的正常速度即29fps,而使用GUI时,处理速度会下降,并且运行不流畅。
处理消耗了我所有四个内核的90%以上。我仅将“ haar级联算法”用于测试目的,主要是想将此GUI用于使用微小Yolo进行对象检测。但是我得到的速度很难在这种情况下实施小型Yolo。
from PyQt5 import QtCore, QtGui, QtWidgets
import cv2
import numpy as np
import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPixmap,QImage
import os
from PyQt5.QtCore import QDate, QTime, QDateTime, Qt
from PyQt5 import QtCore, QtGui, QtWidgets
# main window creator
class Ui_MainWindow(object):
def __init__(self):
super().__init__()
self.face_cascade = cv2.CascadeClassifier('/haarcascade_frontalface_default.xml')
self.eye_cascade = cv2.CascadeClassifier('/haarcascades/haarcascade_eye.xml')
#creating a timer
self.timer=QTimer()
self.timer.timeout.connect(self.nextFrameSlot)
self.filename="Demo-3.mp4" # video having resolution 1920*1080 with 29fps
#self.filename=0 # input 640*480
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1070, 716)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 20, 1051, 591))
# self.label.setGeometry(QtCore.QRect(10, 20, 640, 480))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("video.jpg"))
self.label.setScaledContents(True)
self.label.setObjectName("label")
self.button_Play = QtWidgets.QPushButton(self.centralwidget)
self.button_Play.setGeometry(QtCore.QRect(410, 620, 101, 33))
self.button_Play.setObjectName("button_Play")
self.button_stop = QtWidgets.QPushButton(self.centralwidget)
self.button_stop.setGeometry(QtCore.QRect(550, 620, 101, 33))
self.button_stop.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1070, 27))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.button_Play.clicked.connect(self.Play_Video)
self.button_stop.clicked.connect(self.StopCamera)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.button_Play.setText(_translate("MainWindow", "Play"))
self.button_stop.setText(_translate("MainWindow", "Stop"))
def Play_Video(self):
self.cap=cv2.VideoCapture(self.filename)
self.cap.set(3,640)
self.cap.set(4,480)
self.timer.start(1000./24)
#self.timer.start(1000./100)
def nextFrameSlot(self):
ret,frame=self.cap.read()
####################### algo( haar cascade algorithm ) ###########################
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
frame = cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = self.eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
#####################################################################
frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
image=QImage(frame,frame.shape[1],frame.shape[0],
QImage.Format_RGB888)
pixmap=QPixmap.fromImage(image)
self.label.setPixmap(pixmap)
def StopCamera(self):
# pass
self.timer.stop()
self.cap.release()
self.label.setPixmap(QtGui.QPixmap("video.jpg"))
return
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
"""