ArunLouis commited on
Commit
478fbc7
·
verified ·
1 Parent(s): 2aa301a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -29
app.py CHANGED
@@ -3,42 +3,27 @@ import numpy as np
3
  import torch
4
  from PIL import Image
5
  import cv2
6
- import os # Import the os module
7
 
8
- from transformers import DPTImageProcessor, DPTForDepthEstimation
9
 
10
- # Access the token from the Space's secrets
11
- HF_TOKEN = os.environ.get("HUGGING_FACE_TOKEN")
12
- if HF_TOKEN is None:
13
- raise ValueError("Hugging Face token not found. Make sure you've added it as a secret to your Space.")
14
-
15
- # Load the image processor and model
16
- image_processor = DPTImageProcessor.from_pretrained("Intel/dpt-hybrid-midas", token=HF_TOKEN)
17
- model = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas", low_cpu_mem_usage=True, token=HF_TOKEN)
18
- model.to('cuda')
19
- model.eval()
20
 
21
  def apply_depth_aware_blur(image, foreground_blur, midground_blur, background_blur, foreground_threshold, midground_lower, midground_upper, background_threshold):
22
  original_image = Image.fromarray(image).convert("RGB")
23
  original_image = original_image.resize((512, 512))
24
  image_np = np.array(original_image)
25
 
26
- inputs = image_processor(images=original_image, return_tensors="pt").to('cuda')
27
-
28
- with torch.no_grad():
29
- outputs = model(**inputs)
30
- predicted_depth = outputs.predicted_depth
31
-
32
- prediction = torch.nn.functional.interpolate(
33
- predicted_depth.unsqueeze(1),
34
- size=(512, 512),
35
- mode="bicubic",
36
- align_corners=False,
37
- )
38
-
39
- depth_map = prediction.squeeze().cpu().numpy()
40
- normalized_depth_map = (depth_map - np.min(depth_map)) / (np.max(depth_map) - np.min(depth_map))
41
 
 
42
  foreground_mask = (normalized_depth_map < foreground_threshold).astype(np.uint8) * 255
43
  midground_mask = ((normalized_depth_map >= midground_lower) & (normalized_depth_map < midground_upper)).astype(np.uint8) * 255
44
  background_mask = (normalized_depth_map >= background_threshold).astype(np.uint8) * 255
@@ -65,11 +50,10 @@ iface = gr.Interface(
65
  gr.Slider(0, 1, label="Midground Lower Threshold", default=0.2),
66
  gr.Slider(0, 1, label="Midground Upper Threshold", default=0.6),
67
  gr.Slider(0, 1, label="Background Threshold", default=0.6)
68
-
69
  ],
70
  outputs=gr.Image(label="Blurred Image"),
71
  title="Depth-Aware Lens Blur App",
72
- description="Apply depth-based blur to uploaded images. Adjust blur intensity for foreground, midground, and background.",
73
  )
74
 
75
  if __name__ == "__main__":
 
3
  import torch
4
  from PIL import Image
5
  import cv2
6
+ import requests
7
 
8
+ from transformers import pipeline
9
 
10
+ # Load the depth estimation pipeline
11
+ pipe = pipeline(task="depth-estimation", model="depth-anything/Depth-Anything-V2-Small-hf")
 
 
 
 
 
 
 
 
12
 
13
  def apply_depth_aware_blur(image, foreground_blur, midground_blur, background_blur, foreground_threshold, midground_lower, midground_upper, background_threshold):
14
  original_image = Image.fromarray(image).convert("RGB")
15
  original_image = original_image.resize((512, 512))
16
  image_np = np.array(original_image)
17
 
18
+ # Inference
19
+ depth = pipe(original_image)["depth"]
20
+ depth = np.array(depth) # Convert to numpy array
21
+ depth = cv2.resize(depth, (512, 512), interpolation=cv2.INTER_CUBIC) # Resize depth map
22
+
23
+ # Normalize the depth map
24
+ normalized_depth_map = (depth - np.min(depth)) / (np.max(depth) - np.min(depth))
 
 
 
 
 
 
 
 
25
 
26
+ # Create masks
27
  foreground_mask = (normalized_depth_map < foreground_threshold).astype(np.uint8) * 255
28
  midground_mask = ((normalized_depth_map >= midground_lower) & (normalized_depth_map < midground_upper)).astype(np.uint8) * 255
29
  background_mask = (normalized_depth_map >= background_threshold).astype(np.uint8) * 255
 
50
  gr.Slider(0, 1, label="Midground Lower Threshold", default=0.2),
51
  gr.Slider(0, 1, label="Midground Upper Threshold", default=0.6),
52
  gr.Slider(0, 1, label="Background Threshold", default=0.6)
 
53
  ],
54
  outputs=gr.Image(label="Blurred Image"),
55
  title="Depth-Aware Lens Blur App",
56
+ description="Apply depth-based blur to uploaded images using Depth Anything V2. Adjust blur intensity for foreground, midground, and background.",
57
  )
58
 
59
  if __name__ == "__main__":