Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Farklı filtre fonksiyonları
|
6 |
+
def apply_gaussian_blur(frame):
|
7 |
+
return cv2.GaussianBlur(frame, (15, 15), 0)
|
8 |
+
|
9 |
+
def apply_sharpening_filter(frame):
|
10 |
+
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
11 |
+
return cv2.filter2D(frame, -1, kernel)
|
12 |
+
|
13 |
+
def apply_edge_detection(frame):
|
14 |
+
return cv2.Canny(frame, 100, 200)
|
15 |
+
|
16 |
+
def apply_invert_filter(frame):
|
17 |
+
return cv2.bitwise_not(frame)
|
18 |
+
|
19 |
+
def adjust_brightness_contrast(frame, alpha=1.0, beta=50):
|
20 |
+
return cv2.convertScaleAbs(frame, alpha=alpha, beta=beta)
|
21 |
+
|
22 |
+
def adjust_saturation(frame, saturation_scale=1.0):
|
23 |
+
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
24 |
+
hsv[..., 1] = cv2.multiply(hsv[..., 1], saturation_scale)
|
25 |
+
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
26 |
+
|
27 |
+
def apply_grayscale_filter(frame):
|
28 |
+
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
29 |
+
|
30 |
+
def apply_sepia_filter(frame):
|
31 |
+
sepia_filter = np.array([[0.272, 0.534, 0.131],
|
32 |
+
[0.349, 0.686, 0.168],
|
33 |
+
[0.393, 0.769, 0.189]])
|
34 |
+
return cv2.transform(frame, sepia_filter)
|
35 |
+
|
36 |
+
def apply_fall_filter(frame):
|
37 |
+
fall_filter = np.array([[0.393, 0.769, 0.189],
|
38 |
+
[0.349, 0.686, 0.168],
|
39 |
+
[0.272, 0.534, 0.131]])
|
40 |
+
return cv2.transform(frame, fall_filter)
|
41 |
+
|
42 |
+
def apply_cartoon_filter(frame):
|
43 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
44 |
+
gray = cv2.medianBlur(gray, 5)
|
45 |
+
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
|
46 |
+
color = cv2.bilateralFilter(frame, 9, 300, 300)
|
47 |
+
cartoon = cv2.bitwise_and(color, color, mask=edges)
|
48 |
+
return cartoon
|
49 |
+
|
50 |
+
def apply_pencil_sketch_filter(frame):
|
51 |
+
gray, sketch = cv2.pencilSketch(frame, sigma_s=60, sigma_r=0.07, shade_factor=0.05)
|
52 |
+
return sketch
|
53 |
+
|
54 |
+
def apply_emboss_filter(frame):
|
55 |
+
kernel = np.array([[0, -1, -1], [1, 0, -1], [1, 1, 0]])
|
56 |
+
return cv2.filter2D(frame, -1, kernel)
|
57 |
+
|
58 |
+
def apply_hsv_color_change(frame, hue_shift=10):
|
59 |
+
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
60 |
+
hsv[..., 0] = (hsv[..., 0] + hue_shift) % 180
|
61 |
+
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
62 |
+
|
63 |
+
# Filtre uygulama fonksiyonu
|
64 |
+
def apply_filter(filter_type, input_image=None, alpha=1.0, beta=50, saturation_scale=1.0, hue_shift=10):
|
65 |
+
if input_image is not None:
|
66 |
+
frame = input_image
|
67 |
+
else:
|
68 |
+
cap = cv2.VideoCapture(0)
|
69 |
+
ret, frame = cap.read()
|
70 |
+
cap.release()
|
71 |
+
if not ret:
|
72 |
+
return "Web kameradan görüntü alınamadı"
|
73 |
+
|
74 |
+
if filter_type == "Gaussian Blur":
|
75 |
+
frame = apply_gaussian_blur(frame)
|
76 |
+
elif filter_type == "Sharpen":
|
77 |
+
frame = apply_sharpening_filter(frame)
|
78 |
+
elif filter_type == "Edge Detection":
|
79 |
+
frame = apply_edge_detection(frame)
|
80 |
+
elif filter_type == "Invert":
|
81 |
+
frame = apply_invert_filter(frame)
|
82 |
+
elif filter_type == "Brightness/Contrast":
|
83 |
+
frame = adjust_brightness_contrast(frame, alpha=alpha, beta=beta)
|
84 |
+
elif filter_type == "Grayscale":
|
85 |
+
frame = apply_grayscale_filter(frame)
|
86 |
+
elif filter_type == "Sepia":
|
87 |
+
frame = apply_sepia_filter(frame)
|
88 |
+
elif filter_type == "Sonbahar":
|
89 |
+
frame = apply_fall_filter(frame)
|
90 |
+
elif filter_type == "Cartoonize":
|
91 |
+
frame = apply_cartoon_filter(frame)
|
92 |
+
elif filter_type == "Pencil Sketch":
|
93 |
+
frame = apply_pencil_sketch_filter(frame)
|
94 |
+
elif filter_type == "Emboss":
|
95 |
+
frame = apply_emboss_filter(frame)
|
96 |
+
elif filter_type == "HSV Color Change":
|
97 |
+
frame = apply_hsv_color_change(frame, hue_shift=hue_shift)
|
98 |
+
|
99 |
+
# Saturation ayarını uygulama
|
100 |
+
frame = adjust_saturation(frame, saturation_scale=saturation_scale)
|
101 |
+
|
102 |
+
return frame
|
103 |
+
|
104 |
+
# Gradio arayüzü
|
105 |
+
with gr.Blocks() as demo:
|
106 |
+
gr.Markdown("# Web Kameradan Canlı Filtreleme")
|
107 |
+
|
108 |
+
# Filtre seçenekleri
|
109 |
+
filter_type = gr.Dropdown(
|
110 |
+
label="Filtre Seçin",
|
111 |
+
choices=["Gaussian Blur", "Sharpen", "Edge Detection", "Invert", "Brightness/Contrast",
|
112 |
+
"Grayscale", "Sepia", "Sonbahar", "Cartoonize", "Pencil Sketch", "Emboss", "HSV Color Change"],
|
113 |
+
value="Gaussian Blur"
|
114 |
+
)
|
115 |
+
|
116 |
+
# Görüntü yükleme alanı
|
117 |
+
input_image = gr.Image(label="Resim Yükle", type="numpy")
|
118 |
+
|
119 |
+
# Parlaklık ve Kontrast ayarları
|
120 |
+
brightness_slider = gr.Slider(label="Parlaklık Ayarı (beta)", minimum=-100, maximum=100, step=1, value=50)
|
121 |
+
contrast_slider = gr.Slider(label="Kontrast Ayarı (alpha)", minimum=0.5, maximum=3.0, step=0.1, value=1.0)
|
122 |
+
|
123 |
+
# Saturation ayarı
|
124 |
+
saturation_slider = gr.Slider(label="Renk Doygunluğu", minimum=0.0, maximum=3.0, step=0.1, value=1.0)
|
125 |
+
|
126 |
+
# HSV renk kaydırma ayarı
|
127 |
+
hue_slider = gr.Slider(label="HSV Renk Kaydırma", minimum=0, maximum=180, step=1, value=10)
|
128 |
+
|
129 |
+
# Çıktı için görüntü
|
130 |
+
output_image = gr.Image(label="Filtre Uygulandı")
|
131 |
+
|
132 |
+
# Filtre uygula butonu
|
133 |
+
apply_button = gr.Button("Filtreyi Uygula")
|
134 |
+
|
135 |
+
# Butona tıklanınca filtre uygulama fonksiyonu
|
136 |
+
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image, contrast_slider, brightness_slider, saturation_slider, hue_slider], outputs=output_image)
|
137 |
+
|
138 |
+
# Gradio arayüzünü başlat
|
139 |
+
demo.launch()
|