Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,9 @@ from huggingface_hub import hf_hub_download
|
|
| 12 |
|
| 13 |
from depth_anything_v2.dpt import DepthAnythingV2
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
css = """
|
| 16 |
#img-display-container {
|
| 17 |
max-height: 100vh;
|
|
@@ -37,11 +40,11 @@ encoder2name = {
|
|
| 37 |
'vits': 'Small',
|
| 38 |
'vitb': 'Base',
|
| 39 |
'vitl': 'Large',
|
| 40 |
-
'vitg': 'Giant',
|
| 41 |
}
|
| 42 |
models = {}
|
| 43 |
for encoder_key in model_configs.keys():
|
| 44 |
-
if encoder_key != 'vitg':
|
| 45 |
try:
|
| 46 |
model_name = encoder2name[encoder_key]
|
| 47 |
model = DepthAnythingV2(**model_configs[encoder_key])
|
|
@@ -54,7 +57,6 @@ for encoder_key in model_configs.keys():
|
|
| 54 |
except Exception as e:
|
| 55 |
print(f"Failed to load {encoder2name[encoder_key]} model: {e}")
|
| 56 |
|
| 57 |
-
# Set default model
|
| 58 |
default_model = 'vitl' if 'vitl' in models else list(models.keys())[0]
|
| 59 |
|
| 60 |
title = "# Depthinator"
|
|
@@ -68,7 +70,7 @@ def predict_depth(image, model_choice=None):
|
|
| 68 |
|
| 69 |
def process_multiple_images(files, model_choice=None, include_bw=True):
|
| 70 |
if not files:
|
| 71 |
-
return [],
|
| 72 |
|
| 73 |
if model_choice is None:
|
| 74 |
model_choice = default_model
|
|
@@ -83,6 +85,8 @@ def process_multiple_images(files, model_choice=None, include_bw=True):
|
|
| 83 |
|
| 84 |
if len(image_np.shape) == 3:
|
| 85 |
original_image = image_np.copy()
|
|
|
|
|
|
|
| 86 |
depth = predict_depth(image_np[:, :, ::-1], model_choice)
|
| 87 |
|
| 88 |
# Create colored depth map
|
|
@@ -127,6 +131,8 @@ def process_multiple_images(files, model_choice=None, include_bw=True):
|
|
| 127 |
|
| 128 |
processed_files.extend([colored_depth_path.name, gray_depth_path.name, raw_depth_path.name])
|
| 129 |
|
|
|
|
|
|
|
| 130 |
except Exception as err:
|
| 131 |
print(f"Error processing {file.name}: {str(err)}")
|
| 132 |
continue
|
|
@@ -137,7 +143,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 137 |
gr.Markdown(title)
|
| 138 |
gr.Markdown(description)
|
| 139 |
|
| 140 |
-
# Create model choices for dropdown
|
| 141 |
model_choices = [(f"{encoder2name[k]} ({k})", k) for k in models.keys()]
|
| 142 |
|
| 143 |
with gr.Tabs():
|
|
@@ -183,7 +188,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 183 |
tmp_gray_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
| 184 |
gray_depth.save(tmp_gray_depth.name)
|
| 185 |
|
| 186 |
-
# Create black and white depth if requested
|
| 187 |
tmp_bw_depth = None
|
| 188 |
if include_bw:
|
| 189 |
bw_depth = Image.fromarray(depth).convert('L')
|
|
@@ -196,4 +200,4 @@ with gr.Blocks(css=css) as demo:
|
|
| 196 |
submit_multiple.click(process_multiple_images, inputs=[multiple_files, model_dropdown_multiple, include_bw_multiple], outputs=[gallery_output, download_files])
|
| 197 |
|
| 198 |
if __name__ == '__main__':
|
| 199 |
-
demo.queue().launch(
|
|
|
|
| 12 |
|
| 13 |
from depth_anything_v2.dpt import DepthAnythingV2
|
| 14 |
|
| 15 |
+
# Add this at the top to help with ZeroGPU authentication
|
| 16 |
+
os.environ['SPACES_ZERO_GPU'] = '1'
|
| 17 |
+
|
| 18 |
css = """
|
| 19 |
#img-display-container {
|
| 20 |
max-height: 100vh;
|
|
|
|
| 40 |
'vits': 'Small',
|
| 41 |
'vitb': 'Base',
|
| 42 |
'vitl': 'Large',
|
| 43 |
+
'vitg': 'Giant',
|
| 44 |
}
|
| 45 |
models = {}
|
| 46 |
for encoder_key in model_configs.keys():
|
| 47 |
+
if encoder_key != 'vitg':
|
| 48 |
try:
|
| 49 |
model_name = encoder2name[encoder_key]
|
| 50 |
model = DepthAnythingV2(**model_configs[encoder_key])
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
print(f"Failed to load {encoder2name[encoder_key]} model: {e}")
|
| 59 |
|
|
|
|
| 60 |
default_model = 'vitl' if 'vitl' in models else list(models.keys())[0]
|
| 61 |
|
| 62 |
title = "# Depthinator"
|
|
|
|
| 70 |
|
| 71 |
def process_multiple_images(files, model_choice=None, include_bw=True):
|
| 72 |
if not files:
|
| 73 |
+
return [], []
|
| 74 |
|
| 75 |
if model_choice is None:
|
| 76 |
model_choice = default_model
|
|
|
|
| 85 |
|
| 86 |
if len(image_np.shape) == 3:
|
| 87 |
original_image = image_np.copy()
|
| 88 |
+
|
| 89 |
+
# Call the GPU function for each image
|
| 90 |
depth = predict_depth(image_np[:, :, ::-1], model_choice)
|
| 91 |
|
| 92 |
# Create colored depth map
|
|
|
|
| 131 |
|
| 132 |
processed_files.extend([colored_depth_path.name, gray_depth_path.name, raw_depth_path.name])
|
| 133 |
|
| 134 |
+
print(f"Successfully processed {base_name}")
|
| 135 |
+
|
| 136 |
except Exception as err:
|
| 137 |
print(f"Error processing {file.name}: {str(err)}")
|
| 138 |
continue
|
|
|
|
| 143 |
gr.Markdown(title)
|
| 144 |
gr.Markdown(description)
|
| 145 |
|
|
|
|
| 146 |
model_choices = [(f"{encoder2name[k]} ({k})", k) for k in models.keys()]
|
| 147 |
|
| 148 |
with gr.Tabs():
|
|
|
|
| 188 |
tmp_gray_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
| 189 |
gray_depth.save(tmp_gray_depth.name)
|
| 190 |
|
|
|
|
| 191 |
tmp_bw_depth = None
|
| 192 |
if include_bw:
|
| 193 |
bw_depth = Image.fromarray(depth).convert('L')
|
|
|
|
| 200 |
submit_multiple.click(process_multiple_images, inputs=[multiple_files, model_dropdown_multiple, include_bw_multiple], outputs=[gallery_output, download_files])
|
| 201 |
|
| 202 |
if __name__ == '__main__':
|
| 203 |
+
demo.queue().launch()
|