Overview
The Smart Cropping system automatically converts horizontal videos to vertical 9:16 format optimized for mobile platforms. It uses two distinct algorithms:- Face Detection Mode: Static face-centered crop for videos with visible faces
- Motion Tracking Mode: Dynamic tracking for screen recordings and faceless content
How It Works
Thecrop_to_vertical function in Components/FaceCrop.py:7 analyzes the first 30 frames to determine the optimal cropping strategy:
Initial Analysis
Scans the first 30 frames using Haar Cascade face detection to identify if faces are present.
Mode Selection
- Faces detected: Static face-centered crop
- No faces detected: Motion-tracked crop for screen recordings
Function Signature
Components/FaceCrop.py
Face Detection Algorithm
Haar Cascade Configuration
The system uses OpenCV’s Haar Cascade classifier for face detection:Components/FaceCrop.py:9
Detection Parameters
Components/FaceCrop.py:40
Image pyramid scale factor. Lower values increase detection sensitivity but may cause false positives.
Minimum neighbors required for valid detection. Higher values reduce false positives but may miss faces.
Minimum face size in pixels. Smaller values detect distant faces but increase false positives.
Face Position Calculation
When faces are detected, the algorithm calculates a static crop position:Components/FaceCrop.py:41-56
Median Filtering: Uses the median face position across 30 frames (not mean) to avoid outliers from false detections.
Motion Tracking Algorithm
When no faces are detected, the system uses optical flow-based motion tracking:Scaling Configuration
Components/FaceCrop.py:69-82
Target width for screen recording display. 0.67 shows approximately two-thirds of the original width.
Update Frequency
Motion tracking updates are rate-limited for smooth transitions:Components/FaceCrop.py:99-101
Optical Flow Calculation
The Farneback optical flow algorithm tracks motion between frames:Components/FaceCrop.py:117-119
Farneback Parameters Explained
Farneback Parameters Explained
| Parameter | Value | Description |
|---|---|---|
pyr_scale | 0.5 | Pyramid scale (0.5 = 2-level pyramid) |
levels | 3 | Number of pyramid levels |
winsize | 15 | Averaging window size |
iterations | 3 | Iterations at each pyramid level |
poly_n | 5 | Neighborhood size for polynomial expansion |
poly_sigma | 1.2 | Standard deviation for Gaussian smoothing |
flags | 0 | Operation flags |
Motion Filtering
Only significant motion affects crop position:Components/FaceCrop.py:120-131
Minimum pixel movement to be considered significant motion. Lower values make tracking more sensitive.
Smoothing Parameters
Smoothing prevents abrupt jumps in crop position:Components/FaceCrop.py:133-136
Exponential smoothing weight. 0.90 means 90% previous position, 10% new target. Higher values = smoother but slower response.
Exponential Smoothing: This creates a natural “camera follow” effect similar to professional video editing, avoiding jarring jumps.
Aspect Ratio Calculation
The output dimensions maintain source video quality:Components/FaceCrop.py:21-26
Example Dimensions
| Source Resolution | Output Resolution | Aspect Ratio |
|---|---|---|
| 1920×1080 (1080p) | 607×1080 | 9:16 |
| 1280×720 (720p) | 405×720 | 9:16 |
| 854×480 (480p) | 270×480 | 9:16 |
Video Codec Configuration
The output video uses platform-specific codecs:Components/FaceCrop.py:86-89
Performance Considerations
Processing Speed
- Face Detection Mode: ~50-100 frames per second (static crop, minimal overhead)
- Motion Tracking Mode: ~10-30 frames per second (optical flow computation)
Resource Usage
Components/FaceCrop.py:174-175
Output Quality
The final video is combined with audio using MoviePy:Components/FaceCrop.py:194
H.264 video codec for wide compatibility.
AAC audio codec for mobile compatibility.
Encoding speed preset. Options: ultrafast, fast, medium, slow, veryslow. Slower = better compression.
Target video bitrate. 3000k provides good quality for 1080p vertical video.
Customizing Crop Behavior
Adjust Face Detection Sensitivity
Adjust Face Detection Sensitivity
Edit
Components/FaceCrop.py:40:Change Motion Tracking Update Rate
Change Motion Tracking Update Rate
Edit
Components/FaceCrop.py:99:Modify Smoothing Strength
Modify Smoothing Strength
Edit
Components/FaceCrop.py:136: