ErenKontas commited on
Commit
bc5c57f
·
verified ·
1 Parent(s): a7fac6c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +174 -0
app.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2 as cv
2
+ import numpy as np
3
+ import gradio as gr
4
+
5
+ def filters(image, filter_type, Brightness, Contrast, Saturation, Hue_Shift, Gamma, Blur_Level):
6
+ image = np.array(image, dtype=np.float32) / 255.0 # Normalize the image
7
+ # TR = Görüntüyü 0-1 arasında normalize eder
8
+ # EN = Normalizes the image to a range of 0 to 1
9
+
10
+ # Brightness adjustment
11
+ image = np.clip(image * Brightness, 0, 1)
12
+ # TR = Parlaklık değerini ayarlar
13
+ # EN = Adjusts the brightness value
14
+
15
+ # Contrast adjustment
16
+ image = np.clip((image - 0.5) * Contrast + 0.5, 0, 1)
17
+ # TR = Kontrast değerini ayarlar
18
+ # EN = Adjusts the contrast value
19
+
20
+ # Saturation adjustment (applies only to BGR images)
21
+ hsv_image = cv.cvtColor(image, cv.COLOR_BGR2HSV)
22
+ hsv_image[..., 1] = np.clip(hsv_image[..., 1] * Saturation, 0, 1)
23
+ image = cv.cvtColor(hsv_image, cv.COLOR_HSV2BGR)
24
+ # TR = Doygunluk seviyesini ayarlar (sadece BGR görüntülerine uygulanır)
25
+ # EN = Adjusts the saturation level (applies only to BGR images)
26
+
27
+ # Hue shift
28
+ hsv_image[..., 0] = (hsv_image[..., 0] + Hue_Shift) % 180
29
+ image = cv.cvtColor(hsv_image, cv.COLOR_HSV2BGR)
30
+ # TR = Renk tonunu kaydırır
31
+ # EN = Shifts the hue of the image
32
+
33
+ # Gamma adjustment
34
+ image = np.clip(image ** (1.0 / Gamma), 0, 1)
35
+ # TR = Gamma ayarını yapar
36
+ # EN = Adjusts the gamma of the image
37
+
38
+ # Blur level
39
+ if Blur_Level > 0:
40
+ ksize = int(Blur_Level * 2 + 1)
41
+ image = cv.GaussianBlur(image, (ksize, ksize), 0)
42
+ # TR = Bulanıklık seviyesini ayarlar
43
+ # EN = Adjusts the blur level of the image
44
+
45
+ image = (image * 255).astype(np.uint8) # Convert to uint8 format
46
+ # TR = Görüntüyü uint8 formatına dönüştürür
47
+ # EN = Converts the image back to uint8 format
48
+
49
+ if filter_type == "Gray Scale":
50
+ filtered_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
51
+ elif filter_type == "Sepia":
52
+ filtered_image = cv.applyColorMap(cv.cvtColor(image, cv.COLOR_BGR2GRAY), cv.COLORMAP_AUTUMN)
53
+ elif filter_type == "X-Ray":
54
+ filtered_image = cv.bitwise_not(image)
55
+ elif filter_type == "Blur":
56
+ filtered_image = cv.GaussianBlur(image, (15, 15), 0)
57
+ elif filter_type == "Sketch":
58
+ gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
59
+ filtered_image = cv.divide(gray, cv.GaussianBlur(gray, (21, 21), 0), scale=256)
60
+ elif filter_type == "Sharpen":
61
+ kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
62
+ filtered_image = cv.filter2D(image, -1, kernel)
63
+ elif filter_type == "Emboss":
64
+ kernel = np.array([[-2, -1, 0], [-1, 1, 1], [0, 1, 2]])
65
+ filtered_image = cv.filter2D(image, -1, kernel)
66
+ elif filter_type == "Edge Detection":
67
+ filtered_image = cv.Canny(image, 100, 200)
68
+ elif filter_type == "Pixel Art":
69
+ small = cv.resize(image, (32, 32), interpolation=cv.INTER_LINEAR)
70
+ filtered_image = cv.resize(small, image.shape[:2][::-1], interpolation=cv.INTER_NEAREST)
71
+ elif filter_type == "Mosaic":
72
+ small = cv.resize(image, (16, 16), interpolation=cv.INTER_LINEAR)
73
+ filtered_image = cv.resize(small, image.shape[:2][::-1], interpolation=cv.INTER_NEAREST)
74
+ elif filter_type == "Rainbow":
75
+ filtered_image = cv.applyColorMap(image, cv.COLORMAP_JET)
76
+ elif filter_type == "Night Vision":
77
+ filtered_image = cv.applyColorMap(cv.cvtColor(image, cv.COLOR_BGR2GRAY), cv.COLORMAP_OCEAN)
78
+ elif filter_type == "Matrix Effect":
79
+ filtered_image = image.copy()
80
+ elif filter_type == "Wave Effect":
81
+ filtered_image = cv.GaussianBlur(image, (21, 21), 0)
82
+ elif filter_type == "Timestamp":
83
+ filtered_image = image.copy()
84
+ elif filter_type == "Glitch Effect":
85
+ filtered_image = image.copy()
86
+ elif filter_type == "Pop Art":
87
+ filtered_image = cv.cvtColor(image, cv.COLOR_BGR2HSV)
88
+ elif filter_type == "Oil Paint":
89
+ filtered_image = cv.GaussianBlur(image, (21, 21), 0)
90
+ elif filter_type == "Texture":
91
+ filtered_image = cv.GaussianBlur(image, (21, 21), 0)
92
+ else:
93
+ filtered_image = image # Default to the original image
94
+ # TR = Farklı filtre türlerine göre filtreyi uygular
95
+ # EN = Applies the selected filter based on the filter type
96
+
97
+ return filtered_image
98
+
99
+ # Interface setup
100
+ with gr.Blocks() as demo:
101
+ gr.Markdown("**Live Color Filters**")
102
+ # TR = Arayüz başlığını belirler
103
+ # EN = Sets the title of the interface
104
+
105
+ filter_dropdown = gr.Dropdown(
106
+ ["Gray Scale", "Sepia", "X-Ray", "Blur",
107
+ "Sketch", "Sharpen", "Emboss", "Edge Detection",
108
+ "Pixel Art", "Mosaic", "Rainbow", "Night Vision",
109
+ "Matrix Effect", "Wave Effect", "Timestamp", "Glitch Effect",
110
+ "Pop Art", "Oil Paint", "Texture"],
111
+ label="Select Filter",
112
+ )
113
+ # TR = Kullanıcıya filtre türü seçme imkanı tanır
114
+ # EN = Provides a dropdown for the user to select a filter type
115
+
116
+ Brightness = gr.Slider(minimum=0.5, maximum=5.0, value=1.0, label="Brightness")
117
+ Contrast = gr.Slider(minimum=-100, maximum=100, value=50, label="Contrast")
118
+ Saturation = gr.Slider(minimum=0.0, maximum=5.0, value=1.0, label="Saturation")
119
+ Hue_Shift = gr.Slider(minimum=-100, maximum=100, value=0, label="Hue Shift")
120
+ Gamma = gr.Slider(minimum=0, maximum=5, value=1.0, label="Gamma")
121
+ Blur_Level = gr.Slider(minimum=0, maximum=10, value=1, label="Blur Level")
122
+ # TR = Kullanıcıya görsel düzenlemeleri yapma seçenekleri tanır
123
+ # EN = Provides sliders for adjusting image properties
124
+
125
+ image_input = gr.Image(type='pil', label='Resmi Yükle')
126
+ image_output = gr.Image(type='numpy', label='Filtrelenmiş Resim')
127
+ # TR = Görsel yükleme ve çıktıyı görüntüleme seçenekleri sunar
128
+ # EN = Provides options for uploading the image and displaying the output
129
+
130
+ btn = gr.Button("Filtreyi Uygula")
131
+ btn.click(fn=filters, inputs=[image_input, filter_dropdown, Brightness, Contrast, Saturation, Hue_Shift, Gamma, Blur_Level], outputs=image_output)
132
+ # TR = Filtreyi uygulamak için bir buton ekler
133
+ # EN = Adds a button to apply the selected filter
134
+
135
+ with gr.Accordion('Filtrelerin Açıklanması',open=False):
136
+ # Açıklama kısmı
137
+ gr.Markdown("""
138
+ **Temel Filtreler**
139
+ - **Gri Tonlama**: Görüntüyü siyah-beyaz tonlarında, klasik bir tarzda dönüştürür
140
+ - **Sepya**: Fotoğrafa eski bir fotoğraf havası veren, sıcak kahverengi tonları ekler
141
+ - **Röntgen**: Görüntüye ters ışıklandırma ekleyerek X-ray taraması etkisi oluşturur
142
+ - **Bulanıklaştır**: Görüntüde yumuşak bir bulanıklık oluşturarak detayları azaltır.
143
+
144
+ **Klasik Filtreler Listesi**
145
+ - **Karakalem Efekti**: Görüntüyü karakalem çizimi gibi gösterir
146
+ - **Keskinleştir**: Görüntüdeki detayları belirginleştirir
147
+ - **Kabartma**: Görüntüye kabartma ve derinlik efekti ekler
148
+ - **Kenar Algılama**: Görüntüdeki kenar hatlarını vurgular
149
+
150
+ **Yaratıcı Filtreler**
151
+ - **Piksel Sanatı**: Görüntüyü retro piksel tarzında küçük karelere böler
152
+ - **Mozaik Efekti**: Fotoğrafı küçük mozaik parçalarına böler
153
+ - **Gökkuşağı**: Görüntüye renkli gökkuşağı efektleri ekler
154
+ - **Gece Görüşü**: Gece görüş cihazı efektini simüle eder
155
+
156
+ **Özel Efektler**
157
+ - **Matrix Efekti**: Matrix film efekti
158
+ - **Dalga Efekti**: Görüntüye su dalgası benzeri kıvrımlı bir bozulma ekler, dalgalanma hissi yaratır
159
+ - **Zaman Damgası**: Fotoğrafın üzerine çekildiği tarih ve saati ekleyerek nostaljik bir hava verir
160
+ - **Glitch Efekti**: Dijital bozulmalar ekleyerek, fotoğrafa retro tarzda bir hata efekti katar
161
+
162
+ **Sanatsal Filtreler**
163
+ - **Pop Art**: Canlı ve kontrastlı renklerle, Andy Warhol tarzı ikonik pop-art efekti oluşturur
164
+ - **Yağlı Boya**: Fırça darbelerini simüle ederek görüntüye yağlı boya tablosu görünümü kazandırır
165
+ - **Doku Efekti**: Görüntüye yüzey dokusu ekleyerek dokunsal bir derinlik ve sanat eseri havası verir
166
+ """)
167
+ # TR = Filtre açıklamalarını ekler
168
+ # EN = Adds filter descriptions to the interface
169
+
170
+ if __name__ == "__main__":
171
+ demo.launch(share=True)
172
+ # TR = Uygulamayı başlatır
173
+ # EN = Launches the application
174
+