akhaliq HF staff commited on
Commit
bdf07c0
1 Parent(s): 26691a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -6,7 +6,8 @@ import matplotlib.pyplot as plt
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"])
@@ -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
- # 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)
@@ -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(