Niraj70194 commited on
Commit
ac0c7c2
·
verified ·
1 Parent(s): df6bc83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -13,33 +13,40 @@ segmentation_model = BeitForSemanticSegmentation.from_pretrained("microsoft/beit
13
  depth_feature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-large")
14
  depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
15
 
16
- def apply_gaussian_blur(image):
17
  # Resize and preprocess the image
18
  image = image.resize((512, 512)).convert("RGB")
19
- inputs = segmentation_processor(image, return_tensors="pt")
20
 
21
- # Perform segmentation to get the 'person' mask
22
  with torch.no_grad():
23
- outputs = segmentation_model(**inputs)
24
  logits = outputs.logits
25
- segmentation = torch.argmax(logits, dim=1)[0]
26
-
27
- # Create a binary mask for the 'person' class (index 12)
28
- person_index = 12
29
- binary_mask = (segmentation == person_index).numpy().astype(np.uint8) * 255
30
 
31
- # Apply Gaussian blur to the entire image
 
 
 
 
 
 
 
32
  image_np = np.array(image)
 
 
33
  blurred_image = cv2.GaussianBlur(image_np, (0, 0), sigmaX=15, sigmaY=15)
34
 
35
  # Normalize the mask to range between 0 and 1
36
  normalized_mask = binary_mask / 255.0
37
- normalized_mask = np.expand_dims(normalized_mask, axis=-1)
38
 
39
- # Create the composite image
40
  final_image = (image_np * normalized_mask + blurred_image * (1 - normalized_mask)).astype(np.uint8)
41
 
42
- return Image.fromarray(final_image)
 
 
 
43
 
44
  def apply_lens_blur(image):
45
  # Resize and preprocess the image
 
13
  depth_feature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-large")
14
  depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
15
 
16
+ def apply_gaussian_blur(image: Image.Image) -> Image.Image:
17
  # Resize and preprocess the image
18
  image = image.resize((512, 512)).convert("RGB")
19
+ inputs = image_processor(image, return_tensors="pt")
20
 
21
+ # Perform semantic segmentation using the model
22
  with torch.no_grad():
23
+ outputs = model(**inputs)
24
  logits = outputs.logits
 
 
 
 
 
25
 
26
+ # Get the predicted class for each pixel
27
+ segmentation = torch.argmax(logits, dim=1)[0] # Shape: [height, width]
28
+
29
+ # Create a binary mask for the 'person' class
30
+ person_index = 12 # Assuming 12 is the 'person' class index
31
+ binary_mask = (segmentation == person_index).numpy().astype(np.uint8) * 255 # Convert to 0 and 255
32
+
33
+ # Convert the original image to a numpy array
34
  image_np = np.array(image)
35
+
36
+ # Apply Gaussian blur to the entire image
37
  blurred_image = cv2.GaussianBlur(image_np, (0, 0), sigmaX=15, sigmaY=15)
38
 
39
  # Normalize the mask to range between 0 and 1
40
  normalized_mask = binary_mask / 255.0
41
+ normalized_mask = np.expand_dims(normalized_mask, axis=-1) # Add channel dimension
42
 
43
+ # Create the composite image with the blurred background
44
  final_image = (image_np * normalized_mask + blurred_image * (1 - normalized_mask)).astype(np.uint8)
45
 
46
+ # Convert back to PIL Image
47
+ final_image_pil = Image.fromarray(final_image)
48
+
49
+ return final_image_pil
50
 
51
  def apply_lens_blur(image):
52
  # Resize and preprocess the image