Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,8 @@ import matplotlib.pyplot as plt
|
|
| 6 |
import subprocess
|
| 7 |
import spaces
|
| 8 |
import torch
|
| 9 |
-
import
|
|
|
|
| 10 |
|
| 11 |
# Run the script to get pretrained models
|
| 12 |
subprocess.run(["bash", "get_pretrained_models.sh"])
|
|
@@ -24,21 +25,20 @@ def resize_image(image_path, max_size=1024):
|
|
| 24 |
# Resize the image
|
| 25 |
img = img.resize(new_size, Image.LANCZOS)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return buffer
|
| 33 |
|
| 34 |
@spaces.GPU(duration=120)
|
| 35 |
def predict_depth(input_image):
|
|
|
|
| 36 |
try:
|
| 37 |
# Resize the input image
|
| 38 |
-
|
| 39 |
|
| 40 |
# Preprocess the image
|
| 41 |
-
result = depth_pro.load_rgb(
|
| 42 |
image = result[0]
|
| 43 |
f_px = result[-1] # Assuming f_px is the last item in the returned tuple
|
| 44 |
image = transform(image)
|
|
@@ -76,6 +76,10 @@ def predict_depth(input_image):
|
|
| 76 |
return output_path, f"Focal length: {focallength_px:.2f} pixels"
|
| 77 |
except Exception as e:
|
| 78 |
return None, f"An error occurred: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# Create Gradio interface
|
| 81 |
iface = gr.Interface(
|
|
|
|
| 6 |
import subprocess
|
| 7 |
import spaces
|
| 8 |
import torch
|
| 9 |
+
import tempfile
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
# Run the script to get pretrained models
|
| 13 |
subprocess.run(["bash", "get_pretrained_models.sh"])
|
|
|
|
| 25 |
# Resize the image
|
| 26 |
img = img.resize(new_size, Image.LANCZOS)
|
| 27 |
|
| 28 |
+
# Create a temporary file
|
| 29 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
| 30 |
+
img.save(temp_file, format="PNG")
|
| 31 |
+
return temp_file.name
|
|
|
|
|
|
|
| 32 |
|
| 33 |
@spaces.GPU(duration=120)
|
| 34 |
def predict_depth(input_image):
|
| 35 |
+
temp_file = None
|
| 36 |
try:
|
| 37 |
# Resize the input image
|
| 38 |
+
temp_file = resize_image(input_image)
|
| 39 |
|
| 40 |
# Preprocess the image
|
| 41 |
+
result = depth_pro.load_rgb(temp_file)
|
| 42 |
image = result[0]
|
| 43 |
f_px = result[-1] # Assuming f_px is the last item in the returned tuple
|
| 44 |
image = transform(image)
|
|
|
|
| 76 |
return output_path, f"Focal length: {focallength_px:.2f} pixels"
|
| 77 |
except Exception as e:
|
| 78 |
return None, f"An error occurred: {str(e)}"
|
| 79 |
+
finally:
|
| 80 |
+
# Clean up the temporary file
|
| 81 |
+
if temp_file and os.path.exists(temp_file):
|
| 82 |
+
os.remove(temp_file)
|
| 83 |
|
| 84 |
# Create Gradio interface
|
| 85 |
iface = gr.Interface(
|