alifuatkurt's picture
Upload app.py
94d704c verified
raw
history blame
8.81 kB
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
# Renk kanallarını kaydırma
red_channel = frame[:, :, 0]
green_channel = frame[:, :, 1]
blue_channel = frame[:, :, 2]
# Renk kaydırma etkisi
red_channel_shifted = np.roll(red_channel, 5, axis=0)
green_channel_shifted = np.roll(green_channel, -5, axis=0)
# Yeni görüntüyü oluşturma
glitched_frame = np.zeros_like(frame)
glitched_frame[:, :, 0] = red_channel_shifted
glitched_frame[:, :, 1] = green_channel_shifted
glitched_frame[:, :, 2] = blue_channel
# Görüntüyü rastgele parçalara bölme
num_segments = np.random.randint(5, 10) # Rastgele 5-10 segment
for _ in range(num_segments):
y_start = np.random.randint(0, rows)
y_end = min(y_start + np.random.randint(5, 20), rows) # Segment yüksekliği
x_start = np.random.randint(0, cols)
x_end = min(x_start + np.random.randint(5, 20), cols) # Segment genişliği
# Parçanın bir kısmını kaydırma
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()
# Rastgele segmentler oluşturma
num_segments = np.random.randint(5, 10) # 5-10 segment
segment_width = cols // num_segments # Her segmentin genişliği
for i in range(num_segments):
# Segmentin konumu
x_start = i * segment_width
x_end = (i + 1) * segment_width if i < num_segments - 1 else cols
# Segmenti yukarı veya aşağı kaydırma
shift_direction = np.random.choice([-1, 1]) # Yukarı veya aşağı kaydır
shift_amount = np.random.randint(5, 20) # 5-20 piksel kaydırma miktarı
# Segmenti kaydır
glitched_segment = np.roll(glitched_frame[:, x_start:x_end], shift_amount * shift_direction, axis=0)
# Segmenti orijinal çerçeveye yerleştir
glitched_frame[:, x_start:x_end] = glitched_segment
# Ekstra gürültü ekleme
#noise = np.random.randint(0, 50, frame.shape, dtype=np.uint8) # Gürültü oluşturma
#glitched_frame = cv2.add(glitched_frame, noise) # Gürültüyü ekleme
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)
with gr.Blocks() as demo:
gr.Markdown("# Görüntü Filtreleri Uygulaması")
filter_type = gr.Dropdown(
label="Filtre Seçimi:",
choices=["Retro", "Grayscale", "Cartoon", "Oil Painting", "Emboss", "Brightness & Contrast",
"Pencil Drawing", "Pastel", "HDR", "Summer", "Winter", "Glass Shatter", "Glitch" ],
value="Retro"
)
input_image = gr.Image(label="Resim Yükle", type="numpy")
output_image = gr.Image(label="Filtre Uygulandı")
apply_button = gr.Button("Filtreyi Uygula")
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
with gr.Blocks(css="""
.main { background-color: #1f1f1f; }
.block { 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; /* Başlık rengi beyaz */
}
body {
background-color: #000f26; /* Daha koyu lacivert arka plan rengi */
}
#input-image {
background-color: #000f26; /* Koyu mavi arka plan rengi */
padding: 20px;
border-radius: 10px;
}
#output-image {
background-color: #000f26; /* Koyu mavi arka plan rengi */
padding: 20px;
border-radius: 10px;
}
#filter-dropdown {
background-color: #000f26; /* Filtre seçin arka plan rengi */
color: white; /* Yazı rengi beyaz */
border: none;
padding: 10px;
border-radius: 10px;
}
""") 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"
)
input_image = gr.Image(label="Resim Yükle", type="numpy", elem_id="input-image")
output_image = gr.Image(label="Filtre Uygulandı", elem_id="output-image")
apply_button = gr.Button("Filtreyi Uygula", elem_id="apply-button", elem_classes="custom-button")
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
demo.launch()