Spaces:
Running
Running
| import cv2 | |
| import numpy as np | |
| import random | |
| import math | |
| def apply_old_film(frame, frame_idx, fps): | |
| h, w = frame.shape[:2] | |
| if random.random() < 0.03: | |
| frame = (frame * random.uniform(0.6, 1.0)).astype(np.uint8) | |
| if random.random() < 0.02: | |
| x, length, y = random.randint(0, w - 1), random.randint(10, h // 3), random.randint(0, h - length) | |
| cv2.line(frame, (x, y), (x + random.randint(-2, 2), y + length), (255, 255, 255), 1) | |
| frame = cv2.addWeighted(frame, 0.85, np.full_like(frame, (200, 180, 150)), 0.15, 0) | |
| mask = np.zeros((h, w), dtype=np.float32) | |
| cv2.ellipse(mask, (w // 2, h // 2), (int(w * 0.7), int(h * 0.7)), 0, 0, 360, 1, -1) | |
| mask = cv2.GaussianBlur(mask, (0, 0), w // 3).reshape(h, w, 1) | |
| return (frame * (0.4 + 0.6 * mask)).astype(np.uint8) | |
| def apply_heat_haze(frame, frame_idx, fps): | |
| h, w = frame.shape[:2] | |
| y_start = h * 3 // 4 | |
| displacement = (np.sin(np.linspace(0, math.pi * 4, w) + frame_idx * 0.3) * 3).astype(np.int32) | |
| map_x = np.tile(np.arange(w, dtype=np.float32), (h - y_start, 1)) | |
| map_y = np.tile(np.arange(y_start, h, dtype=np.float32).reshape(-1, 1), (1, w)) | |
| map_x = np.clip(map_x + displacement.reshape(1, w), 0, w - 1) | |
| frame[y_start:, :] = cv2.remap(frame, map_x.astype(np.float32), map_y.astype(np.float32), cv2.INTER_LINEAR) | |
| return frame | |
| def apply_flame(frame, frame_idx, fps): | |
| h, w = frame.shape[:2] | |
| flame_h = h // 5 | |
| flame = np.zeros((flame_h, w, 3), dtype=np.uint8) | |
| for i in range(flame_h): | |
| alpha = 1 - (i / flame_h) | |
| flame[i, :] = (0, int(140 * alpha * (0.5 + 0.5 * math.sin(frame_idx * 0.5 + i * 0.3))), int(255 * alpha)) | |
| flicker = 0.7 + 0.3 * math.sin(frame_idx * 0.8) * random.uniform(0.8, 1.2) | |
| flame = (flame * flicker).astype(np.uint8) | |
| frame[h - flame_h:, :] = cv2.addWeighted(frame[h - flame_h:, :], 0.4, flame, 0.6, 0) | |
| return frame |