Source code for VideoControler

"""
This module defines the Video_Controller class which handles video playback controls
in the MoilApp application.

Dependencies:
- PyQt5.QtCore: Provides core non-GUI functionality.
- PyQt5.QtGui: Provides QIcon for setting button icons.
- cv2: OpenCV library for video processing.

Classes:
- Video_Controller: Manages video playback controls in the main application window.
"""

from PyQt5 import QtCore, QtGui
import cv2


[docs] class Video_Controler: """ The Video_Controller class manages video playback controls in the main application window. Attributes: parent (MainWindow): The main application window instance. play (bool): Indicates whether the video is currently playing. """ def __init__(self, Mainwindow): self.parent = Mainwindow self.play = False self.connectToButton()
[docs] def connectToButton(self): """ Connects UI buttons to their respective event handlers. """ self.parent.ui.actionClose_Cam.triggered.connect(self.stop_camera) self.parent.ui.videoPlay.clicked.connect(self.videoPlayPouse) self.parent.ui.videoStop.clicked.connect(self.stop_video) self.parent.ui.videoSkip.clicked.connect(self.skip_video) self.parent.ui.videoBack.clicked.connect(self.prev_video) self.parent.ui.sliderVideotime.valueChanged.connect(self.changeValue)
[docs] def videoButtonDisable(self): """ Disables all video control buttons and sliders. """ self.parent.ui.videoPlay.setDisabled(True) self.parent.ui.videoStop.setDisabled(True) self.parent.ui.videoSkip.setDisabled(True) self.parent.ui.videoBack.setDisabled(True) self.parent.ui.recordBtn.setDisabled(True) self.parent.ui.sliderVideotime.setDisabled(True) self.parent.ui.label_4.setDisabled(True) self.parent.ui.label_3.setDisabled(True)
[docs] def videoButtonEnable(self): """ Enables all video control buttons and sliders. """ self.parent.ui.videoPlay.setDisabled(False) self.parent.ui.videoStop.setDisabled(False) self.parent.ui.videoSkip.setDisabled(False) self.parent.ui.videoBack.setDisabled(False) self.parent.ui.recordBtn.setDisabled(False) self.parent.ui.sliderVideotime.setDisabled(False) self.parent.ui.label_4.setDisabled(False) self.parent.ui.label_3.setDisabled(False)
[docs] def videoButtonCamera(self): """ Enables video control buttons for camera mode, but disables the slider and time labels. """ self.parent.ui.videoPlay.setDisabled(False) self.parent.ui.videoStop.setDisabled(False) self.parent.ui.videoSkip.setDisabled(False) self.parent.ui.videoBack.setDisabled(False) self.parent.ui.recordBtn.setDisabled(False) self.parent.ui.sliderVideotime.setDisabled(True) self.parent.ui.label_4.setDisabled(True) self.parent.ui.label_3.setDisabled(True)
[docs] def reset_time(self): """ Resets the time labels to "00:00". """ current = self.parent.ui.label_3 current.setAlignment(QtCore.Qt.AlignCenter) current.setText("00:00") current_1 = self.parent.ui.label_4 current_1.setAlignment(QtCore.Qt.AlignCenter) current_1.setText("00:00")
[docs] def videoPlayPouse(self): """ Toggles between play and pause states for the video. """ if self.play: self.timer.stop() self.parent.ui.videoPlay.setIcon(QtGui.QIcon("assets/control.png")) self.pause_video() self.play = False else: self.parent.ui.videoPlay.setIcon(QtGui.QIcon("assets/control-pause.png")) self.play_video() self.play = True
[docs] def play_video(self): """ Starts the video playback by setting up a timer to update frames. """ if self.parent.cap.isOpened(): self.timer = QtCore.QTimer() self.timer.timeout.connect(self.parent.next_frame_slot) self.timer.start(1000. / self.parent.fps) else: pass
[docs] def pause_video(self): """ Pauses the video playback by stopping the timer. """ self.timer.stop()
[docs] def stop_video(self): """ Stops the video playback, resets the video to the beginning, and resets time labels. """ self.play = False self.parent.ui.videoPlay.setIcon(QtGui.QIcon("assets/control.png")) if self.parent.cap.isOpened(): self.parent.cap.set(cv2.CAP_PROP_POS_FRAMES, 0) self.parent.next_frame_slot() self.pause_video() self.reset_time() self.parent.application.applicationMoildevs() self.parent.image = None else: pass
[docs] def stop_camera(self): """ Stops the camera if it is running, releasing the capture and clearing the display. """ if self.parent.cam: self.timer.stop() self.parent.cap.release() self.parent.ui.windowOri.clear() self.parent.ui.windowResult.clear() else: pass
[docs] def prev_video(self): """ Skips the video backward by 5 seconds. """ if self.parent.cap.isOpened(): position = self.parent.pos_frame - 5 * self.parent.fps self.parent.cap.set(cv2.CAP_PROP_POS_FRAMES, position) self.parent.next_frame_slot() else: pass
[docs] def skip_video(self): """ Skips the video forward by 5 seconds. """ if self.parent.cap.isOpened(): position = self.parent.pos_frame + 5 * self.parent.fps self.parent.cap.set(cv2.CAP_PROP_POS_FRAMES, position) self.parent.next_frame_slot() else: pass
[docs] def changeValue(self, value): """ Changes the video playback position based on the slider value. Args: value (int): The value of the slider. """ if self.parent.cap.isOpened(): dst_frame = self.parent.frame_count * value / self.parent.ui.sliderVideotime.maximum() + 1 self.parent.cap.set(cv2.CAP_PROP_POS_FRAMES, dst_frame) self.parent.next_frame_slot() self.timer.stop() else: pass
[docs] def controler(self): """ Updates the slider and time labels based on the current video position. """ dst_value = self.parent.pos_frame * (self.parent.ui.sliderVideotime.maximum() + 1) / self.parent.frame_count self.parent.ui.sliderVideotime.blockSignals(True) self.parent.ui.sliderVideotime.setValue(dst_value) self.parent.ui.sliderVideotime.blockSignals(False) current = self.parent.ui.label_3 current.setAlignment(QtCore.Qt.AlignCenter) current.setText("%02d : %02d" % (self.parent.minute, self.parent.sec)) if self.parent.minute > 1000: my_label3 = self.parent.ui.label_4 my_label3.setAlignment(QtCore.Qt.AlignCenter) my_label3.setText("00:00") else: my_label3 = self.parent.ui.label_4 my_label3.setAlignment(QtCore.Qt.AlignCenter) my_label3.setText("%02d : %02d" % (self.parent.minutes, self.parent.seconds))