Spaces:
Runtime error

celisamevik's picture
Create app.py
4b5add5 verified
Raw History Blame Contribute Delete
3.83 kB
# ----------------------------------------------------------------------------
# Gradio ile Filtre Uygulama
import cv2
import numpy as np
import gradio as gr
# Filtre Fonksiyonları
def grayscale_filter(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
def invert_filter(image):
return cv2.bitwise_not(image)
def edge_detection_filter(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200)
return cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
def blur_filter(image):
return cv2.GaussianBlur(image, (15, 15), 0)
def sepia_filter(image):
sepia = np.array([[0.272, 0.534, 0.131],
[0.349, 0.686, 0.168],
[0.393, 0.769, 0.189]])
sepia_image = cv2.transform(image, sepia)
return np.clip(sepia_image, 0, 255).astype(np.uint8)
# def vintage_filter(image):
# rows, cols = image.shape[:2]
# kernel_x = cv2.getGaussianKernel(cols, 1000)
# kernel_y = cv2.getGaussianKernel(rows, 1000)
# kernel = kernel_y * kernel_x.T
# mask = 255 * kernel / np.linalg.norm(kernel)
# vintage_image = np.copy(image)
# for i in range(3):
# vintage_image[:, :, i] = vintage_image[:, :, i] * mask
# return vintage_image
# Parlaklık, Kontrast ve Doygunluk Ayar Fonksiyonları
def brightness_contrast_filter(image, brightness=0, contrast=0):
beta = brightness - 100 # OpenCV için parlaklık ayarı
alpha = contrast / 50.0 + 1 # OpenCV için kontrast ayarı
adjusted = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)
return adjusted
def saturation_filter(image, saturation=1):
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hsv[:, :, 1] = cv2.add(hsv[:, :, 1], saturation - 1)
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
# Gradio için ana işlev
def apply_filter(image, filter_type, enable_brightness, brightness, enable_contrast, contrast, enable_saturation,
saturation):
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Filtre seçimi
if filter_type == "Grayscale":
filtered_image = grayscale_filter(image)
elif filter_type == "Invert":
filtered_image = invert_filter(image)
elif filter_type == "Edge Detection":
filtered_image = edge_detection_filter(image)
elif filter_type == "Blur":
filtered_image = blur_filter(image)
elif filter_type == "Sepia":
filtered_image = sepia_filter(image)
# elif filter_type == "Vintage":
# filtered_image = vintage_filter(image)
else:
filtered_image = image
# Parlaklık ve kontrast ayarları yalnızca etkinse uygulanır
if enable_brightness:
filtered_image = brightness_contrast_filter(filtered_image, brightness, 0)
if enable_contrast:
filtered_image = brightness_contrast_filter(filtered_image, 0, contrast)
if enable_saturation:
filtered_image = saturation_filter(filtered_image, saturation)
return cv2.cvtColor(filtered_image, cv2.COLOR_BGR2RGB)
# Gradio arayüzünü oluştur
gr.Interface(
fn=apply_filter,
inputs=[
gr.Image(type="numpy"),
gr.Radio(["None", "Grayscale", "Invert", "Edge Detection", "Blur", "Sepia", "Vintage"], label="Filter Type"),
gr.Checkbox(label="Enable Brightness Adjustment"),
gr.Slider(0, 200, value=100, label="Brightness"),
gr.Checkbox(label="Enable Contrast Adjustment"),
gr.Slider(0, 200, value=100, label="Contrast"),
gr.Checkbox(label="Enable Saturation Adjustment"),
gr.Slider(1, 200, value=100, label="Saturation")
],
outputs="image",
title="Gelişmiş Görüntü Filtreleme Uygulaması",
description="Bir resme çeşitli filtreler uygulayın ve yalnızca isterseniz parlaklık, kontrast, doygunluk gibi ayarları etkinleştirin."
).launch()