Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
import torch
|
2 |
-
from transformers import
|
3 |
from PIL import Image, ImageFilter
|
4 |
import numpy as np
|
5 |
import gradio as gr
|
6 |
|
7 |
-
# Load pre-trained models and
|
8 |
-
|
9 |
seg_model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
10 |
-
|
11 |
depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
12 |
|
13 |
def process_image(image):
|
@@ -15,7 +15,7 @@ def process_image(image):
|
|
15 |
image = image.resize((512, 512))
|
16 |
|
17 |
# Perform semantic segmentation
|
18 |
-
seg_inputs =
|
19 |
with torch.no_grad():
|
20 |
seg_outputs = seg_model(**seg_inputs)
|
21 |
seg_logits = seg_outputs.logits
|
@@ -26,7 +26,7 @@ def process_image(image):
|
|
26 |
binary_mask = (segmentation == person_class_index).astype(np.uint8) * 255
|
27 |
|
28 |
# Perform depth estimation
|
29 |
-
depth_inputs =
|
30 |
with torch.no_grad():
|
31 |
depth_outputs = depth_model(**depth_inputs)
|
32 |
predicted_depth = depth_outputs.predicted_depth[0].cpu().numpy()
|
@@ -64,3 +64,4 @@ interface = gr.Interface(
|
|
64 |
# Launch the interface
|
65 |
if __name__ == "__main__":
|
66 |
interface.launch()
|
|
|
|
1 |
import torch
|
2 |
+
from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation, DPTImageProcessor, DPTForDepthEstimation
|
3 |
from PIL import Image, ImageFilter
|
4 |
import numpy as np
|
5 |
import gradio as gr
|
6 |
|
7 |
+
# Load pre-trained models and processors
|
8 |
+
seg_processor = SegformerImageProcessor.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
9 |
seg_model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
10 |
+
depth_processor = DPTImageProcessor.from_pretrained("Intel/dpt-large")
|
11 |
depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
12 |
|
13 |
def process_image(image):
|
|
|
15 |
image = image.resize((512, 512))
|
16 |
|
17 |
# Perform semantic segmentation
|
18 |
+
seg_inputs = seg_processor(images=image, return_tensors="pt")
|
19 |
with torch.no_grad():
|
20 |
seg_outputs = seg_model(**seg_inputs)
|
21 |
seg_logits = seg_outputs.logits
|
|
|
26 |
binary_mask = (segmentation == person_class_index).astype(np.uint8) * 255
|
27 |
|
28 |
# Perform depth estimation
|
29 |
+
depth_inputs = depth_processor(images=image, return_tensors="pt")
|
30 |
with torch.no_grad():
|
31 |
depth_outputs = depth_model(**depth_inputs)
|
32 |
predicted_depth = depth_outputs.predicted_depth[0].cpu().numpy()
|
|
|
64 |
# Launch the interface
|
65 |
if __name__ == "__main__":
|
66 |
interface.launch()
|
67 |
+
|