Spaces:
Sleeping
Sleeping
import cv2 | |
import numpy as np | |
import gradio as gr | |
# Farklı filtre fonksiyonları | |
def apply_gaussian_blur(frame, blur_level=1): | |
ksize = (2 * blur_level + 1, 2 * blur_level + 1) | |
return cv2.GaussianBlur(frame, ksize, 0) | |
def apply_sharpening_filter(frame): | |
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) | |
return cv2.filter2D(frame, -1, kernel) | |
def apply_edge_detection(frame): | |
return cv2.Canny(frame, 100, 200) | |
def apply_invert_filter(frame): | |
return cv2.bitwise_not(frame) | |
def adjust_brightness_contrast(frame, alpha=1.0, beta=50): | |
return cv2.convertScaleAbs(frame, alpha=alpha, beta=beta) | |
def adjust_saturation(frame, saturation=1.0): | |
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV).astype("float32") | |
hsv[..., 1] *= saturation | |
hsv[..., 1] = np.clip(hsv[..., 1], 0, 255) | |
return cv2.cvtColor(hsv.astype("uint8"), cv2.COLOR_HSV2BGR) | |
def adjust_hue(frame, hue_shift=0): | |
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | |
hsv[..., 0] = (hsv[..., 0].astype(int) + hue_shift) % 180 | |
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) | |
def adjust_gamma(frame, gamma=1.0): | |
inv_gamma = 1.0 / gamma | |
table = (np.array([((i / 255.0) ** inv_gamma) * 255 for i in range(256)]) | |
.astype("uint8")) | |
return cv2.LUT(frame, table) | |
def apply_grayscale_filter(frame): | |
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
def apply_sepia_filter(frame): | |
sepia_filter = np.array([[0.272, 0.534, 0.131], | |
[0.349, 0.686, 0.168], | |
[0.393, 0.769, 0.189]]) | |
return cv2.transform(frame, sepia_filter) | |
def apply_fall_filter(frame): | |
fall_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, fall_filter) | |
# Filtre uygulama fonksiyonu | |
def apply_filter(filter_type, input_image=None, alpha=1.0, beta=50, saturation=1.0, hue_shift=0, gamma=1.0, blur_level=1): | |
if input_image is not None: | |
frame = input_image | |
else: | |
cap = cv2.VideoCapture(0) | |
ret, frame = cap.read() | |
cap.release() | |
if not ret: | |
return "Web kameradan görüntü alınamadı" | |
# Seçilen filtreyi uygula | |
if filter_type == "Gaussian Blur": | |
frame = apply_gaussian_blur(frame, blur_level=blur_level) | |
elif filter_type == "Keskinleştir": | |
frame = apply_sharpening_filter(frame) | |
elif filter_type == "Kenar Algılama": | |
frame = apply_edge_detection(frame) | |
elif filter_type == "Ters Çevir": | |
frame = apply_invert_filter(frame) | |
elif filter_type == "Gri Tonlama": | |
frame = apply_grayscale_filter(frame) | |
elif filter_type == "Sepya": | |
frame = apply_sepia_filter(frame) | |
elif filter_type == "Sonbahar": | |
frame = apply_fall_filter(frame) | |
# Tüm filtrelerden bağımsız parametreleri uygula | |
frame = adjust_brightness_contrast(frame, alpha=alpha, beta=beta) | |
frame = adjust_saturation(frame, saturation=saturation) | |
frame = adjust_hue(frame, hue_shift=hue_shift) | |
frame = adjust_gamma(frame, gamma=gamma) | |
return frame | |
# Gradio arayüzü | |
with gr.Blocks() as demo: | |
gr.Markdown("# Web Kameradan Canlı Filtreleme") | |
# Filtre seçenekleri | |
filter_type = gr.Dropdown( | |
label="Filtre Seçin", | |
choices=["Gaussian Blur", "Keskinleştir", "Kenar Algılama", "Ters Çevir", "Parlaklık/Kontrast", "Doygunluk", "Renk Tonu", "Gama", "Gri Tonlama", "Sepya", "Sonbahar"], | |
value="Gaussian Blur" | |
) | |
# Ayar kaydırıcıları | |
alpha_slider = gr.Slider(0.5, 3.0, value=1.0, label="Parlaklık") | |
beta_slider = gr.Slider(-100, 100, value=50, label="Kontrast") | |
saturation_slider = gr.Slider(0.0, 3.0, value=1.0, label="Doygunluk") | |
hue_slider = gr.Slider(-90, 90, value=0, label="Renk Tonu Değişimi") | |
gamma_slider = gr.Slider(0.1, 3.0, value=1.0, label="Gama") | |
blur_slider = gr.Slider(1, 10, value=1, label="Bulanıklık Seviyesi") | |
# Görüntü yükleme alanı | |
input_image = gr.Image(label="Resim Yükle", type="numpy") | |
# Çıktı için görüntü | |
output_image = gr.Image(label="Filtre Uygulandı") | |
# Filtre uygula butonu | |
apply_button = gr.Button("Filtreyi Uygula") | |
# Butona tıklanınca filtre uygulama fonksiyonu | |
apply_button.click( | |
fn=apply_filter, | |
inputs=[filter_type, input_image, alpha_slider, beta_slider, saturation_slider, hue_slider, gamma_slider, blur_slider], | |
outputs=output_image | |
) | |
# Gradio arayüzünü başlat | |
demo.launch() |