Candle commited on
Commit ·
0dca549
1
Parent(s): 670fa55
visualizing motion
Browse files- data/shots/sample-000-0.motion.mp4 +3 -0
- requirements.txt +1 -0
- visualize_motion.py +84 -0
data/shots/sample-000-0.motion.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4d7b60d94b5ceea0e4009f493ace81406301e0572ba21ee4f5964d8a95d4b394
|
| 3 |
+
size 265232
|
requirements.txt
CHANGED
|
@@ -3,3 +3,4 @@ torch
|
|
| 3 |
numpy
|
| 4 |
matplotlib
|
| 5 |
streamlit
|
|
|
|
|
|
| 3 |
numpy
|
| 4 |
matplotlib
|
| 5 |
streamlit
|
| 6 |
+
opencv-python
|
visualize_motion.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Select files using glob (for now, only process the first file for testing)
|
| 7 |
+
shots_dir = Path('data/shots')
|
| 8 |
+
files = sorted(shots_dir.glob('sample-000-0.webp')) # Change pattern to 'sample-*.webp' for batch
|
| 9 |
+
if not files:
|
| 10 |
+
print('No files found.')
|
| 11 |
+
exit(1)
|
| 12 |
+
|
| 13 |
+
# Process each file serially (for now, just one file)
|
| 14 |
+
for webp_path in files:
|
| 15 |
+
print(f'Processing {webp_path}')
|
| 16 |
+
# Extract all frames from the animated webp using PIL
|
| 17 |
+
frames = []
|
| 18 |
+
frame_durations = []
|
| 19 |
+
with Image.open(webp_path) as im:
|
| 20 |
+
try:
|
| 21 |
+
while True:
|
| 22 |
+
frame = im.convert('RGB')
|
| 23 |
+
frames.append(np.array(frame)[:, :, ::-1]) # Convert RGB to BGR for OpenCV
|
| 24 |
+
# Get duration in ms for this frame (default to 100ms if not present)
|
| 25 |
+
duration = im.info.get('duration', 100)
|
| 26 |
+
frame_durations.append(duration)
|
| 27 |
+
im.seek(im.tell() + 1)
|
| 28 |
+
except EOFError:
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
# Debug: check extracted frames
|
| 32 |
+
print(f"Extracted {len(frames)} frames from {webp_path}")
|
| 33 |
+
if len(frames) > 0:
|
| 34 |
+
print(f"First frame shape: {frames[0].shape}, dtype: {frames[0].dtype}, min: {frames[0].min()}, max: {frames[0].max()}")
|
| 35 |
+
|
| 36 |
+
# Compute dense optical flow and overlay visualization
|
| 37 |
+
hsv = None
|
| 38 |
+
motion_frames = []
|
| 39 |
+
for i in range(1, len(frames)):
|
| 40 |
+
prev = cv2.cvtColor(frames[i-1], cv2.COLOR_BGR2GRAY)
|
| 41 |
+
curr = cv2.cvtColor(frames[i], cv2.COLOR_BGR2GRAY)
|
| 42 |
+
flow = cv2.calcOpticalFlowFarneback(prev, curr, None, 0.5, 3, 15, 3, 5, 1.2, 0)
|
| 43 |
+
mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
|
| 44 |
+
print(f"Frame {i}: flow mag min={mag.min()}, max={mag.max()}, mean={mag.mean()}")
|
| 45 |
+
if np.all(mag == 0):
|
| 46 |
+
print(f"Frame {i}: All zero motion, skipping.")
|
| 47 |
+
continue
|
| 48 |
+
# Draw flow as arrows on a grid
|
| 49 |
+
step = 16 # grid size
|
| 50 |
+
arrow_color = (0, 255, 0) # green
|
| 51 |
+
arrow_thickness = 1
|
| 52 |
+
overlay = frames[i].copy()
|
| 53 |
+
h, w = prev.shape
|
| 54 |
+
for y in range(0, h, step):
|
| 55 |
+
for x in range(0, w, step):
|
| 56 |
+
fx, fy = flow[y, x]
|
| 57 |
+
end_x = int(x + fx * 4)
|
| 58 |
+
end_y = int(y + fy * 4)
|
| 59 |
+
cv2.arrowedLine(overlay, (x, y), (end_x, end_y), arrow_color, arrow_thickness, tipLength=0.3)
|
| 60 |
+
motion_frames.append(overlay)
|
| 61 |
+
|
| 62 |
+
# Save as mp4
|
| 63 |
+
if motion_frames:
|
| 64 |
+
height, width, _ = motion_frames[0].shape
|
| 65 |
+
# Calculate FPS from frame durations (use mean duration between frames)
|
| 66 |
+
if len(frame_durations) > 1:
|
| 67 |
+
# Use durations between frames (skip first frame)
|
| 68 |
+
mean_duration = np.mean(frame_durations[1:])
|
| 69 |
+
else:
|
| 70 |
+
mean_duration = 100
|
| 71 |
+
fps = 1000.0 / mean_duration if mean_duration > 0 else 10
|
| 72 |
+
print(f"Using FPS: {fps:.2f} (mean frame duration: {mean_duration} ms)")
|
| 73 |
+
if hasattr(cv2, 'VideoWriter_fourcc'):
|
| 74 |
+
fourcc = cv2.VideoWriter_fourcc(*'avc1') # More compatible MP4 codec for macOS
|
| 75 |
+
else:
|
| 76 |
+
raise RuntimeError('cv2.VideoWriter_fourcc is not available in your OpenCV installation. Please update OpenCV.')
|
| 77 |
+
out_path = webp_path.parent / f"{webp_path.stem}.motion.mp4"
|
| 78 |
+
out = cv2.VideoWriter(str(out_path), fourcc, fps, (width, height))
|
| 79 |
+
for f in motion_frames:
|
| 80 |
+
out.write(f)
|
| 81 |
+
out.release()
|
| 82 |
+
print(f'Saved {out_path}')
|
| 83 |
+
else:
|
| 84 |
+
print('No motion frames to save for', webp_path)
|