Spaces:
Running
Running
Create modules/effects.py
Browse files- modules/effects.py +39 -0
modules/effects.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import random
|
| 4 |
+
import math
|
| 5 |
+
|
| 6 |
+
def apply_old_film(frame, frame_idx, fps):
|
| 7 |
+
h, w = frame.shape[:2]
|
| 8 |
+
if random.random() < 0.03:
|
| 9 |
+
frame = (frame * random.uniform(0.6, 1.0)).astype(np.uint8)
|
| 10 |
+
if random.random() < 0.02:
|
| 11 |
+
x, length, y = random.randint(0, w - 1), random.randint(10, h // 3), random.randint(0, h - length)
|
| 12 |
+
cv2.line(frame, (x, y), (x + random.randint(-2, 2), y + length), (255, 255, 255), 1)
|
| 13 |
+
frame = cv2.addWeighted(frame, 0.85, np.full_like(frame, (200, 180, 150)), 0.15, 0)
|
| 14 |
+
mask = np.zeros((h, w), dtype=np.float32)
|
| 15 |
+
cv2.ellipse(mask, (w // 2, h // 2), (int(w * 0.7), int(h * 0.7)), 0, 0, 360, 1, -1)
|
| 16 |
+
mask = cv2.GaussianBlur(mask, (0, 0), w // 3).reshape(h, w, 1)
|
| 17 |
+
return (frame * (0.4 + 0.6 * mask)).astype(np.uint8)
|
| 18 |
+
|
| 19 |
+
def apply_heat_haze(frame, frame_idx, fps):
|
| 20 |
+
h, w = frame.shape[:2]
|
| 21 |
+
y_start = h * 3 // 4
|
| 22 |
+
displacement = (np.sin(np.linspace(0, math.pi * 4, w) + frame_idx * 0.3) * 3).astype(np.int32)
|
| 23 |
+
map_x = np.tile(np.arange(w, dtype=np.float32), (h - y_start, 1))
|
| 24 |
+
map_y = np.tile(np.arange(y_start, h, dtype=np.float32).reshape(-1, 1), (1, w))
|
| 25 |
+
map_x = np.clip(map_x + displacement.reshape(1, w), 0, w - 1)
|
| 26 |
+
frame[y_start:, :] = cv2.remap(frame, map_x.astype(np.float32), map_y.astype(np.float32), cv2.INTER_LINEAR)
|
| 27 |
+
return frame
|
| 28 |
+
|
| 29 |
+
def apply_flame(frame, frame_idx, fps):
|
| 30 |
+
h, w = frame.shape[:2]
|
| 31 |
+
flame_h = h // 5
|
| 32 |
+
flame = np.zeros((flame_h, w, 3), dtype=np.uint8)
|
| 33 |
+
for i in range(flame_h):
|
| 34 |
+
alpha = 1 - (i / flame_h)
|
| 35 |
+
flame[i, :] = (0, int(140 * alpha * (0.5 + 0.5 * math.sin(frame_idx * 0.5 + i * 0.3))), int(255 * alpha))
|
| 36 |
+
flicker = 0.7 + 0.3 * math.sin(frame_idx * 0.8) * random.uniform(0.8, 1.2)
|
| 37 |
+
flame = (flame * flicker).astype(np.uint8)
|
| 38 |
+
frame[h - flame_h:, :] = cv2.addWeighted(frame[h - flame_h:, :], 0.4, flame, 0.6, 0)
|
| 39 |
+
return frame
|