Kristyyy commited on
Commit
0730fce
·
verified ·
1 Parent(s): bdb2f20

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ from PIL import Image, ImageFilter
4
+ from transformers import AutoImageProcessor, AutoModelForDepthEstimation
5
+ import streamlit as st
6
+
7
+ # Load the depth estimation model
8
+ processor = AutoImageProcessor.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
9
+ model = AutoModelForDepthEstimation.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
10
+
11
+ # Streamlit app title
12
+ st.title("Depth-Based Gaussian Blur App")
13
+ st.markdown("Upload a photo to apply depth-based blurring and adjust blur intensity.")
14
+
15
+ # File uploader
16
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
17
+
18
+ if uploaded_file:
19
+ # Load and preprocess the image
20
+ input_image = Image.open(uploaded_file).convert("RGB") # Ensure image is RGB format
21
+ input_image = input_image.resize((512, 512)) # Resize image for processing
22
+
23
+ # Display the uploaded image
24
+ st.image(input_image, caption="Uploaded Image", use_column_width=True)
25
+
26
+ # Depth map generation
27
+ inputs = processor(images=input_image, return_tensors="pt")
28
+ with torch.no_grad():
29
+ outputs = model(**inputs)
30
+ depth = outputs.predicted_depth.squeeze().cpu().numpy()
31
+
32
+ # Normalize depth map
33
+ depth_min, depth_max = np.min(depth), np.max(depth)
34
+ normalized_depth = (depth - depth_min) / (depth_max - depth_min)
35
+ adjusted_depth = np.clip(normalized_depth, 0, 1)
36
+
37
+ # Blur intensity slider
38
+ max_blur = st.slider("Max Blur Intensity", min_value=1, max_value=10, value=3)
39
+
40
+ # Invert depth for blur
41
+ depth_array = ((1 - adjusted_depth) * max_blur).astype(np.uint8)
42
+
43
+ # Create multiple blurred images
44
+ blurred_images = [input_image.filter(ImageFilter.GaussianBlur(i)) for i in range(max_blur + 1)]
45
+ final_image = Image.new("RGB", input_image.size)
46
+
47
+ # Apply depth-based blur pixel by pixel
48
+ for y in range(input_image.height):
49
+ for x in range(input_image.width):
50
+ blur_level = min(max_blur, max(0, depth_array[y, x]))
51
+ final_image.putpixel((x, y), blurred_images[blur_level].getpixel((x, y)))
52
+
53
+ # Display the blurred image
54
+ st.image(final_image, caption="Depth-Based Blurred Image", use_column_width=True)