Skip to content

Commit cecef04

Browse files
author
arch
committed
improve supervised tracking option
1 parent c3e0450 commit cecef04

File tree

4 files changed

+65
-22
lines changed

4 files changed

+65
-22
lines changed

funscript_editor/algorithms/funscriptgenerator.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class FunscriptGeneratorParameter:
3535
""" Funscript Generator Parameter Dataclass with default values """
3636
video_path: str
3737
track_men: bool
38+
supervised_tracking: bool
3839
metric: str
3940
projection: str
40-
supervised_tracking: bool = True
4141

4242
# Settings
4343
start_frame: int = 0 # default is video start (input: set current video position)
@@ -393,6 +393,8 @@ def scale_score(self, status: str, metric : str = 'y') -> None:
393393
elif 'ou' in self.params.projection.split('_'):
394394
imgMin = imgMin[:int(imgMin.shape[0]/2), :]
395395
imgMax = imgMax[:int(imgMax.shape[0]/2), :]
396+
else:
397+
self.logger.warning("Unknown VR Projection Type: %s", self.params.projection)
396398

397399
if PROJECTION[self.params.projection]['parameter']['width'] > 0:
398400
scale = PROJECTION[self.params.projection]['parameter']['width'] / float(1.75*imgMax.shape[1])
@@ -671,7 +673,10 @@ def tracking(self) -> str:
671673
bbox_woman = self.get_bbox(first_frame, "Select Woman Feature")
672674
preview_frame = self.draw_box(first_frame, bbox_woman, color=(255,0,255))
673675
if self.params.supervised_tracking:
674-
tracking_area_woman = self.get_bbox(preview_frame, "Select the Supervised Tracking Area for the Woman Feature")
676+
while True:
677+
tracking_area_woman = self.get_bbox(preview_frame, "Select the Supervised Tracking Area for the Woman Feature")
678+
if StaticVideoTracker.is_bbox_in_tracking_area(bbox_woman, tracking_area_woman): break
679+
self.logger.error("Invalid supervised tracking area selected")
675680
preview_frame = self.draw_box(preview_frame, tracking_area_woman, color=(0,255,0))
676681
tracker_woman = StaticVideoTracker(first_frame, bbox_woman, supervised_tracking_area = tracking_area_woman)
677682
else:
@@ -682,7 +687,10 @@ def tracking(self) -> str:
682687
bbox_men = self.get_bbox(preview_frame, "Select Men Feature")
683688
preview_frame = self.draw_box(preview_frame, bbox_men, color=(255,0,255))
684689
if self.params.supervised_tracking:
685-
tracking_area_men = self.get_bbox(preview_frame, "Select the Supervised Tracking Area for the Men Feature")
690+
while True:
691+
tracking_area_men = self.get_bbox(preview_frame, "Select the Supervised Tracking Area for the Men Feature")
692+
if StaticVideoTracker.is_bbox_in_tracking_area(bbox_men, tracking_area_men): break
693+
self.logger.error("Invalid supervised tracking area selected")
686694
tracker_men = StaticVideoTracker(first_frame, bbox_men, supervised_tracking_area = tracking_area_men)
687695
else:
688696
tracker_men = StaticVideoTracker(first_frame, bbox_men)
@@ -720,15 +728,15 @@ def tracking(self) -> str:
720728
# Process data from last step while the next tracking points get predicted.
721729
# This should improve the whole processing speed, because the tracker run in a seperate thread
722730
bboxes['Woman'][frame_num-1] = bbox_woman
723-
last_frame = self.draw_box(last_frame, bboxes['Woman'][frame_num-1])
731+
last_frame = self.draw_box(last_frame, bboxes['Woman'][frame_num-1], color=(0,255,0))
724732
if self.params.supervised_tracking:
725733
last_frame = self.draw_box(last_frame, tracking_area_woman, color=(0,255,0))
726734

727735
if self.params.track_men:
728736
bboxes['Men'][frame_num-1] = bbox_men
729-
last_frame = self.draw_box(last_frame, bboxes['Men'][frame_num-1])
737+
last_frame = self.draw_box(last_frame, bboxes['Men'][frame_num-1], color=(255,0,255))
730738
if self.params.supervised_tracking:
731-
last_frame = self.draw_box(last_frame, tracking_area_men, color=(0,255,0))
739+
last_frame = self.draw_box(last_frame, tracking_area_men, color=(255,0,255))
732740

733741
last_frame = self.draw_fps(last_frame)
734742
cv2.putText(last_frame, "Press 'q' if the tracking point shifts or a video cut occured",

funscript_editor/algorithms/videotracker.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class StaticVideoTracker:
2121
first_frame (np.ndarray): open cv image representing the start frame
2222
tracking_bbox (tuple): tuple with (x,y,w,h) of the init tracking box
2323
limit_searchspace (dict) : only insert the specified region around the init box
24+
supervised_tracking_area (tuple, optional): tuple with (x,y,w,h) of the supervised tracking area
2425
queue_size (int): in (work) and out (result) queue size
2526
"""
2627

@@ -46,6 +47,37 @@ def __init__(self,
4647
__logger = logging.getLogger(__name__)
4748

4849

50+
@staticmethod
51+
def is_bbox_in_tracking_area(bbox: tuple, supervised_tracking_area: tuple = None) -> bool:
52+
""" Check if tracking box is inside the supervised tracking area
53+
54+
Args:
55+
bbox (tuple): tuple with (x,y,w,h) of the tracking box
56+
supervised_tracking_area (tuple, optional): tuple with (x,y,w,h) of the supervised tracking area
57+
58+
Returns:
59+
bool: True if tracking box is in supervised_tracking_area else False
60+
"""
61+
if bbox is None:
62+
return False
63+
if supervised_tracking_area is None:
64+
return True
65+
if supervised_tracking_area[2] <= 1:
66+
return False
67+
if supervised_tracking_area[3] <= 1:
68+
return False
69+
if bbox[0] < supervised_tracking_area[0]:
70+
return False
71+
if bbox[1] < supervised_tracking_area[1]:
72+
return False
73+
if bbox[0] + bbox[2] > supervised_tracking_area[0] + supervised_tracking_area[2]:
74+
return False
75+
if bbox[1] + bbox[3] > supervised_tracking_area[1] + supervised_tracking_area[3]:
76+
return False
77+
78+
return True
79+
80+
4981
def stop(self) -> None:
5082
""" Stop the tracker thread """
5183
self.stopped = True
@@ -117,15 +149,8 @@ def run(self) -> None:
117149
if success:
118150
status = "OK"
119151
bbox = (int(bbox[0] + x0), int(bbox[1] + y0), int(bbox[2]), int(bbox[3]))
120-
if self.supervised_tracking_area is not None:
121-
if bbox[0] < self.supervised_tracking_area[0]:
122-
status = "Feature outside the specified area"
123-
elif bbox[1] < self.supervised_tracking_area[1]:
124-
status = "Feature outside the specified area"
125-
elif bbox[0] + bbox[2] > self.supervised_tracking_area[0] + self.supervised_tracking_area[2]:
126-
status = "Feature outside the specified area"
127-
elif bbox[1] + bbox[3] > self.supervised_tracking_area[1] + self.supervised_tracking_area[3]:
128-
status = "Feature outside the specified area"
152+
if not StaticVideoTracker.is_bbox_in_tracking_area(bbox, self.supervised_tracking_area):
153+
status = "Feature outside the specified area"
129154

130155
self.queue_out.put((status, bbox))
131156

funscript_editor/ui/funscript_generator_window.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ def run(self) -> None:
111111
self.funscript_generator = FunscriptGeneratorThread(
112112
FunscriptGeneratorParameter(
113113
video_path = self.video_file,
114-
track_men = self.settings['trackingMethod'] != 'Woman',
114+
track_men = 'Men' in self.settings['trackingMethod'],
115+
supervised_tracking = 'Supervised' in self.settings['trackingMethod'],
115116
metric = self.settings['trackingMetric'],
116117
projection = self.settings['videoType'],
117118
start_frame = self.start_frame,

funscript_editor/ui/settings_dialog.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ def __init__(self, settings: dict, include_vr: bool = True):
2828
#: apply settings event
2929
applySettings = QtCore.pyqtSignal()
3030

31+
3132
def show(self):
32-
""" Show wttings dialog """
33+
""" Show settings dialog """
3334
self.form.show()
3435

36+
3537
def __setup_ui_bindings(self):
3638
self.ui.okButton.clicked.connect(self.__apply)
3739
self.ui.videoTypeComboBox.currentTextChanged.connect(
@@ -43,27 +45,34 @@ def __setup_ui_bindings(self):
4345
self.ui.trackingMetricComboBox.currentTextChanged.connect(self.__set_tracking_metric)
4446
self.ui.trackingMethodComboBox.currentTextChanged.connect(lambda value: self.__set_setting('trackingMethod', value))
4547

48+
4649
def __setup_combo_boxes(self):
4750
self.ui.videoTypeComboBox.addItems([PROJECTION[key]['name'] \
4851
for key in PROJECTION.keys() \
4952
if 'vr' not in key.lower() or self.include_vr])
53+
self.ui.trackingMethodComboBox.addItems(['Unsupervised Woman', 'Unsupervised Woman + Men', 'Supervised Woman', 'Supervised Woman + Men']) # set before tracking metric
5054
self.ui.trackingMetricComboBox.addItems(['y (up-down)', 'x (left-right)', 'euclideanDistance', 'roll (rotation)'])
51-
# self.ui.trackingMethodComboBox.addItems(['Unsupervised Woman', 'Unsupervised Woman + Men', 'Supervised Woman', 'Supervised Woman + Men'])
52-
self.ui.trackingMethodComboBox.addItems(['Woman', 'Woman + Men'])
55+
5356

5457
def __set_tracking_metric(self, value):
5558
value = value.split('(')[0].strip()
59+
current_tracking_method_items = [self.ui.trackingMethodComboBox.itemText(i) for i in range(self.ui.trackingMethodComboBox.count())]
60+
5661
if value in ['x', 'y']:
57-
self.ui.trackingMethodComboBox.setEnabled(True)
62+
if 'Unsupervised Woman' not in current_tracking_method_items:
63+
self.ui.trackingMethodComboBox.addItems(['Unsupervised Woman', 'Supervised Woman'])
5864
else:
59-
self.ui.trackingMethodComboBox.setCurrentText('Woman + Men')
60-
self.ui.trackingMethodComboBox.setEnabled(False)
65+
if 'Unsupervised Woman' in current_tracking_method_items:
66+
self.ui.trackingMethodComboBox.clear()
67+
self.ui.trackingMethodComboBox.addItems(['Unsupervised Woman + Men', 'Supervised Woman + Men'])
6168

6269
self.__set_setting('trackingMetric', value)
6370

71+
6472
def __apply(self):
6573
self.form.hide()
6674
self.applySettings.emit()
6775

76+
6877
def __set_setting(self, key, value):
6978
self.settings[key] = value

0 commit comments

Comments
 (0)