Spaces:
Sleeping
Sleeping
File size: 7,641 Bytes
3b90d9c |
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
"""
I/O utilities for video processing and data export.
This module provides functions for reading/writing videos,
exporting trajectory data to CSV, and handling file operations.
"""
import cv2
import csv
import numpy as np
from typing import List, Tuple, Optional, Generator
from pathlib import Path
class VideoReader:
"""
Context manager for reading video files frame by frame.
Attributes:
video_path (str): Path to input video file
cap (cv2.VideoCapture): OpenCV video capture object
"""
def __init__(self, video_path: str):
"""
Initialize video reader.
Args:
video_path: Path to the video file
Raises:
FileNotFoundError: If video file doesn't exist
RuntimeError: If video cannot be opened
"""
self.video_path = video_path
if not Path(video_path).exists():
raise FileNotFoundError(f"Video file not found: {video_path}")
self.cap = cv2.VideoCapture(video_path)
if not self.cap.isOpened():
raise RuntimeError(f"Failed to open video: {video_path}")
def __enter__(self):
"""Context manager entry."""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Context manager exit - release video capture."""
self.cap.release()
def get_properties(self) -> dict:
"""
Get video properties.
Returns:
Dictionary containing fps, frame_count, width, height
"""
return {
'fps': self.cap.get(cv2.CAP_PROP_FPS),
'frame_count': int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT)),
'width': int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
'height': int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
}
def read_frames(self) -> Generator[Tuple[int, np.ndarray], None, None]:
"""
Generator that yields frames from the video.
Yields:
Tuple of (frame_number, frame_array)
"""
frame_num = 0
while True:
ret, frame = self.cap.read()
if not ret:
break
yield frame_num, frame
frame_num += 1
def read_frame(self) -> Tuple[bool, Optional[np.ndarray]]:
"""
Read a single frame.
Returns:
Tuple of (success, frame) where success is a boolean
"""
return self.cap.read()
class VideoWriter:
"""
Context manager for writing video files.
Attributes:
output_path (str): Path to output video file
fps (float): Frame rate
width (int): Frame width
height (int): Frame height
"""
def __init__(
self,
output_path: str,
fps: float,
width: int,
height: int,
codec: str = 'mp4v'
):
"""
Initialize video writer.
Args:
output_path: Path to save the video
fps: Frame rate
width: Frame width in pixels
height: Frame height in pixels
codec: Video codec fourcc code
"""
self.output_path = output_path
self.fps = fps
self.width = width
self.height = height
# Create output directory if it doesn't exist
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
# Initialize video writer
fourcc = cv2.VideoWriter_fourcc(*codec)
self.writer = cv2.VideoWriter(
output_path,
fourcc,
fps,
(width, height)
)
if not self.writer.isOpened():
raise RuntimeError(f"Failed to create video writer: {output_path}")
def __enter__(self):
"""Context manager entry."""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Context manager exit - release video writer."""
self.writer.release()
def write_frame(self, frame: np.ndarray):
"""
Write a single frame to the video.
Args:
frame: Frame array in BGR format
"""
# Ensure frame has correct dimensions
if frame.shape[1] != self.width or frame.shape[0] != self.height:
frame = cv2.resize(frame, (self.width, self.height))
self.writer.write(frame)
def export_trajectory_csv(
trajectory: List[Tuple[float, float, float, float, int]],
fps: float,
output_path: str
) -> bool:
"""
Export trajectory data to CSV file.
Args:
trajectory: List of (x, y, vx, vy, frame_num) tuples
fps: Video frame rate
output_path: Path to save CSV file
Returns:
True if successful, False otherwise
"""
try:
# Create output directory if needed
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
with open(output_path, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
# Write header
writer.writerow([
'frame',
'timestamp_sec',
'x_pixels',
'y_pixels',
'velocity_x_px_per_sec',
'velocity_y_px_per_sec',
'speed_px_per_sec'
])
# Write data rows
for x, y, vx, vy, frame_num in trajectory:
timestamp = frame_num / fps
speed = np.sqrt(vx**2 + vy**2) / (1.0 / fps)
writer.writerow([
frame_num,
f"{timestamp:.3f}",
f"{x:.2f}",
f"{y:.2f}",
f"{vx / (1.0 / fps):.2f}",
f"{vy / (1.0 / fps):.2f}",
f"{speed:.2f}"
])
return True
except Exception as e:
print(f"Error exporting CSV: {str(e)}")
return False
def get_video_info(video_path: str) -> Optional[dict]:
"""
Get basic information about a video file.
Args:
video_path: Path to video file
Returns:
Dictionary with video properties or None if failed
"""
try:
with VideoReader(video_path) as reader:
return reader.get_properties()
except Exception as e:
print(f"Error reading video info: {str(e)}")
return None
def validate_video_file(video_path: str) -> Tuple[bool, str]:
"""
Validate that a video file exists and can be opened.
Args:
video_path: Path to video file
Returns:
Tuple of (is_valid, error_message)
"""
if not video_path:
return False, "No video path provided"
path = Path(video_path)
if not path.exists():
return False, f"Video file not found: {video_path}"
if not path.is_file():
return False, f"Path is not a file: {video_path}"
# Try to open the video
try:
with VideoReader(video_path) as reader:
props = reader.get_properties()
if props['frame_count'] == 0:
return False, "Video has no frames"
if props['fps'] <= 0:
return False, "Invalid video frame rate"
return True, "Valid video file"
except Exception as e:
return False, f"Failed to open video: {str(e)}"
def create_output_directory(output_dir: str = "output") -> Path:
"""
Create output directory if it doesn't exist.
Args:
output_dir: Directory name/path
Returns:
Path object for the output directory
"""
output_path = Path(output_dir)
output_path.mkdir(parents=True, exist_ok=True)
return output_path
|