maria355 commited on
Commit
db2ac8f
·
verified ·
1 Parent(s): f02f930

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +236 -0
  2. packages.txt +2 -0
  3. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()
packages.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ libgl1
2
+ libglib2.0-0
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ opencv-python
3
+ Pillow
4
+ numpy
5
+ mediapipe