Update app.py
Browse files
app.py
CHANGED
@@ -23,49 +23,49 @@ depth_model.to(device)
|
|
23 |
|
24 |
def process_image(image_pil):
|
25 |
image = ImageOps.exif_transpose(image_pil).resize((512, 512)).convert("RGB")
|
26 |
-
|
27 |
-
#
|
28 |
-
seg_inputs = seg_extractor(images=image, return_tensors="pt")
|
29 |
with torch.no_grad():
|
30 |
-
seg_output = seg_model(**seg_inputs).logits
|
31 |
seg_mask = torch.argmax(seg_output, dim=1)[0].cpu().numpy()
|
32 |
binary_mask = np.where(seg_mask > 0, 255, 0).astype(np.uint8)
|
33 |
foreground_mask = Image.fromarray(binary_mask).convert("L")
|
34 |
|
35 |
-
#
|
36 |
-
blurred_background = image.filter(ImageFilter.GaussianBlur(15))
|
37 |
-
blurred_background = blurred_background.convert("RGBA")
|
38 |
image_rgba = image.convert("RGBA")
|
39 |
-
|
|
|
40 |
|
41 |
-
#
|
42 |
image_np = np.array(image)
|
43 |
-
depth_inputs = depth_extractor(images=image_np, return_tensors="pt")
|
44 |
with torch.no_grad():
|
45 |
-
depth_output = depth_model(**depth_inputs)
|
46 |
predicted_depth = depth_output.predicted_depth.squeeze().cpu().numpy()
|
47 |
normalized_depth = (predicted_depth - predicted_depth.min()) / (predicted_depth.max() - predicted_depth.min())
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
resized_depth = cv2.resize(normalized_depth, (image_np.shape[1], image_np.shape[0]))
|
52 |
inverted_depth = 1.0 - resized_depth
|
53 |
-
|
54 |
-
|
55 |
-
for i in range(
|
56 |
sigma = i * 3
|
57 |
-
blurred = cv2.GaussianBlur(
|
58 |
-
|
59 |
|
60 |
-
blur_indices = (inverted_depth * (
|
61 |
-
|
62 |
-
for i in range(
|
63 |
mask = (blur_indices == i)
|
64 |
for c in range(3):
|
65 |
-
|
66 |
-
|
|
|
|
|
67 |
|
68 |
-
return image, output_blur.convert("RGB"), depth_blur_img
|
69 |
|
70 |
# Gradio Interface
|
71 |
gr.Interface(
|
|
|
23 |
|
24 |
def process_image(image_pil):
|
25 |
image = ImageOps.exif_transpose(image_pil).resize((512, 512)).convert("RGB")
|
26 |
+
|
27 |
+
# ---- Segmentation ----
|
28 |
+
seg_inputs = seg_extractor(images=image, return_tensors="pt", do_resize=True, do_normalize=True)
|
29 |
with torch.no_grad():
|
30 |
+
seg_output = seg_model(**seg_inputs.to(device)).logits
|
31 |
seg_mask = torch.argmax(seg_output, dim=1)[0].cpu().numpy()
|
32 |
binary_mask = np.where(seg_mask > 0, 255, 0).astype(np.uint8)
|
33 |
foreground_mask = Image.fromarray(binary_mask).convert("L")
|
34 |
|
35 |
+
# ---- Blur Background ----
|
|
|
|
|
36 |
image_rgba = image.convert("RGBA")
|
37 |
+
blurred = image.filter(ImageFilter.GaussianBlur(15)).convert("RGBA")
|
38 |
+
composite_blur = Image.composite(image_rgba, blurred, foreground_mask)
|
39 |
|
40 |
+
# ---- Depth ----
|
41 |
image_np = np.array(image)
|
42 |
+
depth_inputs = depth_extractor(images=image_np, return_tensors="pt")
|
43 |
with torch.no_grad():
|
44 |
+
depth_output = depth_model(**depth_inputs.to(device))
|
45 |
predicted_depth = depth_output.predicted_depth.squeeze().cpu().numpy()
|
46 |
normalized_depth = (predicted_depth - predicted_depth.min()) / (predicted_depth.max() - predicted_depth.min())
|
47 |
|
48 |
+
# ---- Depth-Based Blur ----
|
49 |
+
image_np = np.array(image).astype(np.float32)
|
50 |
resized_depth = cv2.resize(normalized_depth, (image_np.shape[1], image_np.shape[0]))
|
51 |
inverted_depth = 1.0 - resized_depth
|
52 |
+
blur_levels = 4
|
53 |
+
blurred_variants = []
|
54 |
+
for i in range(blur_levels):
|
55 |
sigma = i * 3
|
56 |
+
blurred = cv2.GaussianBlur(image_np, (15, 15), sigmaX=sigma, sigmaY=sigma) if sigma > 0 else image_np.copy()
|
57 |
+
blurred_variants.append(blurred)
|
58 |
|
59 |
+
blur_indices = (inverted_depth * (blur_levels - 1)).astype(np.uint8)
|
60 |
+
final_blur = np.zeros_like(image_np)
|
61 |
+
for i in range(blur_levels):
|
62 |
mask = (blur_indices == i)
|
63 |
for c in range(3):
|
64 |
+
final_blur[:, :, c][mask] = blurred_variants[i][:, :, c][mask]
|
65 |
+
lens_blur_pil = Image.fromarray(np.clip(final_blur, 0, 255).astype(np.uint8))
|
66 |
+
|
67 |
+
return image, composite_blur.convert("RGB"), lens_blur_pil
|
68 |
|
|
|
69 |
|
70 |
# Gradio Interface
|
71 |
gr.Interface(
|