File size: 6,245 Bytes
eb09c29 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
#!/usr/bin/env python3
"""
Create a synthetic test video for verifying the tensor creation fix.
This script generates a simple MP4 video with moving shapes that can be used
to test the video action recognition pipeline.
"""
import cv2
import numpy as np
from pathlib import Path
import argparse
def create_test_video(output_path: Path, duration: int = 5, fps: int = 24, width: int = 640, height: int = 480):
"""
Create a synthetic test video with moving objects.
Args:
output_path: Path where to save the video
duration: Video duration in seconds
fps: Frames per second
width: Video width in pixels
height: Video height in pixels
"""
# Set up video writer
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(str(output_path), fourcc, fps, (width, height))
if not out.isOpened():
raise RuntimeError(f"Could not open video writer for {output_path}")
total_frames = duration * fps
print(f"Creating video with {total_frames} frames at {fps} FPS...")
for frame_num in range(total_frames):
# Create a blank frame
frame = np.zeros((height, width, 3), dtype=np.uint8)
# Calculate animation parameters
progress = frame_num / total_frames
# Moving rectangle (simulates "sliding" action)
rect_x = int(50 + (width - 150) * progress)
rect_y = height // 2 - 25
cv2.rectangle(frame, (rect_x, rect_y), (rect_x + 100, rect_y + 50), (0, 255, 0), -1)
# Bouncing circle (simulates "bouncing ball" action)
circle_x = width // 4
circle_y = int(height // 2 + 100 * np.sin(progress * 4 * np.pi))
cv2.circle(frame, (circle_x, circle_y), 30, (255, 100, 100), -1)
# Rotating line (simulates "waving" or "gesturing" action)
center_x, center_y = 3 * width // 4, height // 2
angle = progress * 4 * np.pi
end_x = int(center_x + 80 * np.cos(angle))
end_y = int(center_y + 80 * np.sin(angle))
cv2.line(frame, (center_x, center_y), (end_x, end_y), (100, 100, 255), 8)
# Add frame number for debugging
cv2.putText(frame, f'Frame {frame_num+1}/{total_frames}',
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
# Add title
cv2.putText(frame, 'Test Video - Multiple Actions',
(width//2 - 150, height - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
# Write frame to video
out.write(frame)
if frame_num % 24 == 0: # Progress update every second
print(f" Progress: {frame_num+1}/{total_frames} frames ({(frame_num+1)/total_frames*100:.1f}%)")
# Clean up
out.release()
cv2.destroyAllWindows()
print(f"✅ Video created successfully: {output_path}")
print(f" Duration: {duration} seconds")
print(f" Resolution: {width}x{height}")
print(f" Frame rate: {fps} FPS")
print(f" File size: {output_path.stat().st_size / 1024 / 1024:.1f} MB")
def create_multiple_test_videos(output_dir: Path):
"""Create several test videos with different characteristics."""
output_dir.mkdir(exist_ok=True)
test_configs = [
{
"name": "short_action.mp4",
"duration": 3,
"fps": 30,
"width": 640,
"height": 480,
"description": "Short 3-second video with basic actions"
},
{
"name": "standard_action.mp4",
"duration": 5,
"fps": 24,
"width": 640,
"height": 480,
"description": "Standard 5-second video"
},
{
"name": "hd_action.mp4",
"duration": 4,
"fps": 30,
"width": 1280,
"height": 720,
"description": "HD resolution test video"
},
{
"name": "long_action.mp4",
"duration": 10,
"fps": 24,
"width": 640,
"height": 480,
"description": "Longer video for extended testing"
}
]
print("Creating multiple test videos...")
print("=" * 50)
for config in test_configs:
print(f"\n📽️ Creating: {config['name']}")
print(f" {config['description']}")
video_path = output_dir / config['name']
create_test_video(
output_path=video_path,
duration=config['duration'],
fps=config['fps'],
width=config['width'],
height=config['height']
)
print(f"\n🎉 All test videos created in: {output_dir}")
print("\nYou can now use these videos to test the action recognition system:")
for config in test_configs:
print(f" - {config['name']}: {config['description']}")
def main():
parser = argparse.ArgumentParser(description="Create synthetic test videos for action recognition")
parser.add_argument("--output", "-o", type=Path, default=Path("test_videos"),
help="Output directory for test videos")
parser.add_argument("--single", "-s", type=str, help="Create single video with this filename")
parser.add_argument("--duration", "-d", type=int, default=5, help="Video duration in seconds")
parser.add_argument("--fps", type=int, default=24, help="Frames per second")
parser.add_argument("--width", "-w", type=int, default=640, help="Video width")
parser.add_argument("--height", "-h", type=int, default=480, help="Video height")
args = parser.parse_args()
try:
if args.single:
# Create single video
output_path = args.output / args.single
output_path.parent.mkdir(parents=True, exist_ok=True)
create_test_video(
output_path=output_path,
duration=args.duration,
fps=args.fps,
width=args.width,
height=args.height
)
else:
# Create multiple test videos
create_multiple_test_videos(args.output)
except Exception as e:
print(f"❌ Error creating test video(s): {e}")
return 1
return 0
if __name__ == "__main__":
exit(main())
|