Skip to content

Commit 8a534ec

Browse files
author
arch
committed
optical flow testing
1 parent d05d456 commit 8a534ec

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

funscript_editor/algorithms/opticalflow.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from funscript_editor.algorithms.signal import Signal
1515
from funscript_editor.ui.opencvui import OpenCV_GUI, OpenCV_GUI_Parameters
1616

17+
import matplotlib.pyplot as plt
18+
1719
@dataclass
1820
class OpticalFlowFunscriptGeneratorParameter:
1921
""" Funscript Generator Parameter Dataclass with default values """
@@ -22,7 +24,7 @@ class OpticalFlowFunscriptGeneratorParameter:
2224
start_frame: int
2325
end_frame: int = -1 # default is video end (-1)
2426
skip_frames: int = 0
25-
min_trajectory_len: int = 60
27+
min_trajectory_len: int = 50
2628
feature_detect_interval: int = 10
2729
movement_filter: float = 10.0
2830

@@ -121,20 +123,20 @@ def update(self, frame_roi):
121123
def get_result(self):
122124
result = copy.deepcopy(self.result)
123125
for trajectory in self.trajectories:
124-
if len (trajectory) > self.min_trajectory_len:
126+
if len (trajectory) > (self.min_trajectory_len / 2):
125127
result.append({'end': self.frame_idx, 'trajectory': trajectory})
126128

127129
return { 'meta': { 'last_idx': self.frame_idx }, 'data': result }
128130

129131

130-
def extract_movement(self, optical_flow_result, metric_idx = 1):
132+
def extract_movement(self, optical_flow_result, metric_idx = 1, filter_static_points = True):
131133
result = []
132134
for r in optical_flow_result['data']:
133135
zero_before = r['end'] - len(r['trajectory'])
134136
zero_after = optical_flow_result['meta']['last_idx'] - r['end']
135137
trajectory_min = min([item[metric_idx] for item in r['trajectory']])
136138
y = [0 for _ in range(zero_before)] + [(r['trajectory'][i][metric_idx] - trajectory_min)**2 for i in range(len(r['trajectory']))] + [0 for _ in range(zero_after)]
137-
if max(y) - min(y) > self.params.movement_filter:
139+
if not filter_static_points or (max(y) - min(y)) > self.params.movement_filter:
138140
result.append(y)
139141

140142
return result
@@ -175,7 +177,7 @@ def tracking(self) -> str:
175177

176178
roi = self.ui.bbox_selector(
177179
first_frame,
178-
"Select ROI",
180+
"Select observe area of an single person",
179181
)
180182

181183
optical_flow = OpticalFlowFunscriptGeneratorThread.OpticalFlowPyrLK(
@@ -215,13 +217,26 @@ def tracking(self) -> str:
215217
break
216218

217219
result = optical_flow.get_result()
220+
221+
# for filter_static_points in [True, False]:
222+
# test = self.extract_movement(result, filter_static_points=filter_static_points)
223+
# for i in [1, 2, 3, 4]:
224+
# pca = PCA(n_components=i)
225+
# principalComponents = pca.fit_transform(np.transpose(np.array(test)))
226+
# test_result = np.array(principalComponents)
227+
# plt.plot(test_result)
228+
# plt.savefig('debug_{}_{}.png'.format(filter_static_points, i), dpi=400)
229+
# plt.close()
230+
218231
result = self.extract_movement(result)
219232

220-
pca = PCA(n_components=2)
233+
pca = PCA(n_components=1)
221234
principalComponents = pca.fit_transform(np.transpose(np.array(result)))
222-
result = np.transpose(np.array(principalComponents))
235+
result = [x[0] for x in principalComponents]
223236

224-
result = np.array(result[0]) - np.array(result[1])
237+
# option for pca 2 with two moving persons:
238+
# result = np.transpose(np.array(principalComponents))
239+
# result = np.array(result[0]) - np.array(result[1])
225240

226241
signal = Signal(self.video_info.fps)
227242
points = signal.get_local_min_max_points(result)

0 commit comments

Comments
 (0)