gchallar commited on
Commit
f005457
·
verified ·
1 Parent(s): 1150a04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -40
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
- """Segments the input image and applies the selected blur."""
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
- # Rotate the image (assuming this is still needed)
101
- image = image.rotate(-90, expand=True)
102
 
103
- # Prepare input for semantic segmentation
104
- inputs = oneformer_processor(images=image, task_inputs=["semantic"], return_tensors="pt")
 
105
 
106
- # Semantic segmentation
107
- with torch.no_grad():
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
- # Get the mapping of class IDs to labels
115
- id2label = oneformer_model.config.id2label
116
 
117
- # Set foreground label to person
118
- foreground_label = 'person'
119
- foreground_class_id = None
120
- for id, label in id2label.items():
121
- if label == foreground_label:
122
- foreground_class_id = id
123
- break
124
 
125
- if foreground_class_id is None:
126
- return f"Error: Could not find the label '{foreground_label}' in the model's class mapping."
127
 
128
- # Black background mask
129
- output_mask_array = np.zeros(segmentation_mask.shape, dtype=np.uint8)
 
130
 
131
- # Set the pixels corresponding to the foreground object to white (255)
132
- output_mask_array[segmentation_mask == foreground_class_id] = 255
133
-
134
- # Convert the NumPy array to a PIL Image
135
- mask_pil = Image.fromarray(output_mask_array, mode='L')
 
136
 
137
- if blur_type == "Gaussian":
138
- blurred_image = apply_gaussian_blur_background(image, mask_pil, gaussian_radius)
139
- elif blur_type == "Lens":
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,