Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -92,56 +92,48 @@ def apply_depth_based_blur_background(image, mask, strength):
|
|
92 |
|
93 |
|
94 |
def segment_and_blur(input_image, blur_type, gaussian_radius=15, lens_strength=5):
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
image = image.rotate(-90, expand=True)
|
102 |
|
103 |
-
|
104 |
-
|
|
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
outputs = oneformer_model(**inputs)
|
109 |
-
|
110 |
-
# Processing semantic segmentation output
|
111 |
-
predicted_semantic_map = oneformer_processor.post_process_semantic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]
|
112 |
-
segmentation_mask = predicted_semantic_map.cpu().numpy()
|
113 |
|
114 |
-
|
115 |
-
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
break
|
124 |
|
125 |
-
|
126 |
-
|
127 |
|
128 |
-
|
129 |
-
|
|
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
|
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
blurred_image = apply_depth_based_blur_background(image, mask_pil, lens_strength)
|
141 |
-
else:
|
142 |
-
return "Error: Invalid blur type selected."
|
143 |
|
144 |
-
return blurred_image
|
145 |
|
146 |
iface = gr.Interface(
|
147 |
fn=segment_and_blur,
|
|
|
92 |
|
93 |
|
94 |
def segment_and_blur(input_image, blur_type, gaussian_radius=15, lens_strength=5):
|
95 |
+
try:
|
96 |
+
if oneformer_processor is None or oneformer_model is None:
|
97 |
+
return "Error: OneFormer model not loaded."
|
98 |
|
99 |
+
image = input_image.convert("RGB")
|
100 |
+
image = image.rotate(-90, expand=True)
|
|
|
101 |
|
102 |
+
inputs = oneformer_processor(images=image, task_inputs=["semantic"], return_tensors="pt")
|
103 |
+
with torch.no_grad():
|
104 |
+
outputs = oneformer_model(**inputs)
|
105 |
|
106 |
+
predicted_semantic_map = oneformer_processor.post_process_semantic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]
|
107 |
+
segmentation_mask = predicted_semantic_map.cpu().numpy()
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
+
id2label = oneformer_model.config.id2label
|
110 |
+
print(id2label) # <-- Add this to debug
|
111 |
|
112 |
+
foreground_label = 'person'
|
113 |
+
foreground_class_id = None
|
114 |
+
for id, label in id2label.items():
|
115 |
+
if label.lower() == foreground_label.lower():
|
116 |
+
foreground_class_id = id
|
117 |
+
break
|
|
|
118 |
|
119 |
+
if foreground_class_id is None:
|
120 |
+
return f"Error: Could not find the label '{foreground_label}' in the model's class mapping."
|
121 |
|
122 |
+
output_mask_array = np.zeros(segmentation_mask.shape, dtype=np.uint8)
|
123 |
+
output_mask_array[segmentation_mask == foreground_class_id] = 255
|
124 |
+
mask_pil = Image.fromarray(output_mask_array, mode='L')
|
125 |
|
126 |
+
if blur_type == "Gaussian":
|
127 |
+
blurred_image = apply_gaussian_blur_background(image, mask_pil, gaussian_radius)
|
128 |
+
elif blur_type == "Lens":
|
129 |
+
blurred_image = apply_depth_based_blur_background(image, mask_pil, lens_strength)
|
130 |
+
else:
|
131 |
+
return "Error: Invalid blur type selected."
|
132 |
|
133 |
+
return blurred_image
|
134 |
+
except Exception as e:
|
135 |
+
return f"Error during processing: {str(e)}"
|
|
|
|
|
|
|
136 |
|
|
|
137 |
|
138 |
iface = gr.Interface(
|
139 |
fn=segment_and_blur,
|