from PyQt5 import QtCore, QtGui, QtWidgets
import datetime
import cv2
import os
[docs]
class VideoController(object):
def __init__(self, Mainwindow):
"""
The class to control the video. this class also inheritance from mainwindow class.
Args:
Mainwindow (): Is the parent class to access the user interface widget in this application.
"""
self.parent = Mainwindow
self.play = False
self.record = False
self.videoDir = None
self.connectToButton()
[docs]
def reset_time(self):
"""
Reset the time when open the new video.
Returns:
"""
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):
"""
Control play and pause when playing video or camera.
Returns:
"""
if self.play:
self.timer.stop()
self.parent.ui.videoPlay.setIcon(QtGui.QIcon("images/play.png"))
self.pause_video()
self.play = False
else:
self.parent.ui.videoPlay.setIcon(
QtGui.QIcon("images/pause.png"))
self.play_video()
self.play = True
[docs]
def play_video(self):
"""
Play video.
Returns:
"""
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):
"""
Pause video.
Returns:
"""
self.timer.stop()
[docs]
def stop_video(self):
"""
Stop Video.
Returns:
"""
self.play = False
self.parent.ui.videoPlay.setIcon(QtGui.QIcon("images/play.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.image = None
else:
pass
[docs]
def stop_camera(self):
"""
Stop camera and clear label view.
Returns:
"""
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):
"""
Previous video in 5 second.
Returns:
"""
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):
"""
skip video in 5 second.
Returns:
"""
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):
"""
Control slider time video.
Args:
value ():
Returns:
"""
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 controller(self):
"""
Manage the video to setup the current timer.
Returns:
"""
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))
[docs]
def recordVideo(self):
"""
Create video writer to save video.
Returns:
"""
if self.record is False:
self.timer.stop()
ss = datetime.datetime.now().strftime("%m_%d_%H_%M_%S")
frame_width = int(self.parent.cap.get(3))
frame_height = int(self.parent.cap.get(4))
filename = "Original"
if self.parent.ui.btn_Panorama.isChecked(
) or self.parent.ui.btn_Anypoint.isChecked():
filename = "result"
if self.videoDir is None or self.videoDir == "":
self.selectDir()
else:
name = self.videoDir + "/" + filename + "_" + str(ss) + ".avi"
answer = QtWidgets.QMessageBox.information(
self.parent,
"Information",
" Start Record Video !!",
QtWidgets.QMessageBox.Yes,
QtWidgets.QMessageBox.No)
if answer == QtWidgets.QMessageBox.Yes:
self.timer.start()
self.video_writer = cv2.VideoWriter(
name, cv2.VideoWriter_fourcc(
*'XVID'), 15, (frame_width, frame_height))
os.makedirs(os.path.dirname(name), exist_ok=True)
self.parent.ui.actionRecord_Video.setIcon(QtGui.QIcon("images/record.png"))
self.record = True
else:
pass
else:
self.video_writer.release()
self.timer.stop()
QtWidgets.QMessageBox.information(
self.parent,
"Information",
"Video saved !!\n\nLoc: " +
self.videoDir)
self.timer.start()
self.parent.ui.actionRecord_Video.setIcon(QtGui.QIcon("images/rec.png"))
self.record = False
[docs]
def selectDir(self):
"""
Select destination directory to save the video file.
Returns:
"""
self.videoDir = QtWidgets.QFileDialog.getExistingDirectory(self.parent, 'Select Save Folder')
if self.videoDir:
self.recordVideo()