elfgk commited on
Commit
8f26b56
·
verified ·
1 Parent(s): d2da29f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Yeni filtre fonksiyonları
38
+ def apply_emboss_filter(frame):
39
+ kernel = np.array([[-2, -1, 0],
40
+ [-1, 1, 1],
41
+ [0, 1, 2]])
42
+ return cv2.filter2D(frame, -1, kernel)
43
+
44
+ def apply_sketch_filter(frame):
45
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
46
+ inv_gray = cv2.bitwise_not(gray)
47
+ blur = cv2.GaussianBlur(inv_gray, (21, 21), 0)
48
+ sketch = cv2.divide(gray, 255 - blur, scale=256)
49
+ return sketch
50
+
51
+ def apply_median_blur_filter(frame):
52
+ return cv2.medianBlur(frame, 15)
53
+
54
+ def adjust_contrast(frame, alpha=2.0, beta=0):
55
+ return cv2.convertScaleAbs(frame, alpha=alpha, beta=beta)
56
+
57
+ def adjust_hue(frame, delta=30):
58
+ hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
59
+ hsv[:, :, 0] = (hsv[:, :, 0] + delta) % 180
60
+ return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
61
+
62
+ # Filtre uygulama fonksiyonu
63
+ def apply_filter(filter_type, input_image=None):
64
+ if input_image is not None:
65
+ frame = input_image
66
+ else:
67
+ cap = cv2.VideoCapture(0)
68
+ ret, frame = cap.read()
69
+ cap.release()
70
+ if not ret:
71
+ return "Web kameradan görüntü alınamadı"
72
+
73
+ if filter_type == "Gaussian Blur":
74
+ return apply_gaussian_blur(frame)
75
+ elif filter_type == "Sharpen":
76
+ return apply_sharpening_filter(frame)
77
+ elif filter_type == "Edge Detection":
78
+ return apply_edge_detection(frame)
79
+ elif filter_type == "Invert":
80
+ return apply_invert_filter(frame)
81
+ elif filter_type == "Brightness":
82
+ return adjust_brightness_contrast(frame, alpha=1.0, beta=50)
83
+ elif filter_type == "Grayscale":
84
+ return apply_grayscale_filter(frame)
85
+ elif filter_type == "Sepia":
86
+ return apply_sepia_filter(frame)
87
+ elif filter_type == "Sonbahar":
88
+ return apply_fall_filter(frame)
89
+ elif filter_type == "Emboss":
90
+ return apply_emboss_filter(frame)
91
+ elif filter_type == "Sketch":
92
+ return apply_sketch_filter(frame)
93
+ elif filter_type == "Median Blur":
94
+ return apply_median_blur_filter(frame)
95
+ elif filter_type == "Contrast Adjustment":
96
+ return adjust_contrast(frame, alpha=2.0, beta=0)
97
+ elif filter_type == "Hue Adjustment":
98
+ return adjust_hue(frame, delta=30)
99
+
100
+ # Gradio arayüzü
101
+ with gr.Blocks() as demo:
102
+ gr.Markdown("# Fotoğraf Filtreleme")
103
+
104
+ # Filtre seçenekleri
105
+ filter_type = gr.Dropdown(
106
+ label="Filtre Seçin",
107
+ choices=["Gaussian Blur", "Sharpen", "Edge Detection", "Invert", "Brightness", "Grayscale", "Sepia", "Sonbahar",
108
+ "Emboss", "Sketch", "Median Blur", "Contrast Adjustment", "Hue Adjustment"],
109
+ value="Gaussian Blur"
110
+ )
111
+
112
+ # Görüntü yükleme alanı
113
+ input_image = gr.Image(label="Resim Yükle", type="numpy")
114
+
115
+ # Çıktı için görüntü
116
+ output_image = gr.Image(label="Filtre Uygulandı")
117
+
118
+ # Filtre uygula butonu
119
+ apply_button = gr.Button("Filtreyi Uygula")
120
+
121
+ # Butona tıklanınca filtre uygulama fonksiyonu
122
+ apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
123
+
124
+ # Gradio arayüzünü başlat
125
+ demo.launch()