File size: 5,268 Bytes
f527465
 
 
 
37c13b7
f527465
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37c13b7
f527465
37c13b7
f527465
 
 
 
 
 
37c13b7
 
 
f527465
 
37c13b7
f527465
 
 
37c13b7
f527465
 
37c13b7
f527465
 
37c13b7
f527465
 
37c13b7
f527465
 
37c13b7
f527465
 
37c13b7
f527465
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import cv2
import numpy as np
import gradio as gr

# Farklı filtre fonksiyonlarını da eklediğim kısım
def apply_gaussian_blur(frame):
    return cv2.GaussianBlur(frame, (15, 15), 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_scale=1.0):
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    hsv[..., 1] = cv2.multiply(hsv[..., 1], saturation_scale)
    return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

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)

def apply_cartoon_filter(frame):
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.medianBlur(gray, 5)
    edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
    color = cv2.bilateralFilter(frame, 9, 300, 300)
    cartoon = cv2.bitwise_and(color, color, mask=edges)
    return cartoon

def apply_pencil_sketch_filter(frame):
    gray, sketch = cv2.pencilSketch(frame, sigma_s=60, sigma_r=0.07, shade_factor=0.05)
    return sketch

def apply_emboss_filter(frame):
    kernel = np.array([[0, -1, -1], [1, 0, -1], [1, 1, 0]])
    return cv2.filter2D(frame, -1, kernel)

def apply_hsv_color_change(frame, hue_shift=10):
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    hsv[..., 0] = (hsv[..., 0] + hue_shift) % 180
    return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

# Filtre uygulama fonksiyonu
def apply_filter(filter_type, input_image=None, alpha=1.0, beta=50, saturation_scale=1.0, hue_shift=10):
    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ı"

    if filter_type == "Gaussian Blur":
        frame = apply_gaussian_blur(frame)
    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=alpha, beta=beta)
    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)
    elif filter_type == "Cartoonize":
        frame = apply_cartoon_filter(frame)
    elif filter_type == "Pencil Sketch":
        frame = apply_pencil_sketch_filter(frame)
    elif filter_type == "Emboss":
        frame = apply_emboss_filter(frame)
    elif filter_type == "HSV Color Change":
        frame = apply_hsv_color_change(frame, hue_shift=hue_shift)
    
    # Saturation ayarını uygulama
    frame = adjust_saturation(frame, saturation_scale=saturation_scale)

    return frame

# Gradio arayüzü
with gr.Blocks() as demo:
    gr.Markdown("#Filtreleme")

    # Filtre seçenekleri burada
    filter_type = gr.Dropdown(
        label="Filtre Seçin",
        choices=["Gaussian Blur", "Sharpen", "Edge Detection", "Invert", "Brightness/Contrast", 
                 "Grayscale", "Sepia", "Sonbahar", "Cartoonize", "Pencil Sketch", "Emboss", "HSV Color Change"],
        value="Gaussian Blur"
    )
    # Ayarlarımızı ekleyelim
    
    # Görüntü yükleme alanımız
    input_image = gr.Image(label="Resim Yükle", type="numpy")

    # Parlaklık ve Kontrast ayarlarımız
    brightness_slider = gr.Slider(label="Parlaklık Ayarı (beta)", minimum=-100, maximum=100, step=1, value=50)
    contrast_slider = gr.Slider(label="Kontrast Ayarı (alpha)", minimum=0.5, maximum=3.0, step=0.1, value=1.0)

    # Saturation ayarımız 
    saturation_slider = gr.Slider(label="Renk Doygunluğu", minimum=0.0, maximum=3.0, step=0.1, value=1.0)

    # HSV renk kaydırma ayarımız
    hue_slider = gr.Slider(label="HSV Renk Kaydırma", minimum=0, maximum=180, step=1, value=10)

    # Çıktı için görüntümüz
    output_image = gr.Image(label="Filtre Uygulandı")

    # Filtre uygula butonumuz
    apply_button = gr.Button("Filtreyi Uygula")

    # Butona tıklanınca filtre uygulama fonksiyonumuz
    apply_button.click(fn=apply_filter, inputs=[filter_type, input_image, contrast_slider, brightness_slider, saturation_slider, hue_slider], outputs=output_image)

# Gradio arayüzünü başlatma kısmı
demo.launch()