emirhntozlu's picture
Upload 2 files
e3e52ea verified
import cv2
import numpy as np
import gradio as gr
def apply_gaussian_blur(frame, intensity):
ksize = int(intensity) * 2 + 1
return cv2.GaussianBlur(frame, (ksize, 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=0):
return cv2.convertScaleAbs(frame, alpha=alpha, beta=beta)
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]])
sepia_frame = cv2.transform(frame, sepia_filter)
sepia_frame = np.clip(sepia_frame, 0, 255)
return sepia_frame
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]])
fall_frame = cv2.transform(frame, fall_filter)
fall_frame = np.clip(fall_frame, 0, 255)
return fall_frame
def apply_filter(filter_types, input_image, blur_intensity=1, brightness=1.0, contrast=50):
frame = input_image.copy()
for filter_type in filter_types:
if filter_type == "Gaussian Blur":
frame = apply_gaussian_blur(frame, blur_intensity)
elif filter_type == "Sharpen":
frame = apply_sharpening_filter(frame)
elif filter_type == "Edge Detection":
frame = apply_edge_detection(frame)
elif filter_type == "Invert":
frame = apply_invert_filter(frame)
elif filter_type == "Brightness/Contrast":
frame = adjust_brightness_contrast(frame, alpha=brightness, beta=contrast)
elif filter_type == "Grayscale":
frame = apply_grayscale_filter(frame)
elif filter_type == "Sepia":
frame = apply_sepia_filter(frame)
elif filter_type == "Sonbahar":
frame = apply_fall_filter(frame)
return frame
with gr.Blocks() as demo:
gr.Markdown("# Gelişmiş Web Kameradan Canlı Filtreleme")
filter_types = gr.CheckboxGroup(
label="Filtre Seçin",
choices=["Gaussian Blur", "Sharpen", "Edge Detection", "Invert", "Brightness/Contrast", "Grayscale", "Sepia", "Sonbahar"],
value=["Gaussian Blur"]
)
blur_intensity = gr.Slider(label="Gaussian Blur Yoğunluğu", minimum=1, maximum=10, step=1, value=1)
brightness = gr.Slider(label="Parlaklık", minimum=0.5, maximum=2.0, step=0.1, value=1.0)
contrast = gr.Slider(label="Kontrast", minimum=0, maximum=100, step=10, value=50)
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_types, input_image, blur_intensity, brightness, contrast], outputs=output_image)
demo.launch()