将目标检测的图片实时显示pyqt

1.问题描述

需要设计一个ui界面,使其能够读入任意一段视频并进行实时跟踪(标定目标框)。
目标检测的代码能够实现标定目标框,但是普遍很慢。
目标跟踪的代码,能够达到实时监测的要求,但是需要给定第一帧的目标框,所以最后采用检测+跟踪。
检测的输入为视频路径,可以在ui界面中读取,检测的输出为第一帧的bbox,传入跟踪。
跟踪的输入为视频路径,第一帧bbox;输出为跟踪到的bbox的值,储存的txt文件。

2.代码

class Video(QWidget):

    def __init__(self):
        super(Video, self).__init__()
        self.frame = []  # 存图片
        self.frame_track = []
        self.detectFlag = False  # 检测flag
        self.cap = []
        self.cap_track = []
        self.timer_camera = QTimer()  # 定义定时器
        self.timer_track = QTimer()
        self.name = {}

        # 外框
        self.resize(1600, 700)
        self.setWindowTitle("Head Detection")
        # 图片label
        self.label = QLabel(self)
        self.label.setText("请选择视频序列")
        self.label.setFixedSize(700, 450)  # width height
        self.label.move(50, 100)
        self.label.setStyleSheet("QLabel{background:white;}"
                                 "QLabel{color:rgb(100,100,100);font-size:15px;font-weight:bold;font-family:宋体;}"
                                 )

        # 显示跟踪结果
        self.label_track = QLabel(self)
        self.label_track.setText("显示跟踪结果")
        self.label_track.setFixedSize(700, 450)  # width height
        self.label_track.move(800, 100)
        self.label_track.setStyleSheet("QLabel{background:white;}"
                                 "QLabel{color:rgb(100,100,100);font-size:15px;font-weight:bold;font-family:宋体;}"
                                 )

        self.label_num = QLabel(self)
        self.label_num.setText("无人机跟踪实例检测")
        self.label_num.setFixedSize(430, 40)  # width height
        self.label_num.move(700, 20)
        # self.label_num.setStyleSheet("QLabel{background:yellow;}")
        # 开启视频按键
        self.btn = QPushButton(self)
        self.btn.setText("Open")
        self.btn.move(250, 570)
        self.btn.clicked.connect(self.slotStart)
        # 检测按键
        self.btn_detect = QPushButton(self)
        self.btn_detect.setText("Detect")
        self.btn_detect.move(700, 570)
        self.btn_detect.setStyleSheet("QPushButton{background:red;}")  # 没检测红色,检测绿色
        self.btn_detect.clicked.connect(self.detection)
        # 关闭视频按钮
        self.btn_stop = QPushButton(self)
        self.btn_stop.setText("Stop")
        self.btn_stop.move(1100, 570)
        self.btn_stop.clicked.connect(self.slotStop)

    def slotStart(self):
        """ Slot function to start the progamme
            """
        videoName, _ = QFileDialog.getOpenFileName(self, "Open", "", "*.avi;;*.mp4;;All Files(*)")
        if videoName != "":  # “”为用户取消
            self.cap = cv2.VideoCapture(videoName)
            self.timer_camera.start(100)
            self.timer_camera.timeout.connect(self.openFrame)

    def slotStop(self):
        """ Slot function to stop the programme
            """
        if self.cap != []:
            self.cap.release()
            self.timer_camera.stop()  # 停止计时器
            self.label.setText("This video has been stopped.")
            self.label.setStyleSheet("QLabel{background:pink;}"
                                     "QLabel{color:rgb(100,100,100);font-size:15px;font-weight:bold;font-family:宋体;}"
                                     )
        else:
            self.label_num.setText("Push the left upper corner button to Quit.")
            Warming = QMessageBox.warning(self, "Warming", "Push the left upper corner button to Quit.",
                                          QMessageBox.Yes)

        if self.cap_track != []:
            self.cap_track.release()
            self.timer_track.stop()  # 停止计时器
            self.label_track.setText("This video has been stopped.")
            self.label_track.setStyleSheet("QLabel{background:pink;}"
                                     "QLabel{color:rgb(100,100,100);font-size:15px;font-weight:bold;font-family:宋体;}"
                                     )
        # else:
        #     self.label_num.setText("Push the left upper corner button to Quit.")
        #     Warming = QMessageBox.warning(self, "Warming", "Push the left upper corner button to Quit.",
        #                                   QMessageBox.Yes)

    def openFrame(self):
        """ Slot function to capture frame and process it
            """
        if (self.cap.isOpened()):
            ret, self.frame = self.cap.read()
            # 参数ret 为True 或者False,代表有没有读取到图片
            # 第二个参数frame表示截取到一帧的图片
            if ret:
                frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
                height, width, bytesPerComponent = frame.shape
                bytesPerLine = bytesPerComponent * width
                q_image = QImage(frame.data, width, height, bytesPerLine,
                                 QImage.Format_RGB888).scaled(self.label.width(), self.label.height())
                self.label.setPixmap(QPixmap.fromImage(q_image))
            else:
                self.cap.release()
                self.timer_camera.stop()  # 停止计时器

    def openFrame_track(self):
        # track部分的代码
        run_video("/media/HDDP/zzyan/Anti-UAV-RGBT/test/20190925_111757_1_2/visible.mp4",
                  [298, 169, 52, 39], "/home/csq/data/tracking_ui2/lib/test/tracking_results", True, True, self.label_track)

    def detection(self):
        self.detectFlag = not self.detectFlag  # 取反
        if self.detectFlag == True:
            self.btn_detect.setStyleSheet("QPushButton{background:green;}")
            self.timer_track.start(100)
            self.timer_track.timeout.connect(self.openFrame_track)
        else:
            self.btn_detect.setStyleSheet("QPushButton{background:red;}")
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    my = Video()
    my.show()
    sys.exit(app.exec_())

代码部分参考了博主:https://blog.csdn.net/qq_42272539/article/details/99072807

你可能感兴趣的:(QT,目标检测,pyqt,深度学习)