Spaces:
Runtime error
Runtime error
Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import cv2
|
| 3 |
+
import numexpr
|
| 4 |
+
import re
|
| 5 |
+
import torch
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
def parse_weight_string(string, max_frames):
|
| 9 |
+
string = re.sub(r'\s+', '', str(string))
|
| 10 |
+
keyframes = {}
|
| 11 |
+
parts = string.split(',')
|
| 12 |
+
|
| 13 |
+
for part in parts:
|
| 14 |
+
try:
|
| 15 |
+
if ':' not in part: continue
|
| 16 |
+
f_str, v_str = part.split(':', 1)
|
| 17 |
+
keyframes[int(f_str)] = v_str.strip('()')
|
| 18 |
+
except: continue
|
| 19 |
+
|
| 20 |
+
if 0 not in keyframes: keyframes[0] = "0"
|
| 21 |
+
|
| 22 |
+
series = np.zeros(int(max_frames))
|
| 23 |
+
sorted_keys = sorted(keyframes.keys())
|
| 24 |
+
|
| 25 |
+
for i in range(len(sorted_keys)):
|
| 26 |
+
f_start = sorted_keys[i]
|
| 27 |
+
f_end = sorted_keys[i+1] if i < len(sorted_keys)-1 else int(max_frames)
|
| 28 |
+
formula = keyframes[f_start]
|
| 29 |
+
|
| 30 |
+
for f in range(f_start, f_end):
|
| 31 |
+
t = f
|
| 32 |
+
try:
|
| 33 |
+
val = numexpr.evaluate(formula, local_dict={'t':t, 'pi':np.pi, 'sin':np.sin, 'cos':np.cos})
|
| 34 |
+
series[f] = float(val)
|
| 35 |
+
except:
|
| 36 |
+
try: series[f] = float(formula)
|
| 37 |
+
except: series[f] = series[f-1] if f > 0 else 0.0
|
| 38 |
+
return series
|
| 39 |
+
|
| 40 |
+
def get_border_mode(mode_str):
|
| 41 |
+
return {'Reflect': cv2.BORDER_REFLECT_101, 'Replicate': cv2.BORDER_REPLICATE, 'Wrap': cv2.BORDER_WRAP, 'Black': cv2.BORDER_CONSTANT}.get(mode_str, cv2.BORDER_REFLECT_101)
|
| 42 |
+
|
| 43 |
+
def maintain_colors(image, anchor, mode='LAB'):
|
| 44 |
+
if mode == 'None' or anchor is None: return image
|
| 45 |
+
img_np = np.array(image).astype(np.uint8)
|
| 46 |
+
anc_np = np.array(anchor).astype(np.uint8)
|
| 47 |
+
|
| 48 |
+
if mode == 'LAB':
|
| 49 |
+
img_cvt = cv2.cvtColor(img_np, cv2.COLOR_RGB2LAB)
|
| 50 |
+
anc_cvt = cv2.cvtColor(anc_np, cv2.COLOR_RGB2LAB)
|
| 51 |
+
for i in range(3):
|
| 52 |
+
img_cvt[:,:,i] = np.clip(img_cvt[:,:,i] - img_cvt[:,:,i].mean() + anc_cvt[:,:,i].mean(), 0, 255)
|
| 53 |
+
return Image.fromarray(cv2.cvtColor(img_cvt, cv2.COLOR_LAB2RGB))
|
| 54 |
+
elif mode == 'HSV':
|
| 55 |
+
img_cvt = cv2.cvtColor(img_np, cv2.COLOR_RGB2HSV)
|
| 56 |
+
anc_cvt = cv2.cvtColor(anc_np, cv2.COLOR_RGB2HSV)
|
| 57 |
+
for i in [1, 2]:
|
| 58 |
+
img_cvt[:,:,i] = np.clip(img_cvt[:,:,i] - img_cvt[:,:,i].mean() + anc_cvt[:,:,i].mean(), 0, 255)
|
| 59 |
+
return Image.fromarray(cv2.cvtColor(img_cvt, cv2.COLOR_HSV2RGB))
|
| 60 |
+
return image
|
| 61 |
+
|
| 62 |
+
def anim_frame_warp_2d(prev_img, args, border_mode):
|
| 63 |
+
if prev_img is None: return None
|
| 64 |
+
cv_img = np.array(prev_img)
|
| 65 |
+
h, w = cv_img.shape[:2]
|
| 66 |
+
center = (w // 2, h // 2)
|
| 67 |
+
mat = cv2.getRotationMatrix2D(center, args.get('angle',0), args.get('zoom',1))
|
| 68 |
+
mat[0, 2] += args.get('tx',0); mat[1, 2] += args.get('ty',0)
|
| 69 |
+
return Image.fromarray(cv2.warpAffine(cv_img, mat, (w, h), borderMode=get_border_mode(border_mode)))
|
| 70 |
+
|
| 71 |
+
def add_noise(img, noise_amt):
|
| 72 |
+
if noise_amt <= 0: return img
|
| 73 |
+
img_np = np.array(img).astype(np.float32)
|
| 74 |
+
noise = np.random.normal(0, noise_amt * 255, img_np.shape).astype(np.float32)
|
| 75 |
+
return Image.fromarray(np.clip(img_np + noise, 0, 255).astype(np.uint8))
|