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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 |
+
# Renk kanallarını kaydırma
|
68 |
+
red_channel = frame[:, :, 0]
|
69 |
+
green_channel = frame[:, :, 1]
|
70 |
+
blue_channel = frame[:, :, 2]
|
71 |
+
|
72 |
+
# Renk kaydırma etkisi
|
73 |
+
red_channel_shifted = np.roll(red_channel, 5, axis=0)
|
74 |
+
green_channel_shifted = np.roll(green_channel, -5, axis=0)
|
75 |
+
|
76 |
+
# Yeni görüntüyü oluşturma
|
77 |
+
glitched_frame = np.zeros_like(frame)
|
78 |
+
glitched_frame[:, :, 0] = red_channel_shifted
|
79 |
+
glitched_frame[:, :, 1] = green_channel_shifted
|
80 |
+
glitched_frame[:, :, 2] = blue_channel
|
81 |
+
|
82 |
+
# Görüntüyü rastgele parçalara bölme
|
83 |
+
num_segments = np.random.randint(5, 10) # Rastgele 5-10 segment
|
84 |
+
for _ in range(num_segments):
|
85 |
+
y_start = np.random.randint(0, rows)
|
86 |
+
y_end = min(y_start + np.random.randint(5, 20), rows) # Segment yüksekliği
|
87 |
+
x_start = np.random.randint(0, cols)
|
88 |
+
x_end = min(x_start + np.random.randint(5, 20), cols) # Segment genişliği
|
89 |
+
|
90 |
+
# Parçanın bir kısmını kaydırma
|
91 |
+
shift_value = np.random.randint(-10, 10)
|
92 |
+
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)
|
93 |
+
|
94 |
+
return glitched_frame
|
95 |
+
|
96 |
+
def apply_glass_shatter_filter(frame):
|
97 |
+
rows, cols, _ = frame.shape
|
98 |
+
glitched_frame = frame.copy()
|
99 |
+
|
100 |
+
# Rastgele segmentler oluşturma
|
101 |
+
num_segments = np.random.randint(5, 10) # 5-10 segment
|
102 |
+
segment_width = cols // num_segments # Her segmentin genişliği
|
103 |
+
|
104 |
+
for i in range(num_segments):
|
105 |
+
# Segmentin konumu
|
106 |
+
x_start = i * segment_width
|
107 |
+
x_end = (i + 1) * segment_width if i < num_segments - 1 else cols
|
108 |
+
|
109 |
+
# Segmenti yukarı veya aşağı kaydırma
|
110 |
+
shift_direction = np.random.choice([-1, 1]) # Yukarı veya aşağı kaydır
|
111 |
+
shift_amount = np.random.randint(5, 20) # 5-20 piksel kaydırma miktarı
|
112 |
+
|
113 |
+
# Segmenti kaydır
|
114 |
+
glitched_segment = np.roll(glitched_frame[:, x_start:x_end], shift_amount * shift_direction, axis=0)
|
115 |
+
|
116 |
+
# Segmenti orijinal çerçeveye yerleştir
|
117 |
+
glitched_frame[:, x_start:x_end] = glitched_segment
|
118 |
+
|
119 |
+
# Ekstra gürültü ekleme
|
120 |
+
#noise = np.random.randint(0, 50, frame.shape, dtype=np.uint8) # Gürültü oluşturma
|
121 |
+
#glitched_frame = cv2.add(glitched_frame, noise) # Gürültüyü ekleme
|
122 |
+
|
123 |
+
return glitched_frame
|
124 |
+
|
125 |
+
|
126 |
+
def apply_grayscale_filter(frame):
|
127 |
+
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
128 |
+
|
129 |
+
def apply_filter(filter_type, input_image=None):
|
130 |
+
frame = input_image.copy() if input_image is not None else None
|
131 |
+
if frame is None:
|
132 |
+
return "Görüntü bulunamadı!"
|
133 |
+
|
134 |
+
if filter_type == "Retro":
|
135 |
+
return apply_retro_filter(frame)
|
136 |
+
elif filter_type == "Cartoon":
|
137 |
+
return apply_cartoon_filter(frame)
|
138 |
+
elif filter_type == "Oil Painting":
|
139 |
+
return apply_oil_painting_filter(frame)
|
140 |
+
elif filter_type == "Emboss":
|
141 |
+
return apply_emboss_filter(frame)
|
142 |
+
elif filter_type == "Brightness & Contrast":
|
143 |
+
return adjust_brightness_contrast(frame, alpha=1.2, beta=30)
|
144 |
+
elif filter_type == "Pencil Drawing":
|
145 |
+
return apply_pencil_drawing_filter(frame)
|
146 |
+
elif filter_type == "Pastel":
|
147 |
+
return apply_pastel_filter(frame)
|
148 |
+
elif filter_type == "HDR":
|
149 |
+
return apply_hdr_filter(frame)
|
150 |
+
elif filter_type == "Summer":
|
151 |
+
return apply_summer_filter(frame)
|
152 |
+
elif filter_type == "Winter":
|
153 |
+
return apply_winter_filter(frame)
|
154 |
+
elif filter_type == "Glitch":
|
155 |
+
return apply_glitch_filter(frame)
|
156 |
+
elif filter_type == "Glass Shatter":
|
157 |
+
return apply_glass_shatter_filter(frame)
|
158 |
+
elif filter_type == "Grayscale":
|
159 |
+
return apply_grayscale_filter(frame)
|
160 |
+
|
161 |
+
with gr.Blocks() as demo:
|
162 |
+
gr.Markdown("# Görüntü Filtreleri Uygulaması")
|
163 |
+
|
164 |
+
filter_type = gr.Dropdown(
|
165 |
+
label="Filtre Seçimi:",
|
166 |
+
choices=["Retro", "Grayscale", "Cartoon", "Oil Painting", "Emboss", "Brightness & Contrast",
|
167 |
+
"Pencil Drawing", "Pastel", "HDR", "Summer", "Winter", "Glass Shatter", "Glitch" ],
|
168 |
+
value="Retro"
|
169 |
+
)
|
170 |
+
|
171 |
+
input_image = gr.Image(label="Resim Yükle", type="numpy")
|
172 |
+
output_image = gr.Image(label="Filtre Uygulandı")
|
173 |
+
|
174 |
+
apply_button = gr.Button("Filtreyi Uygula")
|
175 |
+
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
|
176 |
+
|
177 |
+
|
178 |
+
with gr.Blocks(css="""
|
179 |
+
.main { background-color: #1f1f1f; }
|
180 |
+
.block { background-color: #1f1f1f; }
|
181 |
+
.gradio-container { background-color: #1f1f1f; }
|
182 |
+
.custom-button {
|
183 |
+
background-color: #00163a;
|
184 |
+
color: white;
|
185 |
+
border-radius: 10px;
|
186 |
+
border: none;
|
187 |
+
padding: 10px 20px;
|
188 |
+
font-size: 16px;
|
189 |
+
cursor: pointer;
|
190 |
+
}
|
191 |
+
.custom-button:hover {
|
192 |
+
background-color: #001c4d;
|
193 |
+
}
|
194 |
+
#header {
|
195 |
+
text-align: center;
|
196 |
+
color: #ffffff; /* Başlık rengi beyaz */
|
197 |
+
}
|
198 |
+
body {
|
199 |
+
background-color: #000f26; /* Daha koyu lacivert arka plan rengi */
|
200 |
+
}
|
201 |
+
#input-image {
|
202 |
+
background-color: #000f26; /* Koyu mavi arka plan rengi */
|
203 |
+
padding: 20px;
|
204 |
+
border-radius: 10px;
|
205 |
+
}
|
206 |
+
#output-image {
|
207 |
+
background-color: #000f26; /* Koyu mavi arka plan rengi */
|
208 |
+
padding: 20px;
|
209 |
+
border-radius: 10px;
|
210 |
+
}
|
211 |
+
#filter-dropdown {
|
212 |
+
background-color: #000f26; /* Filtre seçin arka plan rengi */
|
213 |
+
color: white; /* Yazı rengi beyaz */
|
214 |
+
border: none;
|
215 |
+
padding: 10px;
|
216 |
+
border-radius: 10px;
|
217 |
+
}
|
218 |
+
""") as demo:
|
219 |
+
gr.Markdown("<h1 id='header'>Görüntü Filtreleri Uygulaması</h1>")
|
220 |
+
|
221 |
+
filter_type = gr.Dropdown(
|
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()
|