maria355 commited on
Commit
0e00bc2
·
verified ·
1 Parent(s): 9197cc4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +235 -236
app.py CHANGED
@@ -1,236 +1,235 @@
1
- # app.py
2
-
3
- import streamlit as st
4
- import cv2
5
- import numpy as np
6
- from PIL import Image
7
- from io import BytesIO
8
- import mediapipe as mp # NEW for background removal
9
-
10
- # ----- Custom CSS Styling -----
11
- def local_css():
12
- st.markdown("""
13
- <style>
14
- /* Main Background */
15
- .stApp {
16
- background-image: url('https://img.freepik.com/free-photo/arrows-pastel-colors_23-2148488400.jpg?semt=ais_hybrid&w=740');
17
- background-size: cover;
18
- background-position: center;
19
- background-attachment: fixed;
20
- min-height: 100vh;
21
- color: darkred;
22
- }
23
-
24
- /* Sidebar Background */
25
- section[data-testid="stSidebar"] {
26
- background-image: url('https://i.pinimg.com/736x/10/76/df/1076df6744238e75e79047f7c2d2bbec.jpg');
27
- background-size: cover;
28
- background-position: center;
29
- }
30
-
31
- /* Container Styling */
32
- .css-1d391kg {
33
- background: linear-gradient(135deg, #89f7fe 0%, #66a6ff 100%);
34
- border-radius: 15px;
35
- padding: 20px;
36
- }
37
-
38
- /* Button Styling */
39
- .stButton > button {
40
- color: white;
41
- background: linear-gradient(45deg, #ff6a00, #ee0979);
42
- border: none;
43
- border-radius: 10px;
44
- padding: 0.75em 1.5em;
45
- font-size: 1.1em;
46
- box-shadow: 0px 4px 15px rgba(0,0,0,0.2);
47
- }
48
- .stButton > button:hover {
49
- background: linear-gradient(45deg, #43cea2, #185a9d);
50
- }
51
-
52
- /* Profile Links Styling */
53
- .profile-links img {
54
- vertical-align: middle;
55
- margin-right: 8px;
56
- }
57
- .profile-links a {
58
- text-decoration: none;
59
- color: #333;
60
- font-size: 0.9em;
61
- }
62
- </style>
63
- """, unsafe_allow_html=True)
64
-
65
- def sidebar_profiles():
66
- st.sidebar.markdown("### 🎉Author: Maria Nadeem🌟")
67
- st.sidebar.markdown("### 🔗 Connect With Me")
68
- st.sidebar.markdown("""
69
- <hr>
70
- <div class="profile-links">
71
- <a href="https://github.com/marianadeem755" target="_blank">
72
- <img src="https://cdn-icons-png.flaticon.com/512/25/25231.png" width="20px"> GitHub
73
- </a><br><br>
74
- <a href="https://www.kaggle.com/marianadeem755" target="_blank">
75
- <img src="https://cdn4.iconfinder.com/data/icons/logos-and-brands/512/189_Kaggle_logo_logos-512.png" width="20px"> Kaggle
76
- </a><br><br>
77
- <a href="mailto:marianadeem755@gmail.com">
78
- <img src="https://cdn-icons-png.flaticon.com/512/561/561127.png" width="20px"> Email
79
- </a><br><br>
80
- <a href="https://huggingface.co/maria355" target="_blank">
81
- <img src="https://huggingface.co/front/assets/huggingface_logo-noborder.svg" width="20px"> Hugging Face
82
- </a>
83
- </div>
84
- <hr>
85
- """, unsafe_allow_html=True)
86
-
87
- # ----- Filters Functions -----
88
- def apply_grayscale(img):
89
- return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
90
-
91
- def apply_blur(img, ksize):
92
- return cv2.GaussianBlur(img, (ksize, ksize), 0)
93
-
94
- def apply_canny(img, threshold1, threshold2):
95
- return cv2.Canny(img, threshold1, threshold2)
96
-
97
- def apply_sepia(img):
98
- kernel = np.array([[0.272, 0.534, 0.131],
99
- [0.349, 0.686, 0.168],
100
- [0.393, 0.769, 0.189]])
101
- sepia_img = cv2.transform(img, kernel)
102
- sepia_img = np.clip(sepia_img, 0, 255)
103
- return sepia_img
104
-
105
- def apply_pencil_sketch(img):
106
- if len(img.shape) == 3:
107
- gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
108
- else:
109
- gray_img = img
110
- inv_img = 255 - gray_img
111
- blur_img = cv2.GaussianBlur(inv_img, (21, 21), 0)
112
- sketch = cv2.divide(gray_img, 255 - blur_img, scale=256)
113
- return sketch
114
-
115
- def apply_invert(img):
116
- return cv2.bitwise_not(img)
117
-
118
- def apply_background_removal(img):
119
- mp_selfie_segmentation = mp.solutions.selfie_segmentation
120
- with mp_selfie_segmentation.SelfieSegmentation(model_selection=1) as selfie_segmentation:
121
- rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
122
- results = selfie_segmentation.process(rgb_img)
123
- mask = results.segmentation_mask
124
- condition = mask > 0.5
125
- bg_color = np.ones(img.shape, dtype=np.uint8) * 255
126
- output_img = np.where(condition[..., None], img, bg_color)
127
- return output_img
128
-
129
- # Convert to PIL
130
- def convert_image(img):
131
- if len(img.shape) == 2:
132
- return Image.fromarray(img)
133
- else:
134
- return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
135
-
136
- # Downloadable image
137
- def download_image(img):
138
- buf = BytesIO()
139
- img.save(buf, format="PNG")
140
- byte_im = buf.getvalue()
141
- return byte_im
142
-
143
- # ----- Streamlit App Starts Here -----
144
- def main():
145
- st.set_page_config(page_title="Advanced Image Filter Studio", page_icon="🎨", layout="wide")
146
- local_css()
147
-
148
- st.title("🎨 Advanced Image Filter Studio")
149
- st.write("Upload an image, apply **amazing filters**, and download your creation!")
150
-
151
- # Sidebar Profiles
152
- sidebar_profiles()
153
-
154
- # Sidebar
155
- st.sidebar.header("1. Upload Image")
156
- uploaded_file = st.sidebar.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
157
-
158
- if uploaded_file is not None:
159
- file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
160
- opencv_image = cv2.imdecode(file_bytes, 1)
161
-
162
- # Filters Section
163
- st.sidebar.header("2. Choose Filters")
164
- grayscale = st.sidebar.checkbox("Grayscale")
165
- blur = st.sidebar.checkbox("Blur")
166
- canny = st.sidebar.checkbox("Edge Detection")
167
- sepia = st.sidebar.checkbox("Sepia Effect")
168
- sketch = st.sidebar.checkbox("Pencil Sketch")
169
- invert = st.sidebar.checkbox("Invert Colors")
170
- remove_bg = st.sidebar.checkbox("Remove Background (Simple)")
171
-
172
- st.sidebar.header("3. Filter Parameters")
173
- blur_strength = st.sidebar.slider("Blur Intensity (odd numbers)", 1, 49, 15, step=2)
174
- threshold1 = st.sidebar.slider("Canny Threshold1", 50, 300, 100)
175
- threshold2 = st.sidebar.slider("Canny Threshold2", 50, 300, 150)
176
-
177
- # Process Image
178
- final_image = opencv_image.copy()
179
-
180
- with st.spinner("🖌️ Applying Filters..."):
181
- if grayscale:
182
- final_image = apply_grayscale(final_image)
183
-
184
- if blur:
185
- if len(final_image.shape) == 2:
186
- final_image = cv2.cvtColor(final_image, cv2.COLOR_GRAY2BGR)
187
- final_image = apply_blur(final_image, blur_strength)
188
-
189
- if canny:
190
- if len(final_image.shape) != 2:
191
- final_image = cv2.cvtColor(final_image, cv2.COLOR_BGR2GRAY)
192
- final_image = apply_canny(final_image, threshold1, threshold2)
193
-
194
- if sepia:
195
- if len(final_image.shape) == 2:
196
- final_image = cv2.cvtColor(final_image, cv2.COLOR_GRAY2BGR)
197
- final_image = apply_sepia(final_image)
198
-
199
- if sketch:
200
- if len(final_image.shape) != 2:
201
- final_image = cv2.cvtColor(final_image, cv2.COLOR_BGR2GRAY)
202
- final_image = apply_pencil_sketch(final_image)
203
-
204
- if invert:
205
- final_image = apply_invert(final_image)
206
-
207
- if remove_bg:
208
- if len(final_image.shape) != 3 or final_image.shape[2] != 3:
209
- final_image = cv2.cvtColor(final_image, cv2.COLOR_GRAY2BGR)
210
- final_image = apply_background_removal(final_image)
211
-
212
- # Columns to show images
213
- col1, col2 = st.columns(2)
214
-
215
- with col1:
216
- st.subheader("Original Image")
217
- st.image(uploaded_file, use_column_width=True)
218
-
219
- with col2:
220
- st.subheader("Processed Image")
221
- final_pil = convert_image(final_image)
222
- st.image(final_pil, use_column_width=True)
223
-
224
- # Download button
225
- st.markdown("---")
226
- st.download_button(
227
- label="📥 Download Processed Image",
228
- data=download_image(final_pil),
229
- file_name="processed_image.png",
230
- mime="image/png"
231
- )
232
- else:
233
- st.info("👈 Please upload an image from the sidebar to get started.")
234
-
235
- if __name__ == "__main__":
236
- main()
 
1
+ # app.py
2
+ import streamlit as st
3
+ import cv2
4
+ import numpy as np
5
+ from PIL import Image
6
+ from io import BytesIO
7
+ import mediapipe as mp # NEW for background removal
8
+
9
+ # ----- Custom CSS Styling -----
10
+ def local_css():
11
+ st.markdown("""
12
+ <style>
13
+ /* Main Background */
14
+ .stApp {
15
+ background-image: url('https://img.freepik.com/free-photo/arrows-pastel-colors_23-2148488400.jpg?semt=ais_hybrid&w=740');
16
+ background-size: cover;
17
+ background-position: center;
18
+ background-attachment: fixed;
19
+ min-height: 100vh;
20
+ color: darkred;
21
+ }
22
+
23
+ /* Sidebar Background */
24
+ section[data-testid="stSidebar"] {
25
+ background-image: url('https://i.pinimg.com/736x/10/76/df/1076df6744238e75e79047f7c2d2bbec.jpg');
26
+ background-size: cover;
27
+ background-position: center;
28
+ }
29
+
30
+ /* Container Styling */
31
+ .css-1d391kg {
32
+ background: linear-gradient(135deg, #89f7fe 0%, #66a6ff 100%);
33
+ border-radius: 15px;
34
+ padding: 20px;
35
+ }
36
+
37
+ /* Button Styling */
38
+ .stButton > button {
39
+ color: white;
40
+ background: linear-gradient(45deg, #ff6a00, #ee0979);
41
+ border: none;
42
+ border-radius: 10px;
43
+ padding: 0.75em 1.5em;
44
+ font-size: 1.1em;
45
+ box-shadow: 0px 4px 15px rgba(0,0,0,0.2);
46
+ }
47
+ .stButton > button:hover {
48
+ background: linear-gradient(45deg, #43cea2, #185a9d);
49
+ }
50
+
51
+ /* Profile Links Styling */
52
+ .profile-links img {
53
+ vertical-align: middle;
54
+ margin-right: 8px;
55
+ }
56
+ .profile-links a {
57
+ text-decoration: none;
58
+ color: #333;
59
+ font-size: 0.9em;
60
+ }
61
+ </style>
62
+ """, unsafe_allow_html=True)
63
+
64
+ def sidebar_profiles():
65
+ st.sidebar.markdown("### 🎉Author: Maria Nadeem🌟")
66
+ st.sidebar.markdown("### 🔗 Connect With Me")
67
+ st.sidebar.markdown("""
68
+ <hr>
69
+ <div class="profile-links">
70
+ <a href="https://github.com/marianadeem755" target="_blank">
71
+ <img src="https://cdn-icons-png.flaticon.com/512/25/25231.png" width="20px"> GitHub
72
+ </a><br><br>
73
+ <a href="https://www.kaggle.com/marianadeem755" target="_blank">
74
+ <img src="https://cdn4.iconfinder.com/data/icons/logos-and-brands/512/189_Kaggle_logo_logos-512.png" width="20px"> Kaggle
75
+ </a><br><br>
76
+ <a href="mailto:marianadeem755@gmail.com">
77
+ <img src="https://cdn-icons-png.flaticon.com/512/561/561127.png" width="20px"> Email
78
+ </a><br><br>
79
+ <a href="https://huggingface.co/maria355" target="_blank">
80
+ <img src="https://huggingface.co/front/assets/huggingface_logo-noborder.svg" width="20px"> Hugging Face
81
+ </a>
82
+ </div>
83
+ <hr>
84
+ """, unsafe_allow_html=True)
85
+
86
+ # ----- Filters Functions -----
87
+ def apply_grayscale(img):
88
+ return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
89
+
90
+ def apply_blur(img, ksize):
91
+ return cv2.GaussianBlur(img, (ksize, ksize), 0)
92
+
93
+ def apply_canny(img, threshold1, threshold2):
94
+ return cv2.Canny(img, threshold1, threshold2)
95
+
96
+ def apply_sepia(img):
97
+ kernel = np.array([[0.272, 0.534, 0.131],
98
+ [0.349, 0.686, 0.168],
99
+ [0.393, 0.769, 0.189]])
100
+ sepia_img = cv2.transform(img, kernel)
101
+ sepia_img = np.clip(sepia_img, 0, 255)
102
+ return sepia_img
103
+
104
+ def apply_pencil_sketch(img):
105
+ if len(img.shape) == 3:
106
+ gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
107
+ else:
108
+ gray_img = img
109
+ inv_img = 255 - gray_img
110
+ blur_img = cv2.GaussianBlur(inv_img, (21, 21), 0)
111
+ sketch = cv2.divide(gray_img, 255 - blur_img, scale=256)
112
+ return sketch
113
+
114
+ def apply_invert(img):
115
+ return cv2.bitwise_not(img)
116
+
117
+ def apply_background_removal(img):
118
+ mp_selfie_segmentation = mp.solutions.selfie_segmentation
119
+ with mp_selfie_segmentation.SelfieSegmentation(model_selection=1) as selfie_segmentation:
120
+ rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
121
+ results = selfie_segmentation.process(rgb_img)
122
+ mask = results.segmentation_mask
123
+ condition = mask > 0.5
124
+ bg_color = np.ones(img.shape, dtype=np.uint8) * 255
125
+ output_img = np.where(condition[..., None], img, bg_color)
126
+ return output_img
127
+
128
+ # Convert to PIL
129
+ def convert_image(img):
130
+ if len(img.shape) == 2:
131
+ return Image.fromarray(img)
132
+ else:
133
+ return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
134
+
135
+ # Downloadable image
136
+ def download_image(img):
137
+ buf = BytesIO()
138
+ img.save(buf, format="PNG")
139
+ byte_im = buf.getvalue()
140
+ return byte_im
141
+
142
+ # ----- Streamlit App Starts Here -----
143
+ def main():
144
+ st.set_page_config(page_title="Advanced Image Filter Studio", page_icon="🎨", layout="wide")
145
+ local_css()
146
+
147
+ st.title("🎨 Advanced Image Filter Studio")
148
+ st.write("Upload an image, apply **amazing filters**, and download your creation!")
149
+
150
+ # Sidebar Profiles
151
+ sidebar_profiles()
152
+
153
+ # Sidebar
154
+ st.sidebar.header("1. Upload Image")
155
+ uploaded_file = st.sidebar.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
156
+
157
+ if uploaded_file is not None:
158
+ file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
159
+ opencv_image = cv2.imdecode(file_bytes, 1)
160
+
161
+ # Filters Section
162
+ st.sidebar.header("2. Choose Filters")
163
+ grayscale = st.sidebar.checkbox("Grayscale")
164
+ blur = st.sidebar.checkbox("Blur")
165
+ canny = st.sidebar.checkbox("Edge Detection")
166
+ sepia = st.sidebar.checkbox("Sepia Effect")
167
+ sketch = st.sidebar.checkbox("Pencil Sketch")
168
+ invert = st.sidebar.checkbox("Invert Colors")
169
+ remove_bg = st.sidebar.checkbox("Remove Background (Simple)")
170
+
171
+ st.sidebar.header("3. Filter Parameters")
172
+ blur_strength = st.sidebar.slider("Blur Intensity (odd numbers)", 1, 49, 15, step=2)
173
+ threshold1 = st.sidebar.slider("Canny Threshold1", 50, 300, 100)
174
+ threshold2 = st.sidebar.slider("Canny Threshold2", 50, 300, 150)
175
+
176
+ # Process Image
177
+ final_image = opencv_image.copy()
178
+
179
+ with st.spinner("🖌️ Applying Filters..."):
180
+ if grayscale:
181
+ final_image = apply_grayscale(final_image)
182
+
183
+ if blur:
184
+ if len(final_image.shape) == 2:
185
+ final_image = cv2.cvtColor(final_image, cv2.COLOR_GRAY2BGR)
186
+ final_image = apply_blur(final_image, blur_strength)
187
+
188
+ if canny:
189
+ if len(final_image.shape) != 2:
190
+ final_image = cv2.cvtColor(final_image, cv2.COLOR_BGR2GRAY)
191
+ final_image = apply_canny(final_image, threshold1, threshold2)
192
+
193
+ if sepia:
194
+ if len(final_image.shape) == 2:
195
+ final_image = cv2.cvtColor(final_image, cv2.COLOR_GRAY2BGR)
196
+ final_image = apply_sepia(final_image)
197
+
198
+ if sketch:
199
+ if len(final_image.shape) != 2:
200
+ final_image = cv2.cvtColor(final_image, cv2.COLOR_BGR2GRAY)
201
+ final_image = apply_pencil_sketch(final_image)
202
+
203
+ if invert:
204
+ final_image = apply_invert(final_image)
205
+
206
+ if remove_bg:
207
+ if len(final_image.shape) != 3 or final_image.shape[2] != 3:
208
+ final_image = cv2.cvtColor(final_image, cv2.COLOR_GRAY2BGR)
209
+ final_image = apply_background_removal(final_image)
210
+
211
+ # Columns to show images
212
+ col1, col2 = st.columns(2)
213
+
214
+ with col1:
215
+ st.subheader("Original Image")
216
+ st.image(uploaded_file, use_column_width=True)
217
+
218
+ with col2:
219
+ st.subheader("Processed Image")
220
+ final_pil = convert_image(final_image)
221
+ st.image(final_pil, use_column_width=True)
222
+
223
+ # Download button
224
+ st.markdown("---")
225
+ st.download_button(
226
+ label="📥 Download Processed Image",
227
+ data=download_image(final_pil),
228
+ file_name="processed_image.png",
229
+ mime="image/png"
230
+ )
231
+ else:
232
+ st.info("👈 Please upload an image from the sidebar to get started.")
233
+
234
+ if __name__ == "__main__":
235
+ main()