Skip to content

Commit

Permalink
added time elapsed, time remaining and speed rate
Browse files Browse the repository at this point in the history
  • Loading branch information
k4yt3x committed May 7, 2020
1 parent 5fbc195 commit 3168737
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 114 deletions.
110 changes: 83 additions & 27 deletions src/video2x_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
# local imports
from upscaler import Upscaler

# built-in imports
import pathlib
import sys

# built-in imports
import contextlib
import pathlib
import re
import shutil
import sys
import tempfile
import threading
import time
import traceback
import yaml
Expand All @@ -45,21 +42,45 @@
'Anime4KCPP': 'anime4kcpp'
}

def resource_path(relative_path: str) -> pathlib.Path:
try:
base_path = pathlib.Path(sys._MEIPASS)
except Exception:
base_path = pathlib.Path(__file__).parent
return base_path / relative_path

class UpscalerSignals(QObject):
finished = pyqtSignal()

class WorkerSignals(QObject):
progress = pyqtSignal(tuple)
error = pyqtSignal(str)
finished = pyqtSignal()

class Worker(QRunnable):
class ProgressBarWorker(QRunnable):
def __init__(self, fn, *args, **kwargs):
super(ProgressBarWorker, self).__init__()
self.fn = fn
self.args = args
self.kwargs = kwargs
self.signals = WorkerSignals()
self.kwargs['progress_callback'] = self.signals.progress

@pyqtSlot()
def run(self):
try:
self.fn(*self.args, **self.kwargs)
except Exception:
pass

class UpscalerWorker(QRunnable):

def __init__(self, fn, *args, **kwargs):
super(Worker, self).__init__()
super(UpscalerWorker, self).__init__()

# Store constructor arguments (re-used for processing)
self.fn = fn
self.args = args
self.kwargs = kwargs
self.signals = UpscalerSignals()
self.signals = WorkerSignals()

@pyqtSlot()
def run(self):
Expand All @@ -78,9 +99,9 @@ class Video2XMainWindow(QtWidgets.QMainWindow):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
uic.loadUi('video2x_gui.ui', self)
uic.loadUi(str(resource_path('video2x_gui.ui')), self)

self.video2x_icon_path = str((pathlib.Path(__file__).parent / 'images' / 'video2x.png').absolute())
self.video2x_icon_path = str(resource_path('images/video2x.png'))
self.setWindowTitle(f'Video2X GUI {VERSION}')
self.setWindowIcon(QtGui.QIcon(self.video2x_icon_path))

Expand Down Expand Up @@ -126,8 +147,11 @@ def __init__(self, *args, **kwargs):

# progress bar and start/stop controls
self.progress_bar = self.findChild(QtWidgets.QProgressBar, 'progressBar')
self.time_elapsed_label = self.findChild(QtWidgets.QLabel, 'timeElapsedLabel')
self.time_remaining_label = self.findChild(QtWidgets.QLabel, 'timeRemainingLabel')
self.rate_label = self.findChild(QtWidgets.QLabel, 'rateLabel')
self.start_button = self.findChild(QtWidgets.QPushButton, 'startButton')
self.start_button.clicked.connect(self.upscale)
self.start_button.clicked.connect(self.start)
self.stop_button = self.findChild(QtWidgets.QPushButton, 'stopButton')
self.stop_button.clicked.connect(self.stop)

Expand Down Expand Up @@ -482,29 +506,65 @@ def show_message(self, message: str, custom_icon=None):
message_box.setText(message)
message_box.exec_()

def start_progress_bar(self):
def start_progress_bar(self, progress_callback):

# initialize variables early
self.upscaler.progress_bar_exit_signal = False
self.upscaler.total_frames_upscaled = 0
self.upscaler.total_frames = 1

# initialize progress bar values
self.progress_bar.setValue(0)
upscale_begin_time = time.time()
progress_callback.emit((0, 0, 0, upscale_begin_time))

# keep querying upscaling process and feed information to callback signal
while not self.upscaler.progress_bar_exit_signal:
self.progress_bar.setValue(int(100 * self.upscaler.total_frames_upscaled / self.upscaler.total_frames))
progress_callback.emit((int(100 * self.upscaler.total_frames_upscaled / self.upscaler.total_frames),
self.upscaler.total_frames_upscaled,
self.upscaler.total_frames,
upscale_begin_time))
time.sleep(1)
self.progress_bar.setValue(100)

def upscale(self):
# upscale process will stop at 99%
# so it's set to 100 manually when all is done
progress_callback.emit((100, 0, 0, upscale_begin_time))

def set_progress(self, progress_information: tuple):
progress_percentage = progress_information[0]
total_frames_upscaled = progress_information[1]
total_frames = progress_information[2]
upscale_begin_time = progress_information[3]

# calculate fields based on frames and time elapsed
time_elapsed = time.time() - upscale_begin_time
try:
rate = total_frames_upscaled / (time.time() - upscale_begin_time)
time_remaining = (total_frames - total_frames_upscaled) / rate
except Exception:
rate = 0.0
time_remaining = 0.0

# set calculated values in GUI
self.progress_bar.setValue(progress_percentage)
self.time_elapsed_label.setText('Time Elapsed: {}'.format(time.strftime("%H:%M:%S", time.gmtime(time_elapsed))))
self.time_remaining_label.setText('Time Remaining: {}'.format(time.strftime("%H:%M:%S", time.gmtime(time_remaining))))
self.rate_label.setText('Rate (FPS): {}'.format(round(rate, 2)))

def start(self):

# start execution
try:
# start timer
self.begin_time = time.time()

# resolve input and output directories from GUI
if self.input_line_edit.text().strip() == '':
self.show_error('Input path not specified')
return
if self.output_line_edit.text().strip() == '':
self.show_error('Output path not specified')
return

input_directory = pathlib.Path(self.input_line_edit.text())
output_directory = pathlib.Path(self.output_line_edit.text())

Expand All @@ -520,8 +580,6 @@ def upscale(self):
# if input specified is a single file
if input_directory.is_file():

# upscale single video file

# check for input output format mismatch
if output_directory.is_dir():
self.show_error('Input and output path type mismatch\n\
Expand All @@ -547,11 +605,12 @@ def upscale(self):

# start progress bar
if AVAILABLE_DRIVERS[self.driver_combo_box.currentText()] != 'anime4kcpp':
progress_bar_worker = Worker(self.start_progress_bar)
progress_bar_worker = ProgressBarWorker(self.start_progress_bar)
progress_bar_worker.signals.progress.connect(self.set_progress)
self.threadpool.start(progress_bar_worker)

# run upscaler
worker = Worker(self.upscaler.run)
worker = UpscalerWorker(self.upscaler.run)
worker.signals.error.connect(self.upscale_errored)
worker.signals.finished.connect(self.upscale_completed)
self.threadpool.start(worker)
Expand Down Expand Up @@ -582,11 +641,11 @@ def upscale(self):

# start progress bar
if AVAILABLE_DRIVERS[self.driver_combo_box.currentText()] != 'anime4kcpp':
progress_bar_worker = Worker(self.start_progress_bar)
progress_bar_worker = ProgressBarWorker(self.start_progress_bar)
self.threadpool.start(progress_bar_worker)

# run upscaler
worker = Worker(self.upscaler.run)
worker = UpscalerWorker(self.upscaler.run)
worker.signals.error.connect(self.upscale_errored)
worker.signals.finished.connect(self.upscale_completed)
self.threadpool.start(worker)
Expand All @@ -598,8 +657,6 @@ def upscale(self):

except Exception:
self.upscale_errored(traceback.format_exc())

finally:
self.upscale_completed()

def upscale_errored(self, error_message):
Expand All @@ -624,7 +681,6 @@ def stop(self):
# TODO unimplemented yet
pass


app = QtWidgets.QApplication(sys.argv)
window = Video2XMainWindow()
window.show()
Expand Down
Loading

0 comments on commit 3168737

Please sign in to comment.