h5yildiz commited on
Commit
9e0fd7b
1 Parent(s): 3407ab2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2 as cv
2
+ import numpy as np
3
+ import gradio as gr
4
+
5
+ # Filtreler
6
+ def retro_filter(frame):
7
+ sepia= cv.transform(frame, np.array([[0.393, 0.769, 0.189],
8
+ [0.349, 0.686, 0.168],
9
+ [0.272, 0.534, 0.131]]))
10
+ normalization= np.clip(sepia, 0, 255).astype(np.uint8) #normalizasyon
11
+ return normalization
12
+
13
+ def apply_gaussian_blur(frame):
14
+ return cv.GaussianBlur(frame, (15, 15), 0)
15
+
16
+ def apply_sharpening_filter(frame):
17
+ kernel = np.array([[0, -1, 0], [-1, 5,-1], [0, -1, 0]])
18
+ return cv.filter2D(frame, -1, kernel)
19
+
20
+ def apply_edge_detection(frame):
21
+ return cv.Canny(frame, 100, 200)
22
+
23
+ def apply_invert_filter(frame):
24
+ return cv.bitwise_not(frame)
25
+
26
+ def adjust_brightness_contrast(frame, alpha=1.0, beta=50):
27
+ return cv.convertScaleAbs(frame, alpha=alpha, beta=beta)
28
+
29
+ def apply_grayscale_filter(frame):
30
+ return cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
31
+
32
+ # Filtre uygulama fonksiyonu
33
+ def apply_filter(filter_type, input_image=None):
34
+ if input_image is not None:
35
+ frame = input_image
36
+ else:
37
+ cap = cv.VideoCapture(0)
38
+ ret, frame = cap.read()
39
+ cap.release()
40
+ if not ret:
41
+ return "Kameradan görüntü alınamadı. Lütfen tekrar deneyin."
42
+
43
+ if filter_type == "Gaussian Blur":
44
+ return apply_gaussian_blur(frame)
45
+ elif filter_type == "Sharpen":
46
+ return apply_sharpening_filter(frame)
47
+ elif filter_type == "Edge Detection":
48
+ return apply_edge_detection(frame)
49
+ elif filter_type == "Invert":
50
+ return apply_invert_filter(frame)
51
+ elif filter_type == "Brightness":
52
+ return adjust_brightness_contrast(frame, alpha=1.0, beta=50)
53
+ elif filter_type == "Grayscale":
54
+ return apply_grayscale_filter(frame)
55
+ elif filter_type == "Retro":
56
+ return retro_filter(frame)
57
+
58
+
59
+ # Gradio arayüzü
60
+ with gr.Blocks() as demo:
61
+ gr.Markdown("# Web Kameradan Canlı Filtreleme")
62
+
63
+ # Filtre seçenekleri
64
+ filter_type = gr.Dropdown(
65
+ label="Filtre Seçin",
66
+ choices=["Retro","Gaussian Blur", "Sharpen", "Edge Detection", "Invert", "Brightness", "Grayscale"],
67
+ value="Retro"
68
+ )
69
+ interactive=True
70
+ # Görüntü yükleme alanı
71
+ input_image = gr.Image(label="Resim Yükle", type="numpy")
72
+
73
+ # Çıktı için görüntü
74
+ output_image = gr.Image(label="Filtre Uygulandı")
75
+
76
+ # Filtre uygula butonu
77
+ apply_button = gr.Button("Filtre Uygula")
78
+
79
+ # Butona tıklanınca filtre uygulama fonksiyonu
80
+ apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
81
+
82
+ # Gradio arayüzünü başlat
83
+ demo.launch()