Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ import imageio as imageio
|
|
| 8 |
import numpy as np
|
| 9 |
import spaces
|
| 10 |
import torch as torch
|
| 11 |
-
from PIL import Image
|
| 12 |
from tqdm import tqdm
|
| 13 |
from pathlib import Path
|
| 14 |
import gradio
|
|
@@ -23,9 +23,17 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
| 23 |
if "HF_TOKEN_LOGIN" in os.environ:
|
| 24 |
login(token=os.environ["HF_TOKEN_LOGIN"])
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def infer(path_input, seed=None):
|
| 27 |
name_base, name_ext = os.path.splitext(os.path.basename(path_input))
|
| 28 |
_, output_d = lotus(path_input, 'depth', seed, device)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if not os.path.exists("files/output"):
|
| 30 |
os.makedirs("files/output")
|
| 31 |
d_save_path = os.path.join("files/output", f"{name_base}_d{name_ext}")
|
|
@@ -34,11 +42,25 @@ def infer(path_input, seed=None):
|
|
| 34 |
|
| 35 |
def infer_video(path_input, seed=None):
|
| 36 |
_, frames_d, fps = lotus_video(path_input, 'depth', seed, device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
if not os.path.exists("files/output"):
|
| 38 |
os.makedirs("files/output")
|
| 39 |
name_base, _ = os.path.splitext(os.path.basename(path_input))
|
| 40 |
d_save_path = os.path.join("files/output", f"{name_base}_d.mp4")
|
| 41 |
-
imageio.mimsave(d_save_path,
|
| 42 |
return d_save_path
|
| 43 |
|
| 44 |
def run_demo_server():
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
import spaces
|
| 10 |
import torch as torch
|
| 11 |
+
from PIL import Image, ImageFilter
|
| 12 |
from tqdm import tqdm
|
| 13 |
from pathlib import Path
|
| 14 |
import gradio
|
|
|
|
| 23 |
if "HF_TOKEN_LOGIN" in os.environ:
|
| 24 |
login(token=os.environ["HF_TOKEN_LOGIN"])
|
| 25 |
|
| 26 |
+
def apply_gaussian_blur(image, radius=0.75):
|
| 27 |
+
"""Apply Gaussian blur to PIL Image with specified radius"""
|
| 28 |
+
return image.filter(ImageFilter.GaussianBlur(radius=radius))
|
| 29 |
+
|
| 30 |
def infer(path_input, seed=None):
|
| 31 |
name_base, name_ext = os.path.splitext(os.path.basename(path_input))
|
| 32 |
_, output_d = lotus(path_input, 'depth', seed, device)
|
| 33 |
+
|
| 34 |
+
# Apply Gaussian blur with 0.75 radius
|
| 35 |
+
output_d = apply_gaussian_blur(output_d, radius=0.75)
|
| 36 |
+
|
| 37 |
if not os.path.exists("files/output"):
|
| 38 |
os.makedirs("files/output")
|
| 39 |
d_save_path = os.path.join("files/output", f"{name_base}_d{name_ext}")
|
|
|
|
| 42 |
|
| 43 |
def infer_video(path_input, seed=None):
|
| 44 |
_, frames_d, fps = lotus_video(path_input, 'depth', seed, device)
|
| 45 |
+
|
| 46 |
+
# Apply Gaussian blur to each frame
|
| 47 |
+
blurred_frames = []
|
| 48 |
+
for frame in frames_d:
|
| 49 |
+
# Convert numpy array to PIL Image if needed
|
| 50 |
+
if isinstance(frame, np.ndarray):
|
| 51 |
+
frame_pil = Image.fromarray(frame)
|
| 52 |
+
else:
|
| 53 |
+
frame_pil = frame
|
| 54 |
+
|
| 55 |
+
# Apply blur and convert back to numpy array
|
| 56 |
+
blurred_frame = apply_gaussian_blur(frame_pil, radius=0.75)
|
| 57 |
+
blurred_frames.append(np.array(blurred_frame))
|
| 58 |
+
|
| 59 |
if not os.path.exists("files/output"):
|
| 60 |
os.makedirs("files/output")
|
| 61 |
name_base, _ = os.path.splitext(os.path.basename(path_input))
|
| 62 |
d_save_path = os.path.join("files/output", f"{name_base}_d.mp4")
|
| 63 |
+
imageio.mimsave(d_save_path, blurred_frames, fps=fps)
|
| 64 |
return d_save_path
|
| 65 |
|
| 66 |
def run_demo_server():
|