Spaces:
Sleeping
Sleeping
File size: 7,407 Bytes
7f54ded 571f667 7f54ded 571f667 7f54ded 571f667 7f54ded 571f667 7f54ded 571f667 7f54ded 571f667 7f54ded 571f667 7f54ded 571f667 7f54ded 571f667 7f54ded 571f667 7f54ded 571f667 7f54ded 312e4f2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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 136 137 138 139 140 141 142 143 144 145 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 |
import cv2
import numpy as np
import gradio as gr
def apply_retro_filter(frame):
retro_filter = np.array([[0.393, 0.769, 0.189],
[0.349, 0.686, 0.168],
[0.272, 0.534, 0.131]])
return cv2.transform(frame, retro_filter)
def apply_cartoon_filter(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 10)
color = cv2.bilateralFilter(frame, 9, 250, 250)
cartoon = cv2.bitwise_and(color, color, mask=edges)
return cartoon
def apply_oil_painting_filter(frame):
oil_painting = cv2.edgePreservingFilter(frame, flags=2, sigma_s=50, sigma_r=0.4)
return oil_painting
def apply_emboss_filter(frame):
kernel = np.array([[0, -1, -1],
[1, 0, -1],
[1, 1, 0]])
emboss = cv2.filter2D(frame, -1, kernel)
return emboss
def adjust_brightness_contrast(frame, alpha=1.2, beta=30):
return cv2.convertScaleAbs(frame, alpha=alpha, beta=beta)
def apply_pencil_drawing_filter(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
inverted = cv2.bitwise_not(gray)
blurred = cv2.GaussianBlur(inverted, (21, 21), 0)
inverted_blurred = cv2.bitwise_not(blurred)
pencil_drawing = cv2.divide(gray, inverted_blurred, scale=256.0)
return pencil_drawing
def apply_pastel_filter(frame):
blurred = cv2.GaussianBlur(frame, (15, 15), 0)
pastel_effect = cv2.addWeighted(frame, 0.5, blurred, 0.5, 0)
return pastel_effect
def apply_hdr_filter(frame):
hdr = cv2.detailEnhance(frame, sigma_s=12, sigma_r=0.15)
return hdr
def apply_summer_filter(frame):
summer_filter = np.array([[0.272, 0.534, 0.131],
[0.349, 0.686, 0.168],
[0.393, 0.769, 0.189]])
summer = cv2.transform(frame, summer_filter)
return summer
def apply_winter_filter(frame):
winter_filter = np.array([[0.272, 0.534, 0.769],
[0.349, 0.686, 0.534],
[0.393, 0.131, 0.272]])
winter = cv2.transform(frame, winter_filter)
return winter
def apply_glitch_filter(frame):
rows, cols, _ = frame.shape
red_channel = frame[:, :, 0]
green_channel = frame[:, :, 1]
blue_channel = frame[:, :, 2]
red_channel_shifted = np.roll(red_channel, 5, axis=0)
green_channel_shifted = np.roll(green_channel, -5, axis=0)
glitched_frame = np.zeros_like(frame)
glitched_frame[:, :, 0] = red_channel_shifted
glitched_frame[:, :, 1] = green_channel_shifted
glitched_frame[:, :, 2] = blue_channel
num_segments = np.random.randint(5, 10)
for _ in range(num_segments):
y_start = np.random.randint(0, rows)
y_end = min(y_start + np.random.randint(5, 20), rows)
x_start = np.random.randint(0, cols)
x_end = min(x_start + np.random.randint(5, 20), cols)
shift_value = np.random.randint(-10, 10)
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)
return glitched_frame
def apply_glass_shatter_filter(frame):
rows, cols, _ = frame.shape
glitched_frame = frame.copy()
num_segments = np.random.randint(5, 10)
segment_width = cols // num_segments
for i in range(num_segments):
x_start = i * segment_width
x_end = (i + 1) * segment_width if i < num_segments - 1 else cols
shift_direction = np.random.choice([-1, 1])
shift_amount = np.random.randint(5, 20)
glitched_segment = np.roll(glitched_frame[:, x_start:x_end], shift_amount * shift_direction, axis=0)
glitched_frame[:, x_start:x_end] = glitched_segment
return glitched_frame
def apply_grayscale_filter(frame):
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
def apply_filter(filter_type, input_image=None):
frame = input_image.copy() if input_image is not None else None
if frame is None:
return "Görüntü bulunamadı!"
if filter_type == "Retro":
return apply_retro_filter(frame)
elif filter_type == "Cartoon":
return apply_cartoon_filter(frame)
elif filter_type == "Oil Painting":
return apply_oil_painting_filter(frame)
elif filter_type == "Emboss":
return apply_emboss_filter(frame)
elif filter_type == "Brightness & Contrast":
return adjust_brightness_contrast(frame, alpha=1.2, beta=30)
elif filter_type == "Pencil Drawing":
return apply_pencil_drawing_filter(frame)
elif filter_type == "Pastel":
return apply_pastel_filter(frame)
elif filter_type == "HDR":
return apply_hdr_filter(frame)
elif filter_type == "Summer":
return apply_summer_filter(frame)
elif filter_type == "Winter":
return apply_winter_filter(frame)
elif filter_type == "Glitch":
return apply_glitch_filter(frame)
elif filter_type == "Glass Shatter":
return apply_glass_shatter_filter(frame)
elif filter_type == "Grayscale":
return apply_grayscale_filter(frame)
def reset_images():
"""Yüklenen ve filtreli görüntüleri temizlemek için kullanılır."""
return None, None
# Gradio arayüz tanımlamaları
with gr.Blocks(css="""
.main { background-color: #1f1f1f; }
.gradio-container { background-color: #1f1f1f; }
.custom-button {
background-color: #00163a;
color: white;
border-radius: 10px;
border: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
.custom-button:hover {
background-color: #001c4d;
}
#header {
text-align: center;
color: #ffffff;
}
body {
background-color: #000f26;
}
#input-image, #output-image {
background-color: #000f26;
padding: 20px;
border-radius: 10px;
}
#filter-dropdown {
background-color: #00163a; /* Burada arka plan rengini değiştirdik */
color: #00ccff;
border: 2px solid #00ccff;
padding: 10px;
border-radius: 10px;
font-size: 16px;
text-align: center;
}
""") as demo:
gr.Markdown("<h1 id='header'>Görüntü Filtreleri Uygulaması</h1>")
filter_type = gr.Dropdown(
label="Filtre Seçin",
choices=["Retro", "Grayscale", "Cartoon", "Oil Painting", "Emboss", "Brightness & Contrast",
"Pencil Drawing", "Pastel", "HDR", "Summer", "Winter", "Glass Shatter", "Glitch"],
value="Retro",
elem_id="filter-dropdown"
)
with gr.Row():
input_image = gr.Image(label="Resim Yükle", type="numpy", elem_id="input-image", width=300, height=300)
output_image = gr.Image(label="Filtre Uygulandı", elem_id="output-image", width=300, height=300)
with gr.Row():
apply_button = gr.Button("Filtreyi Uygula", elem_id="apply-button", elem_classes="custom-button")
reset_button = gr.Button("Sıfırla", elem_id="reset-button", elem_classes="custom-button")
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
reset_button.click(fn=reset_images, inputs=[], outputs=[input_image, output_image])
demo.launch(share = True)
|