Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import matplotlib.pyplot as plt
|
|
6 |
import subprocess
|
7 |
import spaces
|
8 |
import torch
|
|
|
9 |
|
10 |
# Run the script to get pretrained models
|
11 |
subprocess.run(["bash", "get_pretrained_models.sh"])
|
@@ -14,11 +15,30 @@ subprocess.run(["bash", "get_pretrained_models.sh"])
|
|
14 |
model, transform = depth_pro.create_model_and_transforms()
|
15 |
model.eval()
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
@spaces.GPU(duration=120)
|
18 |
def predict_depth(input_image):
|
19 |
try:
|
|
|
|
|
|
|
20 |
# Preprocess the image
|
21 |
-
result = depth_pro.load_rgb(
|
22 |
image = result[0]
|
23 |
f_px = result[-1] # Assuming f_px is the last item in the returned tuple
|
24 |
image = transform(image)
|
@@ -63,7 +83,7 @@ iface = gr.Interface(
|
|
63 |
inputs=gr.Image(type="filepath"),
|
64 |
outputs=[gr.Image(type="filepath", label="Depth Map"), gr.Textbox(label="Focal Length or Error Message")],
|
65 |
title="Depth Prediction Demo",
|
66 |
-
description="Upload an image to predict its depth map and focal length."
|
67 |
)
|
68 |
|
69 |
# Launch the interface
|
|
|
6 |
import subprocess
|
7 |
import spaces
|
8 |
import torch
|
9 |
+
import io
|
10 |
|
11 |
# Run the script to get pretrained models
|
12 |
subprocess.run(["bash", "get_pretrained_models.sh"])
|
|
|
15 |
model, transform = depth_pro.create_model_and_transforms()
|
16 |
model.eval()
|
17 |
|
18 |
+
def resize_image(image_path, max_size=1024):
|
19 |
+
with Image.open(image_path) as img:
|
20 |
+
# Calculate the new size while maintaining aspect ratio
|
21 |
+
ratio = max_size / max(img.size)
|
22 |
+
new_size = tuple([int(x * ratio) for x in img.size])
|
23 |
+
|
24 |
+
# Resize the image
|
25 |
+
img = img.resize(new_size, Image.LANCZOS)
|
26 |
+
|
27 |
+
# Save the resized image to a bytes buffer
|
28 |
+
buffer = io.BytesIO()
|
29 |
+
img.save(buffer, format="PNG")
|
30 |
+
buffer.seek(0)
|
31 |
+
|
32 |
+
return buffer
|
33 |
+
|
34 |
@spaces.GPU(duration=120)
|
35 |
def predict_depth(input_image):
|
36 |
try:
|
37 |
+
# Resize the input image
|
38 |
+
resized_image = resize_image(input_image)
|
39 |
+
|
40 |
# Preprocess the image
|
41 |
+
result = depth_pro.load_rgb(resized_image)
|
42 |
image = result[0]
|
43 |
f_px = result[-1] # Assuming f_px is the last item in the returned tuple
|
44 |
image = transform(image)
|
|
|
83 |
inputs=gr.Image(type="filepath"),
|
84 |
outputs=[gr.Image(type="filepath", label="Depth Map"), gr.Textbox(label="Focal Length or Error Message")],
|
85 |
title="Depth Prediction Demo",
|
86 |
+
description="Upload an image to predict its depth map and focal length. Large images will be automatically resized."
|
87 |
)
|
88 |
|
89 |
# Launch the interface
|