Spaces:
Sleeping
Sleeping
Update core/styles.py
Browse files- core/styles.py +242 -132
core/styles.py
CHANGED
|
@@ -2,12 +2,22 @@
|
|
| 2 |
Video Styles — YouTube Shorts Production Engine
|
| 3 |
SplitVertical & SplitHorizontal rebuilt with seamless gradient blending.
|
| 4 |
All class/method names kept identical for drop-in integration.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"""
|
|
|
|
| 6 |
from abc import ABC, abstractmethod
|
| 7 |
import os
|
| 8 |
import cv2
|
| 9 |
import numpy as np
|
| 10 |
import moviepy.editor as mpe
|
|
|
|
|
|
|
| 11 |
from .config import Config
|
| 12 |
from .logger import Logger
|
| 13 |
from .subtitle_manager import SubtitleManager
|
|
@@ -16,26 +26,16 @@ logger = Logger.get_logger(__name__)
|
|
| 16 |
|
| 17 |
|
| 18 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 19 |
-
# Gradient Mask Helpers
|
| 20 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 21 |
|
| 22 |
def _linear_gradient(length: int, fade_from_zero: bool) -> np.ndarray:
|
| 23 |
-
"""
|
| 24 |
-
Returns a 1-D float32 array [0..1] of given length.
|
| 25 |
-
fade_from_zero=True → 0 → 1 (clip fades IN at this edge)
|
| 26 |
-
fade_from_zero=False → 1 → 0 (clip fades OUT at this edge)
|
| 27 |
-
"""
|
| 28 |
arr = np.linspace(0.0, 1.0, length, dtype=np.float32)
|
| 29 |
return arr if fade_from_zero else arr[::-1]
|
| 30 |
|
| 31 |
|
| 32 |
def _make_vertical_mask(clip_w: int, clip_h: int,
|
| 33 |
blend_top: int = 0, blend_bottom: int = 0) -> np.ndarray:
|
| 34 |
-
"""
|
| 35 |
-
Float32 mask (clip_h × clip_w) in [0,1].
|
| 36 |
-
blend_top → pixels from top that fade in (0→1)
|
| 37 |
-
blend_bottom → pixels from bottom that fade out (1→0)
|
| 38 |
-
"""
|
| 39 |
mask = np.ones((clip_h, clip_w), dtype=np.float32)
|
| 40 |
if blend_top > 0:
|
| 41 |
grad = _linear_gradient(blend_top, fade_from_zero=True)
|
|
@@ -48,11 +48,6 @@ def _make_vertical_mask(clip_w: int, clip_h: int,
|
|
| 48 |
|
| 49 |
def _make_horizontal_mask(clip_w: int, clip_h: int,
|
| 50 |
blend_left: int = 0, blend_right: int = 0) -> np.ndarray:
|
| 51 |
-
"""
|
| 52 |
-
Float32 mask (clip_h × clip_w) in [0,1].
|
| 53 |
-
blend_left → pixels from left that fade in (0→1)
|
| 54 |
-
blend_right → pixels from right that fade out (1→0)
|
| 55 |
-
"""
|
| 56 |
mask = np.ones((clip_h, clip_w), dtype=np.float32)
|
| 57 |
if blend_left > 0:
|
| 58 |
grad = _linear_gradient(blend_left, fade_from_zero=True)
|
|
@@ -64,18 +59,15 @@ def _make_horizontal_mask(clip_w: int, clip_h: int,
|
|
| 64 |
|
| 65 |
|
| 66 |
def _apply_mask(clip: mpe.VideoClip, mask_array: np.ndarray) -> mpe.VideoClip:
|
| 67 |
-
"""Attach a static float32 numpy mask to a video clip."""
|
| 68 |
mask_clip = mpe.ImageClip(mask_array, ismask=True, duration=clip.duration)
|
| 69 |
return clip.set_mask(mask_clip)
|
| 70 |
|
| 71 |
|
| 72 |
def _fit_to_width(clip: mpe.VideoClip, target_w: int) -> mpe.VideoClip:
|
| 73 |
-
"""Resize clip so width == target_w, keeping aspect ratio."""
|
| 74 |
return clip.resize(width=target_w)
|
| 75 |
|
| 76 |
|
| 77 |
def _fit_to_height(clip: mpe.VideoClip, target_h: int) -> mpe.VideoClip:
|
| 78 |
-
"""Resize clip so height == target_h, keeping aspect ratio."""
|
| 79 |
return clip.resize(height=target_h)
|
| 80 |
|
| 81 |
|
|
@@ -86,68 +78,225 @@ def _loop_or_cut(clip: mpe.VideoClip, duration: float) -> mpe.VideoClip:
|
|
| 86 |
|
| 87 |
|
| 88 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 89 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 91 |
|
| 92 |
class SmartFaceCropper:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
def __init__(self, output_size=(1080, 1920)):
|
| 94 |
-
self.output_size
|
| 95 |
-
self.
|
|
|
|
|
|
|
|
|
|
| 96 |
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
|
| 97 |
)
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
self.
|
| 101 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
def get_crop_coordinates(self, frame):
|
|
|
|
| 104 |
h, w = frame.shape[:2]
|
| 105 |
-
|
| 106 |
-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 107 |
-
small = cv2.resize(gray, (0, 0), fx=0.5, fy=0.5)
|
| 108 |
-
faces = self.face_cascade.detectMultiScale(small, 1.1, 8, minSize=(50, 50))
|
| 109 |
-
|
| 110 |
-
if len(faces) > 0:
|
| 111 |
-
faces = sorted(faces, key=lambda f: f[2] * f[3], reverse=True)
|
| 112 |
-
fx, fy, fw, fh = [v * 2 for v in faces[0]]
|
| 113 |
-
current_center_x = fx + fw // 2
|
| 114 |
-
self.last_coords = (fx, fy, fw, fh)
|
| 115 |
-
else:
|
| 116 |
-
current_center_x = w // 2 if self.smoothed_x is None else self.smoothed_x
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
else:
|
| 121 |
-
self.
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
)
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
def apply_to_clip(self, clip):
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
def filter_frame(get_frame, t):
|
| 134 |
frame = get_frame(t)
|
| 135 |
-
self.
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
else:
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
|
| 149 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 150 |
-
# Base Style
|
| 151 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 152 |
|
| 153 |
class BaseStyle(ABC):
|
|
@@ -185,7 +334,7 @@ class BaseStyle(ABC):
|
|
| 185 |
language=language, caption_mode=caption_mode,
|
| 186 |
)
|
| 187 |
|
| 188 |
-
def _create_caption_clips(self, transcript_data, language=None,
|
| 189 |
caption_mode="sentence", caption_style="classic"):
|
| 190 |
return SubtitleManager.create_caption_clips(
|
| 191 |
transcript_data, size=self.output_size,
|
|
@@ -195,7 +344,7 @@ class BaseStyle(ABC):
|
|
| 195 |
|
| 196 |
|
| 197 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 198 |
-
# Cinematic Style
|
| 199 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 200 |
|
| 201 |
class CinematicStyle(BaseStyle):
|
|
@@ -230,7 +379,7 @@ class CinematicStyle(BaseStyle):
|
|
| 230 |
|
| 231 |
|
| 232 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 233 |
-
# Cinematic Blur Style
|
| 234 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 235 |
|
| 236 |
class CinematicBlurStyle(BaseStyle):
|
|
@@ -257,115 +406,68 @@ class CinematicBlurStyle(BaseStyle):
|
|
| 257 |
|
| 258 |
|
| 259 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 260 |
-
# Split Vertical (
|
| 261 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 262 |
|
| 263 |
class SplitVerticalStyle(BaseStyle):
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
Layout
|
| 268 |
-
──────
|
| 269 |
-
• Top segment : 58 % of canvas height → ~1114 px
|
| 270 |
-
• Bottom segment: fills the rest → ~926 px
|
| 271 |
-
• Blend zone : 120 px overlap where the two clips cross-fade via
|
| 272 |
-
gradient masks — no hard dividing line visible.
|
| 273 |
-
|
| 274 |
-
The gradient is very subtle (linear alpha), so it doesn't destroy
|
| 275 |
-
content near the seam, it just dissolves one clip into the other.
|
| 276 |
-
"""
|
| 277 |
-
|
| 278 |
-
SPLIT_RATIO : float = 0.58 # top segment fraction of total height
|
| 279 |
-
BLEND_PX : int = 120 # overlap / blend zone height in pixels
|
| 280 |
|
| 281 |
def apply(self, clip, playground_path=None, **kwargs):
|
| 282 |
-
W, H = self.output_size
|
| 283 |
blend = self.BLEND_PX
|
| 284 |
-
h_top_seg = int(H * self.SPLIT_RATIO)
|
| 285 |
-
h_bot_seg = H - h_top_seg + blend
|
| 286 |
|
| 287 |
-
# ── Prepare main clip for top segment ───────────────────────────────
|
| 288 |
top_clip = _fit_to_width(clip, W)
|
| 289 |
-
|
| 290 |
-
# Crop to the top portion we need (+ blend zone so gradient has room)
|
| 291 |
-
top_h = min(top_clip.h, h_top_seg + blend // 2)
|
| 292 |
top_clip = top_clip.crop(x1=0, y1=0, x2=W, y2=top_h).resize((W, h_top_seg))
|
| 293 |
-
|
| 294 |
-
# Gradient: fade out the bottom `blend` rows → seamless merge
|
| 295 |
top_mask = _make_vertical_mask(W, h_top_seg, blend_bottom=blend)
|
| 296 |
top_clip = _apply_mask(top_clip, top_mask).set_position((0, 0))
|
| 297 |
|
| 298 |
-
# ── Prepare playground / fallback clip for bottom segment ────────────
|
| 299 |
if playground_path and os.path.exists(playground_path):
|
| 300 |
bot_src = _loop_or_cut(
|
| 301 |
mpe.VideoFileClip(playground_path).without_audio(), clip.duration
|
| 302 |
)
|
| 303 |
else:
|
| 304 |
-
# Fallback: mirror/tint of the same source
|
| 305 |
bot_src = clip.set_opacity(0.85)
|
| 306 |
|
| 307 |
bot_clip = _fit_to_width(bot_src, W)
|
| 308 |
-
|
| 309 |
-
# We want the middle/lower portion of the source for the bottom panel
|
| 310 |
if bot_clip.h > h_bot_seg:
|
| 311 |
-
y_start
|
| 312 |
-
bot_clip = bot_clip.crop(x1=0, y1=y_start,
|
| 313 |
-
x2=W, y2=bot_clip.h)
|
| 314 |
|
| 315 |
bot_clip = bot_clip.resize((W, h_bot_seg))
|
| 316 |
-
|
| 317 |
-
# Gradient: fade in the top `blend` rows → seamless merge
|
| 318 |
bot_mask = _make_vertical_mask(W, h_bot_seg, blend_top=blend)
|
| 319 |
-
bot_y = h_top_seg - blend
|
| 320 |
bot_clip = _apply_mask(bot_clip, bot_mask).set_position((0, bot_y))
|
| 321 |
|
| 322 |
return mpe.CompositeVideoClip([bot_clip, top_clip], size=self.output_size)
|
| 323 |
|
| 324 |
|
| 325 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 326 |
-
# Split Horizontal (
|
| 327 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 328 |
|
| 329 |
class SplitHorizontalStyle(BaseStyle):
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
Layout
|
| 334 |
-
──────
|
| 335 |
-
• Each panel fills the full 1920 px height.
|
| 336 |
-
• Left panel: 52 % of canvas width → ~562 px
|
| 337 |
-
• Right panel: fills the rest → ~518 px
|
| 338 |
-
• Blend zone : 80 px overlap with cross-fade gradient masks.
|
| 339 |
-
|
| 340 |
-
Both panels are individually cropped to portrait aspect ratio
|
| 341 |
-
(each showing a 540-wide slice of a 1080-wide source),
|
| 342 |
-
then blended at the seam — no visible dividing line.
|
| 343 |
-
"""
|
| 344 |
-
|
| 345 |
-
SPLIT_RATIO : float = 0.52 # left panel fraction of total width
|
| 346 |
-
BLEND_PX : int = 80 # horizontal overlap / blend zone
|
| 347 |
|
| 348 |
def apply(self, clip, playground_path=None, **kwargs):
|
| 349 |
-
W, H
|
| 350 |
-
blend
|
| 351 |
-
w_left_seg
|
| 352 |
-
w_right_seg = W - w_left_seg + blend
|
| 353 |
|
| 354 |
-
# ── Left panel from main clip ────────────────────────────────────────
|
| 355 |
left_src = _fit_to_height(clip, H)
|
| 356 |
lw = left_src.w
|
| 357 |
-
|
| 358 |
-
# Crop the left portion (slightly more than half for a natural look)
|
| 359 |
crop_w_l = min(lw, w_left_seg + blend)
|
| 360 |
left_clip = left_src.crop(x1=max(0, lw // 2 - crop_w_l),
|
| 361 |
y1=0, x2=lw // 2, y2=H)
|
| 362 |
left_clip = left_clip.resize((w_left_seg, H))
|
| 363 |
-
|
| 364 |
-
# Gradient: fade out rightmost `blend` columns
|
| 365 |
left_mask = _make_horizontal_mask(w_left_seg, H, blend_right=blend)
|
| 366 |
left_clip = _apply_mask(left_clip, left_mask).set_position((0, 0))
|
| 367 |
|
| 368 |
-
# ── Right panel from playground or fallback ───────────────────────────
|
| 369 |
if playground_path and os.path.exists(playground_path):
|
| 370 |
right_src = _loop_or_cut(
|
| 371 |
mpe.VideoFileClip(playground_path).without_audio(), clip.duration
|
|
@@ -375,27 +477,35 @@ class SplitHorizontalStyle(BaseStyle):
|
|
| 375 |
|
| 376 |
right_full = _fit_to_height(right_src, H)
|
| 377 |
rw = right_full.w
|
| 378 |
-
|
| 379 |
-
# Crop the right portion of the source
|
| 380 |
crop_w_r = min(rw, w_right_seg + blend)
|
| 381 |
right_clip = right_full.crop(x1=rw // 2, y1=0,
|
| 382 |
x2=rw // 2 + crop_w_r, y2=H)
|
| 383 |
right_clip = right_clip.resize((w_right_seg, H))
|
| 384 |
-
|
| 385 |
-
# Gradient: fade in leftmost `blend` columns
|
| 386 |
right_mask = _make_horizontal_mask(w_right_seg, H, blend_left=blend)
|
| 387 |
-
right_x = w_left_seg - blend
|
| 388 |
right_clip = _apply_mask(right_clip, right_mask).set_position((right_x, 0))
|
| 389 |
|
| 390 |
return mpe.CompositeVideoClip([right_clip, left_clip], size=self.output_size)
|
| 391 |
|
| 392 |
|
| 393 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 394 |
-
# Vertical Full Style
|
| 395 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 396 |
|
| 397 |
class VerticalFullStyle(BaseStyle):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
def apply(self, clip, **kwargs):
|
|
|
|
| 399 |
cropper = SmartFaceCropper(output_size=self.output_size)
|
| 400 |
return cropper.apply_to_clip(clip)
|
| 401 |
|
|
|
|
| 2 |
Video Styles — YouTube Shorts Production Engine
|
| 3 |
SplitVertical & SplitHorizontal rebuilt with seamless gradient blending.
|
| 4 |
All class/method names kept identical for drop-in integration.
|
| 5 |
+
|
| 6 |
+
VerticalFullStyle — v2 (high-quality, stabilized):
|
| 7 |
+
• INTER_LANCZOS4 for sharp upscaling
|
| 8 |
+
• Kalman-like dual smoothing (position + velocity) → no shakiness
|
| 9 |
+
• DNN face detector (res10 SSD) with Haar fallback
|
| 10 |
+
• Temporal face confidence gating (ignores single-frame false-negatives)
|
| 11 |
+
• apply_to='video' on clip.fl() to skip audio re-encoding artifacts
|
| 12 |
"""
|
| 13 |
+
|
| 14 |
from abc import ABC, abstractmethod
|
| 15 |
import os
|
| 16 |
import cv2
|
| 17 |
import numpy as np
|
| 18 |
import moviepy.editor as mpe
|
| 19 |
+
from collections import deque
|
| 20 |
+
|
| 21 |
from .config import Config
|
| 22 |
from .logger import Logger
|
| 23 |
from .subtitle_manager import SubtitleManager
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 29 |
+
# Gradient Mask Helpers (unchanged)
|
| 30 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 31 |
|
| 32 |
def _linear_gradient(length: int, fade_from_zero: bool) -> np.ndarray:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
arr = np.linspace(0.0, 1.0, length, dtype=np.float32)
|
| 34 |
return arr if fade_from_zero else arr[::-1]
|
| 35 |
|
| 36 |
|
| 37 |
def _make_vertical_mask(clip_w: int, clip_h: int,
|
| 38 |
blend_top: int = 0, blend_bottom: int = 0) -> np.ndarray:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
mask = np.ones((clip_h, clip_w), dtype=np.float32)
|
| 40 |
if blend_top > 0:
|
| 41 |
grad = _linear_gradient(blend_top, fade_from_zero=True)
|
|
|
|
| 48 |
|
| 49 |
def _make_horizontal_mask(clip_w: int, clip_h: int,
|
| 50 |
blend_left: int = 0, blend_right: int = 0) -> np.ndarray:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
mask = np.ones((clip_h, clip_w), dtype=np.float32)
|
| 52 |
if blend_left > 0:
|
| 53 |
grad = _linear_gradient(blend_left, fade_from_zero=True)
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
def _apply_mask(clip: mpe.VideoClip, mask_array: np.ndarray) -> mpe.VideoClip:
|
|
|
|
| 62 |
mask_clip = mpe.ImageClip(mask_array, ismask=True, duration=clip.duration)
|
| 63 |
return clip.set_mask(mask_clip)
|
| 64 |
|
| 65 |
|
| 66 |
def _fit_to_width(clip: mpe.VideoClip, target_w: int) -> mpe.VideoClip:
|
|
|
|
| 67 |
return clip.resize(width=target_w)
|
| 68 |
|
| 69 |
|
| 70 |
def _fit_to_height(clip: mpe.VideoClip, target_h: int) -> mpe.VideoClip:
|
|
|
|
| 71 |
return clip.resize(height=target_h)
|
| 72 |
|
| 73 |
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 81 |
+
# DNN Face Detector loader (singleton, lazy)
|
| 82 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 83 |
+
|
| 84 |
+
_DNN_NET = None # shared across all SmartFaceCropper instances
|
| 85 |
+
|
| 86 |
+
def _get_dnn_net():
|
| 87 |
+
"""
|
| 88 |
+
Lazy-load OpenCV's res10_300x300_ssd face detector.
|
| 89 |
+
Falls back to None if model files are not found — Haar is used instead.
|
| 90 |
+
"""
|
| 91 |
+
global _DNN_NET
|
| 92 |
+
if _DNN_NET is not None:
|
| 93 |
+
return _DNN_NET
|
| 94 |
+
|
| 95 |
+
# Common installation paths (opencv-python-headless bundles these)
|
| 96 |
+
base_candidates = [
|
| 97 |
+
cv2.data.haarcascades, # same folder as haarcascades
|
| 98 |
+
os.path.join(os.path.dirname(cv2.__file__), "data"),
|
| 99 |
+
"/usr/share/opencv4/",
|
| 100 |
+
"/usr/local/share/opencv4/",
|
| 101 |
+
]
|
| 102 |
+
proto_name = "deploy.prototxt"
|
| 103 |
+
model_name = "res10_300x300_ssd_iter_140000_fp16.caffemodel"
|
| 104 |
+
|
| 105 |
+
for base in base_candidates:
|
| 106 |
+
proto = os.path.join(base, proto_name)
|
| 107 |
+
model = os.path.join(base, model_name)
|
| 108 |
+
if os.path.exists(proto) and os.path.exists(model):
|
| 109 |
+
try:
|
| 110 |
+
_DNN_NET = cv2.dnn.readNetFromCaffe(proto, model)
|
| 111 |
+
logger.info("DNN face detector loaded from %s", base)
|
| 112 |
+
return _DNN_NET
|
| 113 |
+
except Exception as e:
|
| 114 |
+
logger.warning("DNN load failed: %s", e)
|
| 115 |
+
|
| 116 |
+
logger.warning("DNN face model not found — falling back to Haar cascade")
|
| 117 |
+
_DNN_NET = False # sentinel so we don't retry every frame
|
| 118 |
+
return None
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 122 |
+
# Smart Face Cropper — v2 (stabilized, high-quality)
|
| 123 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 124 |
|
| 125 |
class SmartFaceCropper:
|
| 126 |
+
"""
|
| 127 |
+
Portrait-mode smart crop with face tracking.
|
| 128 |
+
|
| 129 |
+
Improvements over v1
|
| 130 |
+
────────────────────
|
| 131 |
+
1. INTER_LANCZOS4 — sharpest upscaling interpolation available in OpenCV
|
| 132 |
+
2. Velocity smoothing — exponential smoothing on both position AND velocity
|
| 133 |
+
eliminates the micro-jitter seen with plain EMA
|
| 134 |
+
3. DNN face detector — far more accurate than Haar; auto-falls back to Haar
|
| 135 |
+
4. Confidence gating — ignores detections below threshold + temporal
|
| 136 |
+
buffer prevents single-frame dropouts from snapping
|
| 137 |
+
5. apply_to='video' — tells MoviePy to skip audio channels → no artifacts
|
| 138 |
+
6. Parametric tuning — all magic numbers as named class attributes
|
| 139 |
+
"""
|
| 140 |
+
|
| 141 |
+
# ── Tunable parameters ────────────────────────────────────────────────
|
| 142 |
+
FRAME_SKIP : int = 3 # re-detect face every N frames
|
| 143 |
+
POS_SMOOTH : float = 0.25 # EMA weight for position (higher = faster)
|
| 144 |
+
VEL_SMOOTH : float = 0.15 # EMA weight for velocity (damps oscillation)
|
| 145 |
+
DNN_CONFIDENCE : float = 0.65 # min DNN detection confidence [0-1]
|
| 146 |
+
MISS_TOLERANCE : int = 12 # frames before abandoning last known face
|
| 147 |
+
# ─────────────────────────────────────────────────────────────────────
|
| 148 |
+
|
| 149 |
def __init__(self, output_size=(1080, 1920)):
|
| 150 |
+
self.output_size = output_size # (width, height)
|
| 151 |
+
self.out_w, self.out_h = output_size
|
| 152 |
+
|
| 153 |
+
# Haar fallback
|
| 154 |
+
self._haar = cv2.CascadeClassifier(
|
| 155 |
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
|
| 156 |
)
|
| 157 |
+
|
| 158 |
+
# State
|
| 159 |
+
self._smoothed_x : float | None = None # smoothed crop-center X
|
| 160 |
+
self._velocity_x : float = 0.0 # running velocity estimate
|
| 161 |
+
self._last_face_x : float | None = None # last confirmed face centre
|
| 162 |
+
self._miss_count : int = 0 # consecutive no-detection frames
|
| 163 |
+
self._frame_idx : int = 0 # global frame counter
|
| 164 |
+
|
| 165 |
+
# ── Public API (identical to v1) ─────────────────────────────────────
|
| 166 |
|
| 167 |
def get_crop_coordinates(self, frame):
|
| 168 |
+
"""Return (left, top, right, bottom) crop box for one frame."""
|
| 169 |
h, w = frame.shape[:2]
|
| 170 |
+
crop_w = int(h * self.out_w / self.out_h) # target crop width
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
+
detected_x = self._detect_face_center(frame, w)
|
| 173 |
+
|
| 174 |
+
if detected_x is not None:
|
| 175 |
+
self._last_face_x = detected_x
|
| 176 |
+
self._miss_count = 0
|
| 177 |
+
target_x = detected_x
|
| 178 |
else:
|
| 179 |
+
self._miss_count += 1
|
| 180 |
+
if self._miss_count <= self.MISS_TOLERANCE and self._last_face_x is not None:
|
| 181 |
+
# Hold last known position
|
| 182 |
+
target_x = self._last_face_x
|
| 183 |
+
else:
|
| 184 |
+
# Give up — centre of frame
|
| 185 |
+
target_x = w // 2
|
| 186 |
+
|
| 187 |
+
# ── Velocity-based smoothing ──────────────────────────────────────
|
| 188 |
+
if self._smoothed_x is None:
|
| 189 |
+
# Cold start: snap immediately
|
| 190 |
+
self._smoothed_x = float(target_x)
|
| 191 |
+
self._velocity_x = 0.0
|
| 192 |
+
else:
|
| 193 |
+
# Desired displacement this step
|
| 194 |
+
raw_delta = target_x - self._smoothed_x
|
| 195 |
+
|
| 196 |
+
# Smooth the velocity (acts as a low-pass filter on acceleration)
|
| 197 |
+
self._velocity_x = (
|
| 198 |
+
self._velocity_x * (1.0 - self.VEL_SMOOTH)
|
| 199 |
+
+ raw_delta * self.VEL_SMOOTH
|
| 200 |
)
|
| 201 |
|
| 202 |
+
# Advance position with smoothed velocity
|
| 203 |
+
self._smoothed_x += self._velocity_x * self.POS_SMOOTH
|
| 204 |
+
|
| 205 |
+
# ── Compute crop box ─────────────────────────────────────────────
|
| 206 |
+
left = int(self._smoothed_x - crop_w / 2)
|
| 207 |
+
left = max(0, min(left, w - crop_w))
|
| 208 |
+
return left, 0, left + crop_w, h
|
| 209 |
|
| 210 |
+
def apply_to_clip(self, clip: mpe.VideoClip) -> mpe.VideoClip:
|
| 211 |
+
"""Apply portrait-crop + face-tracking to a MoviePy clip."""
|
| 212 |
+
frame_skip = self.FRAME_SKIP
|
| 213 |
+
# Cached crop so non-detection frames reuse last result
|
| 214 |
+
_last_box = [None]
|
| 215 |
|
| 216 |
def filter_frame(get_frame, t):
|
| 217 |
frame = get_frame(t)
|
| 218 |
+
self._frame_idx += 1
|
| 219 |
+
|
| 220 |
+
# Re-detect every FRAME_SKIP frames; otherwise reuse
|
| 221 |
+
if self._frame_idx % frame_skip == 0 or _last_box[0] is None:
|
| 222 |
+
left, top, right, bottom = self.get_crop_coordinates(frame)
|
| 223 |
+
_last_box[0] = (left, top, right, bottom)
|
| 224 |
else:
|
| 225 |
+
# Still advance the smoother with cached position
|
| 226 |
+
left, top, right, bottom = _last_box[0]
|
| 227 |
+
|
| 228 |
+
cropped = frame[top:bottom, left:right]
|
| 229 |
+
|
| 230 |
+
# ── High-quality resize ───────────────────────────────────────
|
| 231 |
+
# INTER_LANCZOS4 is the highest-quality upscaler in OpenCV.
|
| 232 |
+
# It's ~2× slower than INTER_LINEAR but the difference is
|
| 233 |
+
# very visible when upscaling a narrow crop to 1080 px.
|
| 234 |
+
return cv2.resize(
|
| 235 |
+
cropped,
|
| 236 |
+
(self.out_w, self.out_h),
|
| 237 |
+
interpolation=cv2.INTER_LANCZOS4,
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
# apply_to='video' skips the audio pipeline → no re-encoding artifacts
|
| 241 |
+
return clip.fl(filter_frame, apply_to='video')
|
| 242 |
+
|
| 243 |
+
# ── Internal helpers ─────────────────────────────────────────────────
|
| 244 |
+
|
| 245 |
+
def _detect_face_center(self, frame: np.ndarray, frame_w: int) -> float | None:
|
| 246 |
+
"""
|
| 247 |
+
Try DNN detector first; fall back to Haar.
|
| 248 |
+
Returns the X-coordinate of the largest detected face centre, or None.
|
| 249 |
+
"""
|
| 250 |
+
net = _get_dnn_net()
|
| 251 |
+
if net:
|
| 252 |
+
return self._dnn_detect(frame, net)
|
| 253 |
+
return self._haar_detect(frame, frame_w)
|
| 254 |
+
|
| 255 |
+
def _dnn_detect(self, frame: np.ndarray, net) -> float | None:
|
| 256 |
+
h, w = frame.shape[:2]
|
| 257 |
+
# DNN expects 300×300 blob; BGR input
|
| 258 |
+
blob = cv2.dnn.blobFromImage(
|
| 259 |
+
cv2.resize(frame, (300, 300)),
|
| 260 |
+
scalefactor=1.0,
|
| 261 |
+
size=(300, 300),
|
| 262 |
+
mean=(104.0, 177.0, 123.0),
|
| 263 |
+
)
|
| 264 |
+
net.setInput(blob)
|
| 265 |
+
detections = net.forward() # shape: (1, 1, N, 7)
|
| 266 |
+
|
| 267 |
+
best_cx = None
|
| 268 |
+
best_area = 0
|
| 269 |
+
|
| 270 |
+
for i in range(detections.shape[2]):
|
| 271 |
+
confidence = float(detections[0, 0, i, 2])
|
| 272 |
+
if confidence < self.DNN_CONFIDENCE:
|
| 273 |
+
continue
|
| 274 |
+
x1 = int(detections[0, 0, i, 3] * w)
|
| 275 |
+
y1 = int(detections[0, 0, i, 4] * h)
|
| 276 |
+
x2 = int(detections[0, 0, i, 5] * w)
|
| 277 |
+
y2 = int(detections[0, 0, i, 6] * h)
|
| 278 |
+
area = (x2 - x1) * (y2 - y1)
|
| 279 |
+
if area > best_area:
|
| 280 |
+
best_area = area
|
| 281 |
+
best_cx = (x1 + x2) / 2.0
|
| 282 |
+
|
| 283 |
+
return best_cx
|
| 284 |
+
|
| 285 |
+
def _haar_detect(self, frame: np.ndarray, frame_w: int) -> float | None:
|
| 286 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 287 |
+
small = cv2.resize(gray, (0, 0), fx=0.5, fy=0.5)
|
| 288 |
+
faces = self._haar.detectMultiScale(small, 1.1, 8, minSize=(50, 50))
|
| 289 |
|
| 290 |
+
if len(faces) == 0:
|
| 291 |
+
return None
|
| 292 |
+
|
| 293 |
+
faces = sorted(faces, key=lambda f: f[2] * f[3], reverse=True)
|
| 294 |
+
fx, _, fw, _ = [v * 2 for v in faces[0]]
|
| 295 |
+
return float(fx + fw / 2.0)
|
| 296 |
|
| 297 |
|
| 298 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 299 |
+
# Base Style (unchanged)
|
| 300 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 301 |
|
| 302 |
class BaseStyle(ABC):
|
|
|
|
| 334 |
language=language, caption_mode=caption_mode,
|
| 335 |
)
|
| 336 |
|
| 337 |
+
def _create_caption_clips(self, transcript_data, language=None,
|
| 338 |
caption_mode="sentence", caption_style="classic"):
|
| 339 |
return SubtitleManager.create_caption_clips(
|
| 340 |
transcript_data, size=self.output_size,
|
|
|
|
| 344 |
|
| 345 |
|
| 346 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 347 |
+
# Cinematic Style (unchanged)
|
| 348 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 349 |
|
| 350 |
class CinematicStyle(BaseStyle):
|
|
|
|
| 379 |
|
| 380 |
|
| 381 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 382 |
+
# Cinematic Blur Style (unchanged)
|
| 383 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 384 |
|
| 385 |
class CinematicBlurStyle(BaseStyle):
|
|
|
|
| 406 |
|
| 407 |
|
| 408 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 409 |
+
# Split Vertical (unchanged)
|
| 410 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 411 |
|
| 412 |
class SplitVerticalStyle(BaseStyle):
|
| 413 |
+
SPLIT_RATIO : float = 0.58
|
| 414 |
+
BLEND_PX : int = 120
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
|
| 416 |
def apply(self, clip, playground_path=None, **kwargs):
|
| 417 |
+
W, H = self.output_size
|
| 418 |
blend = self.BLEND_PX
|
| 419 |
+
h_top_seg = int(H * self.SPLIT_RATIO)
|
| 420 |
+
h_bot_seg = H - h_top_seg + blend
|
| 421 |
|
|
|
|
| 422 |
top_clip = _fit_to_width(clip, W)
|
| 423 |
+
top_h = min(top_clip.h, h_top_seg + blend // 2)
|
|
|
|
|
|
|
| 424 |
top_clip = top_clip.crop(x1=0, y1=0, x2=W, y2=top_h).resize((W, h_top_seg))
|
|
|
|
|
|
|
| 425 |
top_mask = _make_vertical_mask(W, h_top_seg, blend_bottom=blend)
|
| 426 |
top_clip = _apply_mask(top_clip, top_mask).set_position((0, 0))
|
| 427 |
|
|
|
|
| 428 |
if playground_path and os.path.exists(playground_path):
|
| 429 |
bot_src = _loop_or_cut(
|
| 430 |
mpe.VideoFileClip(playground_path).without_audio(), clip.duration
|
| 431 |
)
|
| 432 |
else:
|
|
|
|
| 433 |
bot_src = clip.set_opacity(0.85)
|
| 434 |
|
| 435 |
bot_clip = _fit_to_width(bot_src, W)
|
|
|
|
|
|
|
| 436 |
if bot_clip.h > h_bot_seg:
|
| 437 |
+
y_start = max(0, bot_clip.h - h_bot_seg)
|
| 438 |
+
bot_clip = bot_clip.crop(x1=0, y1=y_start, x2=W, y2=bot_clip.h)
|
|
|
|
| 439 |
|
| 440 |
bot_clip = bot_clip.resize((W, h_bot_seg))
|
|
|
|
|
|
|
| 441 |
bot_mask = _make_vertical_mask(W, h_bot_seg, blend_top=blend)
|
| 442 |
+
bot_y = h_top_seg - blend
|
| 443 |
bot_clip = _apply_mask(bot_clip, bot_mask).set_position((0, bot_y))
|
| 444 |
|
| 445 |
return mpe.CompositeVideoClip([bot_clip, top_clip], size=self.output_size)
|
| 446 |
|
| 447 |
|
| 448 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 449 |
+
# Split Horizontal (unchanged)
|
| 450 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 451 |
|
| 452 |
class SplitHorizontalStyle(BaseStyle):
|
| 453 |
+
SPLIT_RATIO : float = 0.52
|
| 454 |
+
BLEND_PX : int = 80
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 455 |
|
| 456 |
def apply(self, clip, playground_path=None, **kwargs):
|
| 457 |
+
W, H = self.output_size
|
| 458 |
+
blend = self.BLEND_PX
|
| 459 |
+
w_left_seg = int(W * self.SPLIT_RATIO)
|
| 460 |
+
w_right_seg = W - w_left_seg + blend
|
| 461 |
|
|
|
|
| 462 |
left_src = _fit_to_height(clip, H)
|
| 463 |
lw = left_src.w
|
|
|
|
|
|
|
| 464 |
crop_w_l = min(lw, w_left_seg + blend)
|
| 465 |
left_clip = left_src.crop(x1=max(0, lw // 2 - crop_w_l),
|
| 466 |
y1=0, x2=lw // 2, y2=H)
|
| 467 |
left_clip = left_clip.resize((w_left_seg, H))
|
|
|
|
|
|
|
| 468 |
left_mask = _make_horizontal_mask(w_left_seg, H, blend_right=blend)
|
| 469 |
left_clip = _apply_mask(left_clip, left_mask).set_position((0, 0))
|
| 470 |
|
|
|
|
| 471 |
if playground_path and os.path.exists(playground_path):
|
| 472 |
right_src = _loop_or_cut(
|
| 473 |
mpe.VideoFileClip(playground_path).without_audio(), clip.duration
|
|
|
|
| 477 |
|
| 478 |
right_full = _fit_to_height(right_src, H)
|
| 479 |
rw = right_full.w
|
|
|
|
|
|
|
| 480 |
crop_w_r = min(rw, w_right_seg + blend)
|
| 481 |
right_clip = right_full.crop(x1=rw // 2, y1=0,
|
| 482 |
x2=rw // 2 + crop_w_r, y2=H)
|
| 483 |
right_clip = right_clip.resize((w_right_seg, H))
|
|
|
|
|
|
|
| 484 |
right_mask = _make_horizontal_mask(w_right_seg, H, blend_left=blend)
|
| 485 |
+
right_x = w_left_seg - blend
|
| 486 |
right_clip = _apply_mask(right_clip, right_mask).set_position((right_x, 0))
|
| 487 |
|
| 488 |
return mpe.CompositeVideoClip([right_clip, left_clip], size=self.output_size)
|
| 489 |
|
| 490 |
|
| 491 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 492 |
+
# Vertical Full Style — v2 (drop-in replacement, zero API change)
|
| 493 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 494 |
|
| 495 |
class VerticalFullStyle(BaseStyle):
|
| 496 |
+
"""
|
| 497 |
+
Portrait-mode style using SmartFaceCropper v2.
|
| 498 |
+
|
| 499 |
+
Identical public interface to v1:
|
| 500 |
+
style = VerticalFullStyle()
|
| 501 |
+
result = style.apply(clip)
|
| 502 |
+
result = style.apply_with_captions(clip, transcript_data, ...)
|
| 503 |
+
|
| 504 |
+
All quality and stability improvements are internal to SmartFaceCropper.
|
| 505 |
+
"""
|
| 506 |
+
|
| 507 |
def apply(self, clip, **kwargs):
|
| 508 |
+
# A fresh cropper per render keeps state isolated
|
| 509 |
cropper = SmartFaceCropper(output_size=self.output_size)
|
| 510 |
return cropper.apply_to_clip(clip)
|
| 511 |
|