Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 apply_grayscale_filter(frame):
|
23 |
+
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
24 |
+
|
25 |
+
def apply_sepia_filter(frame):
|
26 |
+
sepia_filter = np.array([[0.272, 0.534, 0.131],
|
27 |
+
[0.349, 0.686, 0.168],
|
28 |
+
[0.393, 0.769, 0.189]])
|
29 |
+
return cv2.transform(frame, sepia_filter)
|
30 |
+
|
31 |
+
def apply_fall_filter(frame):
|
32 |
+
fall_filter = np.array([[0.393, 0.769, 0.189],
|
33 |
+
[0.349, 0.686, 0.168],
|
34 |
+
[0.272, 0.534, 0.131]])
|
35 |
+
return cv2.transform(frame, fall_filter)
|
36 |
+
|
37 |
+
def apply_emboss_filter(frame):
|
38 |
+
kernel = np.array([[ -2, -1, 0],
|
39 |
+
[ -1, 1, 1],
|
40 |
+
[ 0, 1, 2]])
|
41 |
+
return cv2.filter2D(frame, -1, kernel)
|
42 |
+
|
43 |
+
def apply_cartoon_filter(frame):
|
44 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
45 |
+
gray = cv2.medianBlur(gray, 5)
|
46 |
+
edges = cv2.adaptiveThreshold(gray, 255,
|
47 |
+
cv2.ADAPTIVE_THRESH_MEAN_C,
|
48 |
+
cv2.THRESH_BINARY, 9, 9)
|
49 |
+
color = cv2.bilateralFilter(frame, 9, 300, 300)
|
50 |
+
return cv2.bitwise_and(color, color, mask=edges)
|
51 |
+
|
52 |
+
def apply_threshold(frame, thresh_value=127):
|
53 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
54 |
+
_, thresh = cv2.threshold(gray, thresh_value, 255, cv2.THRESH_BINARY)
|
55 |
+
return thresh
|
56 |
+
|
57 |
+
def apply_blurred_edges(frame):
|
58 |
+
blurred = cv2.GaussianBlur(frame, (21, 21), 0)
|
59 |
+
edges = cv2.Canny(frame, 100, 200)
|
60 |
+
return cv2.bitwise_and(blurred, blurred, mask=edges)
|
61 |
+
|
62 |
+
# Filtre uygulama fonksiyonu
|
63 |
+
def apply_filter(filter_type, input_image):
|
64 |
+
if input_image is None:
|
65 |
+
return "Resim yüklenmedi"
|
66 |
+
|
67 |
+
frame = input_image
|
68 |
+
|
69 |
+
if filter_type == "Gaussian Blur":
|
70 |
+
return apply_gaussian_blur(frame)
|
71 |
+
elif filter_type == "Sharpen":
|
72 |
+
return apply_sharpening_filter(frame)
|
73 |
+
elif filter_type == "Edge Detection":
|
74 |
+
return apply_edge_detection(frame)
|
75 |
+
elif filter_type == "Invert":
|
76 |
+
return apply_invert_filter(frame)
|
77 |
+
elif filter_type == "Brightness":
|
78 |
+
return adjust_brightness_contrast(frame, alpha=1.0, beta=50)
|
79 |
+
elif filter_type == "Grayscale":
|
80 |
+
return apply_grayscale_filter(frame)
|
81 |
+
elif filter_type == "Sepia":
|
82 |
+
return apply_sepia_filter(frame)
|
83 |
+
elif filter_type == "Sonbahar":
|
84 |
+
return apply_fall_filter(frame)
|
85 |
+
elif filter_type == "Emboss":
|
86 |
+
return apply_emboss_filter(frame)
|
87 |
+
elif filter_type == "Cartoon":
|
88 |
+
return apply_cartoon_filter(frame)
|
89 |
+
elif filter_type == "Threshold":
|
90 |
+
return apply_threshold(frame)
|
91 |
+
elif filter_type == "Blurred Edges":
|
92 |
+
return apply_blurred_edges(frame)
|
93 |
+
|
94 |
+
# Gradio arayüzü
|
95 |
+
with gr.Blocks() as demo:
|
96 |
+
gr.Markdown("# Web Kameradan Canlı Filtreleme")
|
97 |
+
|
98 |
+
# Filtre seçenekleri
|
99 |
+
filter_type = gr.Dropdown(
|
100 |
+
label="Filtre Seçin",
|
101 |
+
choices=["Gaussian Blur", "Sharpen", "Edge Detection", "Invert", "Brightness",
|
102 |
+
"Grayscale", "Sepia", "Sonbahar", "Emboss", "Cartoon", "Threshold", "Blurred Edges"],
|
103 |
+
value="Gaussian Blur"
|
104 |
+
)
|
105 |
+
|
106 |
+
# Görüntü yükleme alanı
|
107 |
+
input_image = gr.Image(label="Resim Yükle", type="numpy")
|
108 |
+
|
109 |
+
# Çıktı için görüntü
|
110 |
+
output_image = gr.Image(label="Filtre Uygulandı")
|
111 |
+
|
112 |
+
# Filtre uygula butonu
|
113 |
+
apply_button = gr.Button("Filtreyi Uygula")
|
114 |
+
|
115 |
+
# Butona tıklanınca filtre uygulama fonksiyonu
|
116 |
+
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
|
117 |
+
|
118 |
+
# Gradio arayüzünü başlat
|
119 |
+
demo.launch()
|