Skip to content

Commit f075f9f

Browse files
author
arch
committed
refactor distance to metric
1 parent 681458f commit f075f9f

File tree

5 files changed

+61
-53
lines changed

5 files changed

+61
-53
lines changed

funscript_editor/algorithms/funscriptgenerator.py

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class FunscriptGeneratorParameter:
3535
# No default values
3636
video_path: str # no default value
3737
track_men: bool
38-
direction: str
38+
metric: str
3939
projection: str
4040

4141
# Settings
@@ -95,7 +95,7 @@ def __init__(self,
9595
self.score = {
9696
'x': [],
9797
'y': [],
98-
'd': []
98+
'euclideanDistance': []
9999
}
100100
self.bboxes = {
101101
'Men': [],
@@ -309,7 +309,7 @@ def calculate_score(self) -> None:
309309
if self.params.track_men:
310310
self.score['x'] = [w[0] - m[0] for w, m in zip(self.bboxes['Woman'], self.bboxes['Men'])]
311311
self.score['y'] = [m[1] - w[1] for w, m in zip(self.bboxes['Woman'], self.bboxes['Men'])]
312-
self.score['d'] = [np.sqrt(np.sum((np.array(m[:2]) - np.array(w[:2])) ** 2, axis=0)) \
312+
self.score['euclideanDistance'] = [np.sqrt(np.sum((np.array(m[:2]) - np.array(w[:2])) ** 2, axis=0)) \
313313
for w, m in zip(self.bboxes['Woman'], self.bboxes['Men'])]
314314
else:
315315
self.score['x'] = [w[0] - min([x[0] for x in self.bboxes['Woman']]) for w in self.bboxes['Woman']]
@@ -318,29 +318,29 @@ def calculate_score(self) -> None:
318318

319319
self.score['x'] = sp.scale_signal(self.score['x'], 0, 100)
320320
self.score['y'] = sp.scale_signal(self.score['y'], 0, 100)
321-
self.score['d'] = sp.scale_signal(self.score['d'], 0, 100)
321+
self.score['euclideanDistance'] = sp.scale_signal(self.score['euclideanDistance'], 0, 100)
322322

323323

324-
def scale_score(self, status: str, direction : str = 'y') -> None:
324+
def scale_score(self, status: str, metric : str = 'y') -> None:
325325
""" Scale the score to desired stroke high
326326
327327
Note:
328328
We determine the lowerst and highes positions in the score and request the real position from user.
329329
330330
Args:
331331
status (str): a status/info message to display in the window
332-
direction (str): scale the 'y' or 'x' score
332+
metric (str): scale the 'y' or 'x' score
333333
"""
334334
if len(self.score['y']) < 2: return
335335

336336
cap = cv2.VideoCapture(self.params.video_path)
337337
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
338338
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
339339

340-
if direction == 'd':
341-
min_frame = np.argmin(np.array(self.score['d'])) + self.params.start_frame
342-
max_frame = np.argmax(np.array(self.score['d'])) + self.params.start_frame
343-
elif direction == 'x':
340+
if metric == 'euclideanDistance':
341+
min_frame = np.argmin(np.array(self.score['euclideanDistance'])) + self.params.start_frame
342+
max_frame = np.argmax(np.array(self.score['euclideanDistance'])) + self.params.start_frame
343+
elif metric == 'x':
344344
min_frame = np.argmin(np.array(self.score['x'])) + self.params.start_frame
345345
max_frame = np.argmax(np.array(self.score['x'])) + self.params.start_frame
346346
else:
@@ -370,16 +370,16 @@ def scale_score(self, status: str, direction : str = 'y') -> None:
370370
imgMin = cv2.resize(imgMin, None, fx=scale, fy=scale)
371371
imgMax = cv2.resize(imgMax, None, fx=scale, fy=scale)
372372

373-
if direction == 'y':
373+
if metric == 'y':
374374
title_min = "Bottom"
375-
elif direction == 'x':
375+
elif metric == 'x':
376376
title_min = "Left"
377377
else:
378378
title_min = "Minimum"
379379

380-
if direction == 'y':
380+
if metric == 'y':
381381
title_max = "Top"
382-
elif direction == 'x':
382+
elif metric == 'x':
383383
title_max = "Right"
384384
else:
385385
title_max = "Maximum"
@@ -396,9 +396,9 @@ def scale_score(self, status: str, direction : str = 'y') -> None:
396396
desired_min = 0
397397
desired_max = 99
398398

399-
if direction == 'd':
400-
self.score['d'] = sp.scale_signal(self.score['d'], desired_min, desired_max)
401-
elif direction == 'x':
399+
if metric == 'euclideanDistance':
400+
self.score['euclideanDistance'] = sp.scale_signal(self.score['euclideanDistance'], desired_min, desired_max)
401+
elif metric == 'x':
402402
self.score['x'] = sp.scale_signal(self.score['x'], desired_min, desired_max)
403403
else:
404404
self.score['y'] = sp.scale_signal(self.score['y'], desired_min, desired_max)
@@ -796,16 +796,16 @@ def apply_shift(self, frame_number, position: str) -> int:
796796
Args:
797797
position (str): is max or min
798798
"""
799-
if self.params.direction == 'd':
799+
if self.params.metric == 'euclideanDistance':
800800
shift_a = self.params.shift_top_points
801-
elif self.params.direction == 'x':
801+
elif self.params.metric == 'x':
802802
shift_a = self.params.shift_right_points
803803
else:
804804
shift_a = self.params.shift_top_points
805805

806-
if self.params.direction == 'd':
806+
if self.params.metric == 'euclideanDistance':
807807
shift_b = self.params.shift_bottom_points
808-
elif self.params.direction == 'x':
808+
elif self.params.metric == 'x':
809809
shift_b = self.params.shift_left_points
810810
else:
811811
shift_b = self.params.shift_bottom_points
@@ -832,23 +832,23 @@ def get_score_with_offset(self, idx_dict) -> list:
832832
Returns:
833833
list: score with offset
834834
"""
835-
if self.params.direction == 'd':
835+
if self.params.metric == 'euclideanDistance':
836836
offset_a = self.params.top_points_offset
837-
elif self.params.direction == 'x':
837+
elif self.params.metric == 'x':
838838
offset_a = self.params.right_points_offset
839839
else:
840840
offset_a = self.params.top_points_offset
841841

842-
if self.params.direction == 'd':
842+
if self.params.metric == 'euclideanDistance':
843843
offset_b = self.params.bottom_points_offset
844-
elif self.params.direction == 'x':
844+
elif self.params.metric == 'x':
845845
offset_b = self.params.left_points_offset
846846
else:
847847
offset_b = self.params.bottom_points_offset
848848

849-
if self.params.direction == 'd':
850-
score = copy.deepcopy(self.score['d'])
851-
elif self.params.direction == 'x':
849+
if self.params.metric == 'euclideanDistance':
850+
score = copy.deepcopy(self.score['euclideanDistance'])
851+
elif self.params.metric == 'x':
852852
score = copy.deepcopy(self.score['x'])
853853
else:
854854
score = copy.deepcopy(self.score['y'])
@@ -892,9 +892,9 @@ def determin_change_points(self) -> dict:
892892
dict: all local max and min points in score {'min':[idx1, idx2, ...], 'max':[idx1, idx2, ...]}
893893
"""
894894
self.logger.info("Determine change points")
895-
if self.params.direction == 'd':
896-
idx_dict = sp.get_local_max_and_min_idx(self.score['d'], self.video_info.fps)
897-
elif self.params.direction == 'x':
895+
if self.params.metric == 'euclideanDistance':
896+
idx_dict = sp.get_local_max_and_min_idx(self.score['euclideanDistance'], self.video_info.fps)
897+
elif self.params.metric == 'x':
898898
idx_dict = sp.get_local_max_and_min_idx(self.score['x'], self.video_info.fps)
899899
else:
900900
idx_dict = sp.get_local_max_and_min_idx(self.score['y'], self.video_info.fps)
@@ -909,9 +909,9 @@ def create_funscript(self, idx_dict: dict) -> None:
909909
{'min':[idx1, idx2, ...], 'max':[idx1, idx2, ...]}
910910
"""
911911
if self.params.raw_output:
912-
if self.params.direction == 'd':
913-
output_score = copy.deepcopy(self.score['d'])
914-
elif self.params.direction == 'x':
912+
if self.params.metric == 'euclideanDistance':
913+
output_score = copy.deepcopy(self.score['euclideanDistance'])
914+
elif self.params.metric == 'x':
915915
output_score = copy.deepcopy(self.score['x'])
916916
else:
917917
output_score = copy.deepcopy(self.score['y'])
@@ -925,16 +925,16 @@ def create_funscript(self, idx_dict: dict) -> None:
925925
else:
926926
output_score = self.get_score_with_offset(idx_dict)
927927

928-
if self.params.direction == 'd':
928+
if self.params.metric == 'euclideanDistance':
929929
threshold_a = self.params.bottom_threshold
930-
elif self.params.direction == 'x':
930+
elif self.params.metric == 'x':
931931
threshold_a = self.params.left_threshold
932932
else:
933933
threshold_a = self.params.bottom_threshold
934934

935-
if self.params.direction == 'd':
935+
if self.params.metric == 'euclideanDistance':
936936
threshold_b = self.params.top_threshold
937-
elif self.params.direction == 'x':
937+
elif self.params.metric == 'x':
938938
threshold_b = self.params.right_threshold
939939
else:
940940
threshold_b = self.params.top_threshold
@@ -959,9 +959,8 @@ def create_funscript(self, idx_dict: dict) -> None:
959959
def run(self) -> None:
960960
""" The Funscript Generator Thread Function """
961961
try:
962-
if self.params.direction == 'd' and not self.params.track_men:
963-
self.finished("Tracking with 'd' and no men tracker is not implemented!", False)
964-
return
962+
if self.params.metric not in ['x', 'y']:
963+
self.params.track_men = True # we need 2 tracking points
965964

966965
if self.video_info.fps < 31.0 and self.params.skip_frames > 0:
967966
self.logger.warning("The Video has less than 30 frames per seconds and you have set skip_frames to %d "\
@@ -980,7 +979,7 @@ def run(self) -> None:
980979

981980
if len(self.score['y']) >= HYPERPARAMETER['min_frames']:
982981
self.logger.info("Scale score")
983-
self.scale_score(status, direction=self.params.direction)
982+
self.scale_score(status, metric=self.params.metric)
984983

985984
if len(self.score['y']) < HYPERPARAMETER['min_frames']:
986985
self.finished(status + ' -> Tracking time insufficient', False)

funscript_editor/ui/funscript_generator_window.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def run(self) -> None:
108108
FunscriptGeneratorParameter(
109109
video_path = self.video_file,
110110
track_men = self.settings['trackingMethod'] != 'Woman',
111-
direction = self.settings['trackingAxis'],
111+
metric = self.settings['trackingMetric'],
112112
projection = self.settings['videoType'],
113113
start_frame = self.start_frame,
114114
end_frame = self.end_frame

funscript_editor/ui/settings_dialog.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,23 @@ def __setup_ui_bindings(self):
3838
list(filter(lambda x: PROJECTION[x]['name'] == value, PROJECTION.keys()))[0]
3939
)
4040
)
41-
self.ui.trackingAxisComboBox.currentTextChanged.connect(lambda value: self.__set_setting('trackingAxis', value))
41+
self.ui.trackingMetricComboBox.currentTextChanged.connect(self.__set_tracking_metric)
4242
self.ui.trackingMethodComboBox.currentTextChanged.connect(lambda value: self.__set_setting('trackingMethod', value))
4343

4444
def __setup_combo_boxes(self):
4545
self.ui.videoTypeComboBox.addItems([PROJECTION[key]['name'] for key in PROJECTION.keys()])
46-
self.ui.trackingAxisComboBox.addItems(['y', 'x'])
46+
self.ui.trackingMetricComboBox.addItems(['y', 'x', 'euclideanDistance'])
4747
self.ui.trackingMethodComboBox.addItems(['Woman', 'Woman + Men'])
4848

49+
def __set_tracking_metric(self, value):
50+
if value in ['x', 'y']:
51+
self.ui.trackingMethodComboBox.setEnabled(True)
52+
else:
53+
self.ui.trackingMethodComboBox.setCurrentText('Woman + Men')
54+
self.ui.trackingMethodComboBox.setEnabled(False)
55+
56+
self.__set_setting('trackingMetric', value)
57+
4958
def __apply(self):
5059
self.form.hide()
5160
self.applySettings.emit()

funscript_editor/ui/settings_view.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ def setupUi(self, Form):
4141
self.label_3 = QtWidgets.QLabel(Form)
4242
self.label_3.setObjectName("label_3")
4343
self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.label_3)
44-
self.trackingAxisComboBox = QtWidgets.QComboBox(Form)
44+
self.trackingMetricComboBox = QtWidgets.QComboBox(Form)
4545
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Fixed)
4646
sizePolicy.setHorizontalStretch(0)
4747
sizePolicy.setVerticalStretch(0)
48-
sizePolicy.setHeightForWidth(self.trackingAxisComboBox.sizePolicy().hasHeightForWidth())
49-
self.trackingAxisComboBox.setSizePolicy(sizePolicy)
50-
self.trackingAxisComboBox.setObjectName("trackingAxisComboBox")
51-
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.trackingAxisComboBox)
48+
sizePolicy.setHeightForWidth(self.trackingMetricComboBox.sizePolicy().hasHeightForWidth())
49+
self.trackingMetricComboBox.setSizePolicy(sizePolicy)
50+
self.trackingMetricComboBox.setObjectName("trackingMetricComboBox")
51+
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.trackingMetricComboBox)
5252
self.trackingMethodComboBox = QtWidgets.QComboBox(Form)
5353
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Fixed)
5454
sizePolicy.setHorizontalStretch(0)
@@ -83,7 +83,7 @@ def retranslateUi(self, Form):
8383
_translate = QtCore.QCoreApplication.translate
8484
Form.setWindowTitle(_translate("Form", "Form"))
8585
self.label.setText(_translate("Form", "Video Type:"))
86-
self.label_2.setText(_translate("Form", "Tracking Axis:"))
86+
self.label_2.setText(_translate("Form", "Tracking Metric:"))
8787
self.label_3.setText(_translate("Form", "Tracking Method:"))
8888
self.okButton.setText(_translate("Form", "OK"))
8989
self.label_4.setText(_translate("Form", " # Settings"))

funscript_editor/ui/settings_view.ui

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<item row="2" column="0">
6565
<widget class="QLabel" name="label_2">
6666
<property name="text">
67-
<string>Tracking Axis:</string>
67+
<string>Tracking Metric:</string>
6868
</property>
6969
</widget>
7070
</item>
@@ -76,7 +76,7 @@
7676
</widget>
7777
</item>
7878
<item row="2" column="1">
79-
<widget class="QComboBox" name="trackingAxisComboBox">
79+
<widget class="QComboBox" name="trackingMetricComboBox">
8080
<property name="sizePolicy">
8181
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
8282
<horstretch>0</horstretch>

0 commit comments

Comments
 (0)