File size: 2,907 Bytes
6f110c6
 
 
 
 
 
 
 
b145bbf
 
 
 
 
 
 
 
 
6f110c6
b145bbf
 
 
 
 
6f110c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b145bbf
 
 
 
 
 
 
 
6f110c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b145bbf
6f110c6
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import cv2 as cv
import numpy as np
import gradio as gr

# Farklı filtre fonksiyonları
def apply_gaussian_blur(frame):
    return cv.GaussianBlur(frame, (15, 15), 0)

def apply_box_blur(frame): 
    return cv.blur(frame, (5,5))

def apply_hsv(frame): 
    return cv.cvtColor(frame, cv.COLOR_BGR2HSV)

def apply_scale_contrast(frame):
    return cv.convertScaleAbs(frame , alpha = 1.5 , beta = 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_emboss_filter(frame):
    kernel = np.array([[-2,-1,0],[-1,1,1],[0,1,2]])
    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)

# Filtre uygulama fonksiyonu
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 "Web kameradan görüntü alınamadı"

    if filter_type == "Gaussian Blur":
        return apply_gaussian_blur(frame)
    elif filter_type == "Box Blur":
        return apply_box_blur(frame)
    elif filter_type == "HSV": 
        return apply_hsv(frame)
    elif filter_type == "Scale Contrast": 
        return apply_scale_contrast(frame)
    elif filter_type == "Emboss Filter":
        return apply_emboss_filter(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)

# 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", "Sharpen", "Edge Detection", "Invert", "Brightness", "Grayscale", "Box Blur", "HSV", "Scale Contrast", "Emboss Filter"],
        value="Gaussian Blur"
    )

    # 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ı")

    # Görüntü yüklendiğinde filtre uygulama fonksiyonu
    input_image.change(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)

# Gradio arayüzünü başlat
demo.launch()