Commit ·
7442339
1
Parent(s): ab310bb
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import requests
|
| 2 |
import numpy as np
|
| 3 |
-
from models.deep_colorization.colorizers import postprocess_tens, preprocess_img
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
# Define a function that we can use to load lottie files from a link.
|
|
@@ -36,3 +37,16 @@ def format_time(seconds: float) -> str:
|
|
| 36 |
def colorize_frame(frame, colorizer) -> np.ndarray:
|
| 37 |
tens_l_orig, tens_l_rs = preprocess_img(frame, HW=(256, 256))
|
| 38 |
return postprocess_tens(tens_l_orig, colorizer(tens_l_rs).cpu())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import numpy as np
|
| 3 |
+
from models.deep_colorization.colorizers import postprocess_tens, preprocess_img, load_img
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
|
| 7 |
# Define a function that we can use to load lottie files from a link.
|
|
|
|
| 37 |
def colorize_frame(frame, colorizer) -> np.ndarray:
|
| 38 |
tens_l_orig, tens_l_rs = preprocess_img(frame, HW=(256, 256))
|
| 39 |
return postprocess_tens(tens_l_orig, colorizer(tens_l_rs).cpu())
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def colorize_image(file, loaded_model):
|
| 43 |
+
img = load_img(file)
|
| 44 |
+
# If user input a colored image with 4 channels, discard the fourth channel
|
| 45 |
+
if img.shape[2] == 4:
|
| 46 |
+
img = img[:, :, :3]
|
| 47 |
+
|
| 48 |
+
tens_l_orig, tens_l_rs = preprocess_img(img, HW=(256, 256))
|
| 49 |
+
out_img = postprocess_tens(tens_l_orig, loaded_model(tens_l_rs).cpu())
|
| 50 |
+
new_img = Image.fromarray((out_img * 255).astype(np.uint8))
|
| 51 |
+
|
| 52 |
+
return out_img, new_img
|