|
import cv2 as cv |
|
import numpy as np |
|
import gradio as gr |
|
|
|
|
|
def retro_filter(frame): |
|
sepia= cv.transform(frame, np.array([[0.393, 0.769, 0.189], |
|
[0.349, 0.686, 0.168], |
|
[0.272, 0.534, 0.131]])) |
|
normalization= np.clip(sepia, 0, 255).astype(np.uint8) |
|
return normalization |
|
|
|
def apply_gaussian_blur(frame): |
|
return cv.GaussianBlur(frame, (15, 15), 0) |
|
|
|
def apply_sharpening_filter(frame): |
|
kernel = np.array([[0, -1, 0], [-1, 5,-1], [0, -1, 0]]) |
|
return cv.filter2D(frame, -1, kernel) |
|
|
|
def apply_edge_detection(frame): |
|
return cv.Canny(frame, 100, 200) |
|
|
|
def apply_invert_filter(frame): |
|
return cv.bitwise_not(frame) |
|
|
|
def adjust_brightness_contrast(frame, alpha=1.0, beta=50): |
|
return cv.convertScaleAbs(frame, alpha=alpha, beta=beta) |
|
|
|
def apply_grayscale_filter(frame): |
|
return cv.cvtColor(frame, cv.COLOR_BGR2GRAY) |
|
|
|
|
|
def apply_filter(filter_type, input_image=None): |
|
if input_image is not None: |
|
frame = input_image |
|
else: |
|
cap = cv.VideoCapture(0) |
|
ret, frame = cap.read() |
|
cap.release() |
|
if not ret: |
|
return "Kameradan görüntü alınamadı. Lütfen tekrar deneyin." |
|
|
|
if filter_type == "Gaussian Blur": |
|
return apply_gaussian_blur(frame) |
|
elif filter_type == "Sharpen": |
|
return apply_sharpening_filter(frame) |
|
elif filter_type == "Edge Detection": |
|
return apply_edge_detection(frame) |
|
elif filter_type == "Invert": |
|
return apply_invert_filter(frame) |
|
elif filter_type == "Brightness": |
|
return adjust_brightness_contrast(frame, alpha=1.0, beta=50) |
|
elif filter_type == "Grayscale": |
|
return apply_grayscale_filter(frame) |
|
elif filter_type == "Retro": |
|
return retro_filter(frame) |
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# Web Kameradan Canlı Filtreleme") |
|
|
|
|
|
filter_type = gr.Dropdown( |
|
label="Filtre Seçin", |
|
choices=["Retro","Gaussian Blur", "Sharpen", "Edge Detection", "Invert", "Brightness", "Grayscale"], |
|
value="Retro" |
|
) |
|
interactive=True |
|
|
|
input_image = gr.Image(label="Resim Yükle", type="numpy") |
|
|
|
|
|
output_image = gr.Image(label="Filtre Uygulandı") |
|
|
|
|
|
apply_button = gr.Button("Filtre Uygula") |
|
|
|
|
|
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image) |
|
|
|
|
|
demo.launch() |