Spaces:
Sleeping
Sleeping
alifuatkurt
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,235 +1,221 @@
|
|
1 |
-
import cv2
|
2 |
-
import numpy as np
|
3 |
-
import gradio as gr
|
4 |
-
|
5 |
-
def apply_retro_filter(frame):
|
6 |
-
retro_filter = np.array([[0.393, 0.769, 0.189],
|
7 |
-
[0.349, 0.686, 0.168],
|
8 |
-
[0.272, 0.534, 0.131]])
|
9 |
-
return cv2.transform(frame, retro_filter)
|
10 |
-
|
11 |
-
def apply_cartoon_filter(frame):
|
12 |
-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
13 |
-
gray = cv2.medianBlur(gray, 5)
|
14 |
-
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 10)
|
15 |
-
color = cv2.bilateralFilter(frame, 9, 250, 250)
|
16 |
-
cartoon = cv2.bitwise_and(color, color, mask=edges)
|
17 |
-
return cartoon
|
18 |
-
|
19 |
-
def apply_oil_painting_filter(frame):
|
20 |
-
oil_painting = cv2.edgePreservingFilter(frame, flags=2, sigma_s=50, sigma_r=0.4)
|
21 |
-
return oil_painting
|
22 |
-
|
23 |
-
def apply_emboss_filter(frame):
|
24 |
-
kernel = np.array([[0, -1, -1],
|
25 |
-
[1, 0, -1],
|
26 |
-
[1, 1, 0]])
|
27 |
-
emboss = cv2.filter2D(frame, -1, kernel)
|
28 |
-
return emboss
|
29 |
-
|
30 |
-
def adjust_brightness_contrast(frame, alpha=1.2, beta=30):
|
31 |
-
return cv2.convertScaleAbs(frame, alpha=alpha, beta=beta)
|
32 |
-
|
33 |
-
def apply_pencil_drawing_filter(frame):
|
34 |
-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
35 |
-
inverted = cv2.bitwise_not(gray)
|
36 |
-
blurred = cv2.GaussianBlur(inverted, (21, 21), 0)
|
37 |
-
inverted_blurred = cv2.bitwise_not(blurred)
|
38 |
-
pencil_drawing = cv2.divide(gray, inverted_blurred, scale=256.0)
|
39 |
-
return pencil_drawing
|
40 |
-
|
41 |
-
def apply_pastel_filter(frame):
|
42 |
-
blurred = cv2.GaussianBlur(frame, (15, 15), 0)
|
43 |
-
pastel_effect = cv2.addWeighted(frame, 0.5, blurred, 0.5, 0)
|
44 |
-
return pastel_effect
|
45 |
-
|
46 |
-
def apply_hdr_filter(frame):
|
47 |
-
hdr = cv2.detailEnhance(frame, sigma_s=12, sigma_r=0.15)
|
48 |
-
return hdr
|
49 |
-
|
50 |
-
def apply_summer_filter(frame):
|
51 |
-
summer_filter = np.array([[0.272, 0.534, 0.131],
|
52 |
-
[0.349, 0.686, 0.168],
|
53 |
-
[0.393, 0.769, 0.189]])
|
54 |
-
summer = cv2.transform(frame, summer_filter)
|
55 |
-
return summer
|
56 |
-
|
57 |
-
def apply_winter_filter(frame):
|
58 |
-
winter_filter = np.array([[0.272, 0.534, 0.769],
|
59 |
-
[0.349, 0.686, 0.534],
|
60 |
-
[0.393, 0.131, 0.272]])
|
61 |
-
winter = cv2.transform(frame, winter_filter)
|
62 |
-
return winter
|
63 |
-
|
64 |
-
def apply_glitch_filter(frame):
|
65 |
-
rows, cols, _ = frame.shape
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
glitched_frame =
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
x_start = np.
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
x_end =
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
return
|
136 |
-
elif filter_type == "
|
137 |
-
return
|
138 |
-
elif filter_type == "
|
139 |
-
return
|
140 |
-
elif filter_type == "
|
141 |
-
return
|
142 |
-
elif filter_type == "
|
143 |
-
return
|
144 |
-
elif filter_type == "
|
145 |
-
return
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
""")
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
label="Filtre Seçin",
|
223 |
-
choices=["Retro", "Grayscale", "Cartoon", "Oil Painting", "Emboss", "Brightness & Contrast",
|
224 |
-
"Pencil Drawing","Pastel", "HDR", "Summer", "Winter", "Glass Shatter", "Glitch"],
|
225 |
-
value="Retro",
|
226 |
-
elem_id="filter-dropdown"
|
227 |
-
)
|
228 |
-
|
229 |
-
input_image = gr.Image(label="Resim Yükle", type="numpy", elem_id="input-image")
|
230 |
-
output_image = gr.Image(label="Filtre Uygulandı", elem_id="output-image")
|
231 |
-
|
232 |
-
apply_button = gr.Button("Filtreyi Uygula", elem_id="apply-button", elem_classes="custom-button")
|
233 |
-
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
|
234 |
-
|
235 |
-
demo.launch()
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
def apply_retro_filter(frame):
|
6 |
+
retro_filter = np.array([[0.393, 0.769, 0.189],
|
7 |
+
[0.349, 0.686, 0.168],
|
8 |
+
[0.272, 0.534, 0.131]])
|
9 |
+
return cv2.transform(frame, retro_filter)
|
10 |
+
|
11 |
+
def apply_cartoon_filter(frame):
|
12 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
13 |
+
gray = cv2.medianBlur(gray, 5)
|
14 |
+
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 10)
|
15 |
+
color = cv2.bilateralFilter(frame, 9, 250, 250)
|
16 |
+
cartoon = cv2.bitwise_and(color, color, mask=edges)
|
17 |
+
return cartoon
|
18 |
+
|
19 |
+
def apply_oil_painting_filter(frame):
|
20 |
+
oil_painting = cv2.edgePreservingFilter(frame, flags=2, sigma_s=50, sigma_r=0.4)
|
21 |
+
return oil_painting
|
22 |
+
|
23 |
+
def apply_emboss_filter(frame):
|
24 |
+
kernel = np.array([[0, -1, -1],
|
25 |
+
[1, 0, -1],
|
26 |
+
[1, 1, 0]])
|
27 |
+
emboss = cv2.filter2D(frame, -1, kernel)
|
28 |
+
return emboss
|
29 |
+
|
30 |
+
def adjust_brightness_contrast(frame, alpha=1.2, beta=30):
|
31 |
+
return cv2.convertScaleAbs(frame, alpha=alpha, beta=beta)
|
32 |
+
|
33 |
+
def apply_pencil_drawing_filter(frame):
|
34 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
35 |
+
inverted = cv2.bitwise_not(gray)
|
36 |
+
blurred = cv2.GaussianBlur(inverted, (21, 21), 0)
|
37 |
+
inverted_blurred = cv2.bitwise_not(blurred)
|
38 |
+
pencil_drawing = cv2.divide(gray, inverted_blurred, scale=256.0)
|
39 |
+
return pencil_drawing
|
40 |
+
|
41 |
+
def apply_pastel_filter(frame):
|
42 |
+
blurred = cv2.GaussianBlur(frame, (15, 15), 0)
|
43 |
+
pastel_effect = cv2.addWeighted(frame, 0.5, blurred, 0.5, 0)
|
44 |
+
return pastel_effect
|
45 |
+
|
46 |
+
def apply_hdr_filter(frame):
|
47 |
+
hdr = cv2.detailEnhance(frame, sigma_s=12, sigma_r=0.15)
|
48 |
+
return hdr
|
49 |
+
|
50 |
+
def apply_summer_filter(frame):
|
51 |
+
summer_filter = np.array([[0.272, 0.534, 0.131],
|
52 |
+
[0.349, 0.686, 0.168],
|
53 |
+
[0.393, 0.769, 0.189]])
|
54 |
+
summer = cv2.transform(frame, summer_filter)
|
55 |
+
return summer
|
56 |
+
|
57 |
+
def apply_winter_filter(frame):
|
58 |
+
winter_filter = np.array([[0.272, 0.534, 0.769],
|
59 |
+
[0.349, 0.686, 0.534],
|
60 |
+
[0.393, 0.131, 0.272]])
|
61 |
+
winter = cv2.transform(frame, winter_filter)
|
62 |
+
return winter
|
63 |
+
|
64 |
+
def apply_glitch_filter(frame):
|
65 |
+
rows, cols, _ = frame.shape
|
66 |
+
|
67 |
+
red_channel = frame[:, :, 0]
|
68 |
+
green_channel = frame[:, :, 1]
|
69 |
+
blue_channel = frame[:, :, 2]
|
70 |
+
|
71 |
+
red_channel_shifted = np.roll(red_channel, 5, axis=0)
|
72 |
+
green_channel_shifted = np.roll(green_channel, -5, axis=0)
|
73 |
+
|
74 |
+
glitched_frame = np.zeros_like(frame)
|
75 |
+
glitched_frame[:, :, 0] = red_channel_shifted
|
76 |
+
glitched_frame[:, :, 1] = green_channel_shifted
|
77 |
+
glitched_frame[:, :, 2] = blue_channel
|
78 |
+
|
79 |
+
num_segments = np.random.randint(5, 10)
|
80 |
+
for _ in range(num_segments):
|
81 |
+
y_start = np.random.randint(0, rows)
|
82 |
+
y_end = min(y_start + np.random.randint(5, 20), rows)
|
83 |
+
x_start = np.random.randint(0, cols)
|
84 |
+
x_end = min(x_start + np.random.randint(5, 20), cols)
|
85 |
+
|
86 |
+
shift_value = np.random.randint(-10, 10)
|
87 |
+
glitched_frame[y_start:y_end, x_start:x_end] = np.roll(glitched_frame[y_start:y_end, x_start:x_end], shift_value, axis=1)
|
88 |
+
|
89 |
+
return glitched_frame
|
90 |
+
|
91 |
+
def apply_glass_shatter_filter(frame):
|
92 |
+
rows, cols, _ = frame.shape
|
93 |
+
glitched_frame = frame.copy()
|
94 |
+
|
95 |
+
num_segments = np.random.randint(5, 10)
|
96 |
+
segment_width = cols // num_segments
|
97 |
+
|
98 |
+
for i in range(num_segments):
|
99 |
+
x_start = i * segment_width
|
100 |
+
x_end = (i + 1) * segment_width if i < num_segments - 1 else cols
|
101 |
+
|
102 |
+
shift_direction = np.random.choice([-1, 1])
|
103 |
+
shift_amount = np.random.randint(5, 20)
|
104 |
+
|
105 |
+
glitched_segment = np.roll(glitched_frame[:, x_start:x_end], shift_amount * shift_direction, axis=0)
|
106 |
+
|
107 |
+
glitched_frame[:, x_start:x_end] = glitched_segment
|
108 |
+
|
109 |
+
return glitched_frame
|
110 |
+
|
111 |
+
|
112 |
+
def apply_grayscale_filter(frame):
|
113 |
+
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
114 |
+
|
115 |
+
def apply_filter(filter_type, input_image=None):
|
116 |
+
frame = input_image.copy() if input_image is not None else None
|
117 |
+
if frame is None:
|
118 |
+
return "Görüntü bulunamadı!"
|
119 |
+
|
120 |
+
if filter_type == "Retro":
|
121 |
+
return apply_retro_filter(frame)
|
122 |
+
elif filter_type == "Cartoon":
|
123 |
+
return apply_cartoon_filter(frame)
|
124 |
+
elif filter_type == "Oil Painting":
|
125 |
+
return apply_oil_painting_filter(frame)
|
126 |
+
elif filter_type == "Emboss":
|
127 |
+
return apply_emboss_filter(frame)
|
128 |
+
elif filter_type == "Brightness & Contrast":
|
129 |
+
return adjust_brightness_contrast(frame, alpha=1.2, beta=30)
|
130 |
+
elif filter_type == "Pencil Drawing":
|
131 |
+
return apply_pencil_drawing_filter(frame)
|
132 |
+
elif filter_type == "Pastel":
|
133 |
+
return apply_pastel_filter(frame)
|
134 |
+
elif filter_type == "HDR":
|
135 |
+
return apply_hdr_filter(frame)
|
136 |
+
elif filter_type == "Summer":
|
137 |
+
return apply_summer_filter(frame)
|
138 |
+
elif filter_type == "Winter":
|
139 |
+
return apply_winter_filter(frame)
|
140 |
+
elif filter_type == "Glitch":
|
141 |
+
return apply_glitch_filter(frame)
|
142 |
+
elif filter_type == "Glass Shatter":
|
143 |
+
return apply_glass_shatter_filter(frame)
|
144 |
+
elif filter_type == "Grayscale":
|
145 |
+
return apply_grayscale_filter(frame)
|
146 |
+
|
147 |
+
with gr.Blocks() as demo:
|
148 |
+
gr.Markdown("# Görüntü Filtreleri Uygulaması")
|
149 |
+
|
150 |
+
filter_type = gr.Dropdown(
|
151 |
+
label="Filtre Seçimi:",
|
152 |
+
choices=["Retro", "Grayscale", "Cartoon", "Oil Painting", "Emboss", "Brightness & Contrast",
|
153 |
+
"Pencil Drawing", "Pastel", "HDR", "Summer", "Winter", "Glass Shatter", "Glitch" ],
|
154 |
+
value="Retro"
|
155 |
+
)
|
156 |
+
|
157 |
+
input_image = gr.Image(label="Resim Yükle", type="numpy")
|
158 |
+
output_image = gr.Image(label="Filtre Uygulandı")
|
159 |
+
|
160 |
+
apply_button = gr.Button("Filtreyi Uygula")
|
161 |
+
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
|
162 |
+
|
163 |
+
|
164 |
+
with gr.Blocks(css="""
|
165 |
+
.main { background-color: #1f1f1f; }
|
166 |
+
.block { background-color: #1f1f1f; }
|
167 |
+
.gradio-container { background-color: #1f1f1f; }
|
168 |
+
.custom-button {
|
169 |
+
background-color: #00163a;
|
170 |
+
color: white;
|
171 |
+
border-radius: 10px;
|
172 |
+
border: none;
|
173 |
+
padding: 10px 20px;
|
174 |
+
font-size: 16px;
|
175 |
+
cursor: pointer;
|
176 |
+
}
|
177 |
+
.custom-button:hover {
|
178 |
+
background-color: #001c4d;
|
179 |
+
}
|
180 |
+
#header {
|
181 |
+
text-align: center;
|
182 |
+
color: #ffffff; /* Başlık rengi beyaz */
|
183 |
+
}
|
184 |
+
body {
|
185 |
+
background-color: #000f26; /* Daha koyu lacivert arka plan rengi */
|
186 |
+
}
|
187 |
+
#input-image {
|
188 |
+
background-color: #000f26; /* Koyu mavi arka plan rengi */
|
189 |
+
padding: 20px;
|
190 |
+
border-radius: 10px;
|
191 |
+
}
|
192 |
+
#output-image {
|
193 |
+
background-color: #000f26; /* Koyu mavi arka plan rengi */
|
194 |
+
padding: 20px;
|
195 |
+
border-radius: 10px;
|
196 |
+
}
|
197 |
+
#filter-dropdown {
|
198 |
+
background-color: #000f26; /* Filtre seçin arka plan rengi */
|
199 |
+
color: white; /* Yazı rengi beyaz */
|
200 |
+
border: none;
|
201 |
+
padding: 10px;
|
202 |
+
border-radius: 10px;
|
203 |
+
}
|
204 |
+
""") as demo:
|
205 |
+
gr.Markdown("<h1 id='header'>Görüntü Filtreleri Uygulaması</h1>")
|
206 |
+
|
207 |
+
filter_type = gr.Dropdown(
|
208 |
+
label="Filtre Seçin",
|
209 |
+
choices=["Retro", "Grayscale", "Cartoon", "Oil Painting", "Emboss", "Brightness & Contrast",
|
210 |
+
"Pencil Drawing","Pastel", "HDR", "Summer", "Winter", "Glass Shatter", "Glitch"],
|
211 |
+
value="Retro",
|
212 |
+
elem_id="filter-dropdown"
|
213 |
+
)
|
214 |
+
|
215 |
+
input_image = gr.Image(label="Resim Yükle", type="numpy", elem_id="input-image")
|
216 |
+
output_image = gr.Image(label="Filtre Uygulandı", elem_id="output-image")
|
217 |
+
|
218 |
+
apply_button = gr.Button("Filtreyi Uygula", elem_id="apply-button", elem_classes="custom-button")
|
219 |
+
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
|
220 |
+
|
221 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|