Niraj70194 commited on
Commit
8609659
·
verified ·
1 Parent(s): 6d84080

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -22
app.py CHANGED
@@ -16,37 +16,38 @@ depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
16
  def apply_gaussian_blur(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
 
16
  def apply_gaussian_blur(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
+ return image
51
 
52
  def apply_lens_blur(image):
53
  # Resize and preprocess the image