Skip to content

Commit d76780a

Browse files
author
arch
committed
eliminate duplicated code
1 parent 8f64a5b commit d76780a

File tree

5 files changed

+45
-55
lines changed

5 files changed

+45
-55
lines changed

funscript_editor/algorithms/funscriptgenerator.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class FunscriptGeneratorParameter:
6767
right_threshold: float = float(HYPERPARAMETER['right_threshold'])
6868

6969

70-
class FunscriptGenerator(QtCore.QThread):
70+
class FunscriptGeneratorThread(QtCore.QThread):
7171
""" Funscript Generator Thread
7272
7373
Args:
@@ -105,9 +105,6 @@ def __init__(self,
105105
#: completed event with reference to the funscript with the predicted actions, status message and success flag
106106
funscriptCompleted = QtCore.pyqtSignal(object, str, bool)
107107

108-
#: processing event with current processed frame number
109-
processStatus = QtCore.pyqtSignal(int)
110-
111108
logger = logging.getLogger(__name__)
112109

113110

@@ -687,7 +684,6 @@ def tracking(self) -> str:
687684

688685
trackerWoman.update(frame)
689686
if self.params.track_men: trackerMen.update(frame)
690-
self.processStatus.emit(frame_num)
691687

692688
if last_frame is not None:
693689
# Process data from last step while the next tracking points get predicted.

funscript_editor/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from funscript_editor.utils.logging import setup_logging
77
from funscript_editor.ui.funscript_editor_window import FunscriptEditorWindow
8-
from funscript_editor.ui.minimal import MinimalFunscriptGenerator
8+
from funscript_editor.ui.funscript_generator_window import FunscriptGeneratorWindow
99
from funscript_editor.utils.config import VERSION
1010

1111
from PyQt5 import QtCore, QtGui, QtWidgets
@@ -40,6 +40,6 @@ def generate_funscript(
4040
logging.info("Args: video_file=%s, start_time=%s, end_time=%s, output_file=%s", \
4141
str(video_file), str(start_time), str(end_time), str(output_file))
4242
app = QtWidgets.QApplication(sys.argv)
43-
generator = MinimalFunscriptGenerator(video_file, start_time, end_time, output_file)
43+
generator = FunscriptGeneratorWindow(video_file, start_time, end_time, output_file)
4444
generator.run()
4545
sys.exit(app.exec_())

funscript_editor/config/settings.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ preview_scaling: 0.6
2323
# Set the video type. Available Options:
2424
# - 'flat': 2D Videos
2525
# - 'vr_he_sbs': 3D VR Side-By-Side Video
26-
projection: 'vr_he_sbs'
26+
projection: 'flat'
2727

2828
# Specify the tracker algorithm. Available options are 'MIL', 'KCF', 'CSRT'.
2929
tracker: 'CSRT'

funscript_editor/ui/funscript_editor_window.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
from funscript_editor.ui.video_player import VideoPlayer
2222
from funscript_editor.ui.funscript_visualizer import FunscriptVisualizer
2323
from funscript_editor.data.funscript import Funscript
24-
from funscript_editor.algorithms.funscriptgenerator import FunscriptGenerator, FunscriptGeneratorParameter
24+
from funscript_editor.algorithms.funscriptgenerator import FunscriptGeneratorThread, FunscriptGeneratorParameter
2525
from funscript_editor.utils.config import UI_CONFIG, VERSION
2626
from funscript_editor.definitions import APP_DOCUMENTATION_DIR, CODE_DOCUMENTATION_DIR
2727
from funscript_editor.data.ffmpegstream import FFmpegStream
28+
from funscript_editor.ui.funscript_generator_window import FunscriptGeneratorWindow
2829

2930
class FunscriptEditorWindow(QtWidgets.QMainWindow):
3031
""" Funscript Editor window """
@@ -295,38 +296,28 @@ def __new_funscript(self):
295296
self.video_player.set_funscript(self.funscript)
296297
self.funscript_visualizer.set_funscript(self.funscript)
297298

298-
def __generator_status_changed(self, current):
299-
self.statusBar().showMessage("{} ({})".format(current, datetime.now().strftime("%H:%M:%S")))
300-
301299
def __generate_funscript(self):
302300
if self.funscript is None: return
303301
if self.video_player is None: return
304302
if self.video_player.get_video_file is None: return
305-
start_frame = self.video_player.get_current_frame
303+
start_time = self.video_player.get_current_timestamp_in_millis
306304
next_action = self.funscript.get_next_action(self.video_player.get_current_timestamp_in_millis+100)
307305
if next_action['at'] > self.video_player.get_current_timestamp_in_millis+100:
308-
end_frame = self.video_player.millisec_to_frame(next_action['at'])
306+
end_time = next_action['at']
309307
else:
310-
end_frame = -1
308+
end_time = -1
311309

312-
self.__logger.info("Stop at Frame {}".format(end_frame))
310+
self.__logger.info("Stop at {}".format(end_time))
311+
312+
self.funscript_generator_window = FunscriptGeneratorWindow(
313+
self.video_player.get_video_file,
314+
start_time,
315+
end_time,
316+
self.funscript
317+
)
318+
self.funscript_generator_window.funscriptCompleted.connect(self.__funscript_generated)
319+
self.funscript_generator_window.run()
313320

314-
reply = QtWidgets.QMessageBox.question(None, 'Track Men', 'Do you want to track the Men? ',
315-
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)
316-
trackMen = True if reply == QtWidgets.QMessageBox.Yes else False
317-
318-
self.video_player.set_indicate_bussy(True)
319-
self.funscript_generator = FunscriptGenerator(
320-
FunscriptGeneratorParameter(
321-
video_path = self.video_player.get_video_file,
322-
start_frame = start_frame,
323-
end_frame = end_frame,
324-
track_men = trackMen
325-
),
326-
self.funscript)
327-
self.funscript_generator.funscriptCompleted.connect(self.__funscript_generated)
328-
self.funscript_generator.processStatus.connect(self.__generator_status_changed)
329-
self.funscript_generator.start()
330321

331322
def __funscript_generated(self, funscript, status, success):
332323
self.video_player.set_funscript(self.funscript)

funscript_editor/ui/minimal.py renamed to funscript_editor/ui/funscript_generator_window.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
import cv2
55

66
from funscript_editor.utils.logging import setup_logging
7-
from funscript_editor.ui.funscript_editor_window import FunscriptEditorWindow
8-
from funscript_editor.algorithms.funscriptgenerator import FunscriptGenerator, FunscriptGeneratorParameter
7+
from funscript_editor.algorithms.funscriptgenerator import FunscriptGeneratorThread, FunscriptGeneratorParameter
98
from funscript_editor.data.funscript import Funscript
109

1110
from PyQt5 import QtCore, QtGui, QtWidgets
1211

1312

14-
class MinimalFunscriptGenerator(QtWidgets.QMainWindow):
13+
class FunscriptGeneratorWindow(QtWidgets.QMainWindow):
1514
""" Class to Generate a funscript with minimal UI
1615
1716
Note:
@@ -21,22 +20,24 @@ class MinimalFunscriptGenerator(QtWidgets.QMainWindow):
2120
video_file (str): path to video file
2221
start_time (float): start position in video (timestamp in milliseconds)
2322
end_time (float): end position in video (timestamp in milliseconds) use -1.0 for video end.
24-
output_file (str): csv output file path
23+
output_file (str, Funscript): csv output file path (Optional you can pass a funscript object where to store the result)
2524
"""
2625

2726
def __init__(self,
2827
video_file: str,
2928
start_time: float,
3029
end_time: float,
3130
output_file: str):
32-
super(MinimalFunscriptGenerator, self).__init__()
31+
super(FunscriptGeneratorWindow, self).__init__()
3332

34-
if os.path.isdir(output_file):
35-
self.__show_message("The output TempFile path must be a file not a folder", error=True)
36-
sys.exit()
33+
if not isinstance(output_file, Funscript):
34+
output_file = os.path.abspath(output_file)
35+
if os.path.isdir(output_file):
36+
self.__show_message("The output TempFile path must be a file not a folder", error=True)
37+
sys.exit()
3738

38-
if os.path.exists(output_file):
39-
os.remove(output_file)
39+
if os.path.exists(output_file):
40+
os.remove(output_file)
4041

4142
if video_file is None or video_file == "":
4243
self.__show_message("Video file was not specified! " \
@@ -53,7 +54,7 @@ def __init__(self,
5354
cap.release()
5455

5556
self.funscript = Funscript(fps)
56-
self.output_file = os.path.abspath(output_file)
57+
self.output_file = output_file
5758

5859
if False:
5960
reply = QtWidgets.QMessageBox.question(None, 'Generate Funscript ', \
@@ -72,7 +73,7 @@ def __init__(self,
7273

7374
self.__logger.info("Set End Time to Frame Number %d", end_frame)
7475

75-
self.funscript_generator = FunscriptGenerator(
76+
self.funscript_generator = FunscriptGeneratorThread(
7677
FunscriptGeneratorParameter(
7778
video_path = video_file,
7879
start_frame = start_frame,
@@ -81,11 +82,12 @@ def __init__(self,
8182
),
8283
self.funscript)
8384
self.funscript_generator.funscriptCompleted.connect(self.__funscript_generated)
84-
self.funscript_generator.processStatus.connect(self.__status_changed)
8585

8686

8787
__logger = logging.getLogger(__name__)
8888

89+
funscriptCompleted = QtCore.pyqtSignal(object, str, bool)
90+
8991

9092
def __show_message(self, message :str, error: bool = False) -> None:
9193
if error: self.__logger.error(message)
@@ -98,17 +100,18 @@ def __show_message(self, message :str, error: bool = False) -> None:
98100

99101

100102
def __funscript_generated(self, funscript, msg, success) -> None:
101-
os.makedirs(os.path.dirname(self.output_file), exist_ok=True)
102-
with open(self.output_file, 'w') as f:
103-
f.write('at;pos\n')
103+
if isinstance(self.output_file, Funscript):
104104
for item in funscript.get_actions():
105-
f.write('{at};{pos}\n'.format(at=item['at'], pos=item['pos']))
106-
if not success: self.__show_message(msg, error=True)
107-
sys.exit()
108-
109-
110-
def __status_changed(self, frame) -> None:
111-
pass
105+
self.output_file.add_action(item['pos'], item['at'])
106+
self.funscriptCompleted.emit(self.output_file, msg, success)
107+
else:
108+
os.makedirs(os.path.dirname(self.output_file), exist_ok=True)
109+
with open(self.output_file, 'w') as f:
110+
f.write('at;pos\n')
111+
for item in funscript.get_actions():
112+
f.write('{at};{pos}\n'.format(at=item['at'], pos=item['pos']))
113+
if not success: self.__show_message(msg, error=True)
114+
sys.exit()
112115

113116

114117
def run(self) -> None:

0 commit comments

Comments
 (0)