Skip to content
Snippets Groups Projects

Update video-compare script

Merged Schrödter, Tobias requested to merge updated-compare-skript into master
All threads resolved!
1 file
+ 110
51
Compare changes
  • Side-by-side
  • Inline
import argparse
from typing import Callable, List, Set, Tuple
try:
import cv2
except ImportError:
@@ -99,8 +100,8 @@ class ComparisonVideoPlayer:
nearby_people_truth: List[int] = []
for person in self.test_trajectories:
if (
seed_person.last_frame < person.first_frame
or seed_person.first_frame > person.last_frame
seed_person.last_frame < person.first_frame
or seed_person.first_frame > person.last_frame
):
continue
if seed_person.first_frame > person.first_frame:
@@ -117,8 +118,8 @@ class ComparisonVideoPlayer:
nearby_people_test.append(person.id + 1)
for person in self.truth_trajectories:
if (
seed_person.last_frame < person.first_frame
or seed_person.first_frame > person.last_frame
seed_person.last_frame < person.first_frame
or seed_person.first_frame > person.last_frame
):
continue
if seed_person.first_frame > person.first_frame:
@@ -136,7 +137,7 @@ class ComparisonVideoPlayer:
return nearby_people_truth, nearby_people_test
def play_video(
self, start: int, end: int, draw_callback: Callable[[np.ndarray, int], None]
self, start: int, end: int, draw_callback: Callable[[np.ndarray, int], None]
):
"""Plays the video from frame start to frame end, calling the draw_callback each frame
@@ -155,57 +156,92 @@ class ComparisonVideoPlayer:
currFrame = start
self.stream.set(cv2.CAP_PROP_POS_FRAMES, start)
playing = True
while (k := cv2.waitKey(20)) != 110:
grabbed, frame = self.stream.read()
if not grabbed or currFrame > end:
self.stream.set(cv2.CAP_PROP_POS_FRAMES, start)
currFrame = start
_, frame = self.stream.read()
frame = cv2.copyMakeBorder(
frame,
self.border,
self.border,
self.border,
self.border,
cv2.BORDER_CONSTANT,
)
frame = cv2.remap(
frame,
self.map1,
self.map2,
cv2.INTER_LINEAR,
borderMode=cv2.BORDER_CONSTANT,
)
if not playing:
if k == ord('a') or k == ord('d'):
draw_callback(frame, currFrame)
cv2.putText(frame, "Frame: {:7d}".format(currFrame), (50,100), cv2.FONT_HERSHEY_DUPLEX, 3, (0,0,255), 5, cv2.LINE_8)
if k == ord('a'):
newFrame = max(currFrame - 1, start)
if k == ord('d'):
newFrame = min(currFrame + 1, end)
cv2.imshow("Comparison", frame)
if newFrame <= currFrame:
self.stream.set(cv2.CAP_PROP_POS_FRAMES, newFrame)
currFrame += 1
currFrame = newFrame
_, frame = self.stream.read()
if k == ord('p'):
while cv2.waitKey(-1) != ord('p'):
if k == ord('w'):
currFrame = currFrame+1
if k == ord('s'):
currFrame = currFrame-1
pass
frame = cv2.copyMakeBorder(
frame,
self.border,
self.border,
self.border,
self.border,
cv2.BORDER_CONSTANT,
)
frame = cv2.remap(
frame,
self.map1,
self.map2,
cv2.INTER_LINEAR,
borderMode=cv2.BORDER_CONSTANT,
)
draw_callback(frame, currFrame)
cv2.putText(frame, "Frame: {:7d}".format(currFrame), (50, 100), cv2.FONT_HERSHEY_DUPLEX, 3,
(0, 0, 255), 5, cv2.LINE_8)
cv2.imshow("Comparison", frame)
else:
grabbed, frame = self.stream.read()
if not grabbed or currFrame > end:
self.stream.set(cv2.CAP_PROP_POS_FRAMES, start)
currFrame = start
_, frame = self.stream.read()
frame = cv2.copyMakeBorder(
frame,
self.border,
self.border,
self.border,
self.border,
cv2.BORDER_CONSTANT,
)
frame = cv2.remap(
frame,
self.map1,
self.map2,
cv2.INTER_LINEAR,
borderMode=cv2.BORDER_CONSTANT,
)
draw_callback(frame, currFrame)
cv2.putText(frame, "Frame: {:7d}".format(currFrame), (50, 100), cv2.FONT_HERSHEY_DUPLEX, 3, (0, 0, 255),
5, cv2.LINE_8)
cv2.imshow("Comparison", frame)
currFrame += 1
if k == ord('p'):
playing = not playing
def drawPoints(
self,
person: Person,
currFrame: int,
frame: np.ndarray,
color: Tuple[int],
thickness: int,
self,
person: Person,
currFrame: int,
frame: np.ndarray,
color: Tuple[int],
thickness: int,
):
if person.first_frame <= currFrame <= person.last_frame:
points = person.points
for i in range(min(10, currFrame - person.first_frame)):
if i == 0:
continue
frame = cv2.circle(
frame,
(
@@ -214,9 +250,13 @@ class ComparisonVideoPlayer:
),
thickness,
color,
-1,
1,
)
for i in range(min(10, person.last_frame - currFrame)):
if i == 0:
continue
frame = cv2.circle(
frame,
(
@@ -225,9 +265,27 @@ class ComparisonVideoPlayer:
),
thickness,
color,
-1,
1,
)
marker = cv2.MARKER_CROSS
if color == (255, 0, 0):
marker = cv2.MARKER_TILTED_CROSS
frame = cv2.drawMarker(
frame,
(
int(points[currFrame].x + self.border),
int(points[currFrame].y + self.border),
),
color,
marker,
thickness=2,
line_type=cv2.LINE_AA
)
def visualize_people(self, idx_truth: int, idx_test: int):
tr = self.truth_trajectories[idx_truth - 1]
te = self.test_trajectories[idx_test - 1]
@@ -244,17 +302,18 @@ class ComparisonVideoPlayer:
truth = self.truth_trajectories[idx_truth - 1].points[currFrame]
test = self.test_trajectories[idx_test - 1].points[currFrame]
diff = math.sqrt((truth.x - test.x)**2 + (truth.y - test.y)**2)
frame = cv2.putText(frame, "Diff: {:7.3f}".format(diff), (4100,100), cv2.FONT_HERSHEY_DUPLEX, 3, (0,0,255), 5, cv2.LINE_8)
diff = math.sqrt((truth.x - test.x) ** 2 + (truth.y - test.y) ** 2)
frame = cv2.putText(frame, "Diff: {:7.3f}".format(diff), (4100, 100), cv2.FONT_HERSHEY_DUPLEX, 3,
(0, 0, 255), 5, cv2.LINE_8)
self.play_video(start, end, draw)
def visualize_many(
self,
idxs_truth: List[int],
idxs_test: List[int],
idx_seed: int,
is_seed_test: bool,
self,
idxs_truth: List[int],
idxs_test: List[int],
idx_seed: int,
is_seed_test: bool,
):
# Idea: Try out taking the first first and last last frame, as in two people?
if is_seed_test:
Loading