Video Analysis for Drone Surveillance
Video analysis transforms sequential drone footage into temporal intelligence—recognizing actions, detecting anomalies, and understanding events as they unfold. This tutorial covers the architectures for comprehensive aerial video understanding.
Video Analysis Pipeline
From frame extraction to temporal reasoning, video analysis builds understanding across time.
**Real-world analogy:** Video analysis is like watching a movie instead of looking at a single photo. A photo shows what's happening now, but video shows the story—how things change, what actions occur, and what events unfold over time.
## Action Recognition
```python
from collections import deque
from dataclasses import dataclass
from typing import List, Dict
@dataclass
class ActionPrediction:
"""Action recognition prediction."""
action_name: str
confidence: float
start_frame: int
end_frame: int
class VideoActionRecognizer:
"""Recognize actions from drone video streams."""
ACTIONS = {
'walking': 0,
'running': 1,
'standing': 2,
'sitting': 3,
'fighting': 4,
'falling': 5,
'entering_vehicle': 6,
'exiting_vehicle': 7,
'carrying_object': 8,
'waving': 9,
}
def __init__(self, num_frames=16, img_size=(112, 112)):
self.num_frames = num_frames
self.img_size = img_size
# Simulated model weights
self.spatial_weights = np.random.randn(512, 2048) * 0.01
self.temporal_weights = np.random.randn(len(self.ACTIONS), 512) * 0.01
# Frame buffer
self.frame_buffer = deque(maxlen=num_frames)
def extract_spatial_features(self, frame):
"""Extract spatial features from a single frame (simulated CNN)."""
# In production, use ResNet3D, I3D, or similar
features = np.random.randn(2048)
return features / np.linalg.norm(features)
def extract_temporal_features(self, frame_sequence):
"""Extract temporal features from frame sequence."""
# Simulate optical flow or temporal convolution
if len(frame_sequence) < 2:
return np.zeros(1024)
# Compute frame differences
diffs = []
for i in range(1, len(frame_sequence)):
diff = np.mean(np.abs(
frame_sequence[i].astype(float) -
frame_sequence[i-1].astype(float)
))
diffs.append(diff)
# Temporal features
features = np.array(diffs + [np.mean(diffs), np.std(diffs)])
features = np.pad(features, (0, 1024 - len(features)))
return features[:1024]
def classify_action(self, spatial_features, temporal_features):
"""Classify action from combined features."""
# Combine features
combined = np.concatenate([spatial_features, temporal_features[:1024]])
# Simulated classification
logits = np.random.randn(len(self.ACTIONS))
probs = np.exp(logits) / np.sum(np.exp(logits))
action_idx = np.argmax(probs)
action_name = list(self.ACTIONS.keys())[action_idx]
confidence = probs[action_idx]
return action_name, confidence
def process_video_clip(self, frames):
"""Process a video clip for action recognition."""
if len(frames) < self.num_frames:
# Pad with last frame
while len(frames) < self.num_frames:
frames.append(frames[-1])
# Sample frames
indices = np.linspace(0, len(frames) - 1, self.num_frames, dtype=int)
sampled_frames = [frames[i] for i in indices]
# Extract spatial features from each frame
spatial_features = []
for frame in sampled_frames:
feat = self.extract_spatial_features(frame)
spatial_features.append(feat)
# Average spatial features
avg_spatial = np.mean(spatial_features, axis=0)
# Extract temporal features
temporal_features = self.extract_temporal_features(sampled_frames)
# Classify action
action, confidence = self.classify_action(avg_spatial, temporal_features)
return ActionPrediction(
action_name=action,
confidence=confidence,
start_frame=0,
end_frame=len(frames) - 1
)
def process_stream(self, frame):
"""Process streaming video frame."""
self.frame_buffer.append(frame)
if len(self.frame_buffer) >= self.num_frames:
prediction = self.process_video_clip(list(self.frame_buffer))
return prediction
return None
# Example: Action recognition from drone video
np.random.seed(42)
recognizer = VideoActionRecognizer(num_frames=16)
print("Video Action Recognition for Drones")
print("=" * 50)
# Simulate video stream
print("\nProcessing simulated video stream:")
for frame_idx in range(30):
# Create frame with simulated motion
frame = np.random.randint(0, 255, (240, 320, 3), dtype=np.uint8)
# Add motion pattern
if 10 <= frame_idx <= 20:
# Simulate walking motion
frame[100:150, 100:200] = [100, 150, 100]
prediction = recognizer.process_stream(frame)
if prediction and frame_idx >= 15:
print(f" Frame {frame_idx}: {prediction.action_name} "
f"({prediction.confidence:.1%})")
print("\nAction recognition complete!")
Anomaly Detection
Event Summarization
Hands-On Project: Complete Video Surveillance System
Build a complete video surveillance system for drone monitoring.
Key Takeaways
- Action recognition identifies human activities in real-time
- Anomaly detection flags unusual behavior for security alerts
- Event summarization creates concise video overviews
- Zone monitoring tracks occupancy and restricted access
- Trajectory analysis follows individual movements over time
Course Complete
Congratulations on completing the AI Fundamentals for Drones course! You've learned:
- Machine learning and deep learning fundamentals
- Computer vision and image processing
- Object detection and tracking
- Semantic and instance segmentation
- Image classification with transfer learning
- Pose estimation and gesture recognition
- Face detection and recognition
- Video analysis and event understanding
These skills form the foundation for building intelligent drone systems that can perceive, understand, and interact with their environment autonomously.