Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,227 @@
|
|
| 1 |
-
import cv2
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
fps = int(cap.get(cv.CAP_PROP_FPS)) # Saniyedeki frame sayısı
|
| 14 |
-
width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH)) # Genişlik
|
| 15 |
-
height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT)) # Yükseklik
|
| 16 |
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
device_label = "Device: GPU" if cuda_enabled else "Device: CPU"
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
cv.destroyAllWindows()
|
|
|
|
| 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_bilateral_filter(frame):
|
| 32 |
+
return cv2.bilateralFilter(frame, 9, 75, 75)
|
| 33 |
|
| 34 |
+
def apply_darkening_filter(frame, alpha=0.5):
|
| 35 |
+
return cv2.convertScaleAbs(frame, alpha=alpha, beta=0)
|
| 36 |
+
|
| 37 |
+
def apply_histogram_equalization(frame):
|
| 38 |
+
if len(frame.shape) == 2: # Grayscale
|
| 39 |
+
return cv2.equalizeHist(frame)
|
| 40 |
+
else:
|
| 41 |
+
ycrcb = cv2.cvtColor(frame, cv2.COLOR_BGR2YCrCb)
|
| 42 |
+
channels = cv2.split(ycrcb)
|
| 43 |
+
cv2.equalizeHist(channels[0], channels[0])
|
| 44 |
+
ycrcb = cv2.merge(channels) # Burada düzeltme yapıldı
|
| 45 |
+
return cv2.cvtColor(ycrcb, cv2.COLOR_YCrCb2BGR)
|
| 46 |
+
|
| 47 |
+
def apply_median_blur(frame):
|
| 48 |
+
return cv2.medianBlur(frame, 15)
|
| 49 |
+
|
| 50 |
+
def apply_dilation(frame, kernel_size=5):
|
| 51 |
+
kernel = np.ones((kernel_size, kernel_size), np.uint8)
|
| 52 |
+
return cv2.dilate(frame, kernel, iterations=1)
|
| 53 |
+
|
| 54 |
+
def apply_erosion(frame, kernel_size=5):
|
| 55 |
+
kernel = np.ones((kernel_size, kernel_size), np.uint8)
|
| 56 |
+
return cv2.erode(frame, kernel, iterations=1)
|
| 57 |
+
|
| 58 |
+
def apply_line_detection(frame):
|
| 59 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 60 |
+
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
|
| 61 |
+
lines = cv2.HoughLines(edges, 1, np.pi/180, 200)
|
| 62 |
+
if lines is not None:
|
| 63 |
+
for rho, theta in lines[:,0]:
|
| 64 |
+
a = np.cos(theta)
|
| 65 |
+
b = np.sin(theta)
|
| 66 |
+
x0 = a * rho
|
| 67 |
+
y0 = b * rho
|
| 68 |
+
x1 = int(x0 + 1000 * (-b))
|
| 69 |
+
x2 = int(x0 - 1000 * (-b))
|
| 70 |
+
y1 = int(y0 + 1000 * (a))
|
| 71 |
+
y2 = int(y0 - 1000 * (a))
|
| 72 |
+
cv2.line(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
| 73 |
+
return frame
|
| 74 |
+
|
| 75 |
+
def apply_contour_detection(frame):
|
| 76 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 77 |
+
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 78 |
+
edged = cv2.Canny(blurred, 50, 150)
|
| 79 |
+
contours, _ = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 80 |
+
cv2.drawContours(frame, contours, -1, (0, 255, 0), 2)
|
| 81 |
+
return frame
|
| 82 |
+
|
| 83 |
+
def apply_box_blur(frame):
|
| 84 |
+
return cv2.blur(frame, (15, 15))
|
| 85 |
+
|
| 86 |
+
def apply_emboss_filter(frame):
|
| 87 |
+
kernel = np.array([[-2, -1, 0], [-1, 1, 1], [0, 1, 2]])
|
| 88 |
+
return cv2.filter2D(frame, -1, kernel)
|
| 89 |
+
|
| 90 |
+
def apply_sobel_edge_detection(frame):
|
| 91 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 92 |
+
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)
|
| 93 |
+
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5)
|
| 94 |
+
sobel = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
|
| 95 |
+
return cv2.convertScaleAbs(sobel)
|
| 96 |
+
|
| 97 |
+
def apply_thresholding(frame, threshold_value=127):
|
| 98 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 99 |
+
_, thresh = cv2.threshold(gray, threshold_value, 255, cv2.THRESH_BINARY)
|
| 100 |
+
return thresh
|
| 101 |
+
|
| 102 |
+
def apply_color_quantization(frame, k=8):
|
| 103 |
+
Z = frame.reshape((-1, 3))
|
| 104 |
+
Z = np.float32(Z)
|
| 105 |
+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
|
| 106 |
+
_, label, center = cv2.kmeans(Z, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
|
| 107 |
+
center = np.uint8(center)
|
| 108 |
+
res = center[label.flatten()]
|
| 109 |
+
return res.reshape((frame.shape))
|
| 110 |
+
|
| 111 |
+
# Filtre açıklamaları
|
| 112 |
+
filter_descriptions = {
|
| 113 |
+
"Gaussian Blur": "Resmi bulanıklaştırır. Bu filtre, resimdeki gürültüyü azaltmak için kullanılır.",
|
| 114 |
+
"Sharpen": "Resmi netleştirir. Bu filtre, resimdeki kenarları daha belirgin hale getirir.",
|
| 115 |
+
"Edge Detection": "Canny Edge Detection algoritmasını kullanarak resimdeki kenarları tespit eder.",
|
| 116 |
+
"Invert": "Resmin renklerini tersine çevirir.",
|
| 117 |
+
"Brightness": "Resmin parlaklığını ve kontrastını ayarlar.",
|
| 118 |
+
"Grayscale": "Resmi gri tonlamalı hale getirir.",
|
| 119 |
+
"Sepia": "Resmi sepiya tonlarıyla işler.",
|
| 120 |
+
"Bilateral": "Kenar koruyarak resmi bulanıklaştırır.",
|
| 121 |
+
"Darkening": "Resmi karartır.",
|
| 122 |
+
"Histogram Equalization": "Resmin histogramını eşitleyerek kontrastı artırır.",
|
| 123 |
+
"Median Blur": "Medyan filtresi ile resmi bulanıklaştırır. Bu filtre, gürültüyü azaltmak için kullanılır.",
|
| 124 |
+
"Dilation": "Resimdeki beyaz bölgeleri genişletir.",
|
| 125 |
+
"Erosion": "Resimdeki beyaz bölgeleri daraltır.",
|
| 126 |
+
"Line Detection": "Hough dönüşümü ile resimdeki doğruları tespit eder.",
|
| 127 |
+
"Contour Detection": "Resimdeki konturları tespit eder ve çizer.",
|
| 128 |
+
"Box Blur": "Basit bir bulanıklaştırma filtresi.",
|
| 129 |
+
"Emboss": "Resmi kabartma efektiyle işler.",
|
| 130 |
+
"Sobel Edge Detection": "Sobel operatörü ile kenarları tespit eder.",
|
| 131 |
+
"Thresholding": "Eşikleme ile resmi ikili (siyah-beyaz) hale getirir.",
|
| 132 |
+
"Color Quantization": "Renk sayısını azaltarak resmi daha basit bir renk paletiyle gösterir."
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
# Filtre uygulama fonksiyonu
|
| 136 |
+
def apply_filter(filter_type, input_image=None):
|
| 137 |
+
if input_image is not None:
|
| 138 |
+
frame = input_image
|
| 139 |
+
else:
|
| 140 |
+
cap = cv2.VideoCapture(0)
|
| 141 |
+
ret, frame = cap.read()
|
| 142 |
+
cap.release()
|
| 143 |
+
if not ret:
|
| 144 |
+
return "Web kameradan görüntü alınamadı"
|
| 145 |
|
| 146 |
+
if filter_type == "Gaussian Blur":
|
| 147 |
+
return apply_gaussian_blur(frame)
|
| 148 |
+
elif filter_type == "Sharpen":
|
| 149 |
+
return apply_sharpening_filter(frame)
|
| 150 |
+
elif filter_type == "Edge Detection":
|
| 151 |
+
return apply_edge_detection(frame)
|
| 152 |
+
elif filter_type == "Invert":
|
| 153 |
+
return apply_invert_filter(frame)
|
| 154 |
+
elif filter_type == "Brightness":
|
| 155 |
+
return adjust_brightness_contrast(frame, alpha=1.0, beta=50)
|
| 156 |
+
elif filter_type == "Grayscale":
|
| 157 |
+
return apply_grayscale_filter(frame)
|
| 158 |
+
elif filter_type == "Sepia":
|
| 159 |
+
return apply_sepia_filter(frame)
|
| 160 |
+
elif filter_type == "Bilateral":
|
| 161 |
+
return apply_bilateral_filter(frame)
|
| 162 |
+
elif filter_type == "Darkening":
|
| 163 |
+
return apply_darkening_filter(frame)
|
| 164 |
+
elif filter_type == "Histogram Equalization":
|
| 165 |
+
return apply_histogram_equalization(frame)
|
| 166 |
+
elif filter_type == "Median Blur":
|
| 167 |
+
return apply_median_blur(frame)
|
| 168 |
+
elif filter_type == "Dilation":
|
| 169 |
+
return apply_dilation(frame)
|
| 170 |
+
elif filter_type == "Erosion":
|
| 171 |
+
return apply_erosion(frame)
|
| 172 |
+
elif filter_type == "Line Detection":
|
| 173 |
+
return apply_line_detection(frame)
|
| 174 |
+
elif filter_type == "Contour Detection":
|
| 175 |
+
return apply_contour_detection(frame)
|
| 176 |
+
elif filter_type == "Box Blur":
|
| 177 |
+
return apply_box_blur(frame)
|
| 178 |
+
elif filter_type == "Emboss":
|
| 179 |
+
return apply_emboss_filter(frame)
|
| 180 |
+
elif filter_type == "Sobel Edge Detection":
|
| 181 |
+
return apply_sobel_edge_detection(frame)
|
| 182 |
+
elif filter_type == "Thresholding":
|
| 183 |
+
return apply_thresholding(frame)
|
| 184 |
+
elif filter_type == "Color Quantization":
|
| 185 |
+
return apply_color_quantization(frame)
|
| 186 |
|
| 187 |
+
# Gradio arayüzü
|
| 188 |
+
with gr.Blocks() as demo:
|
| 189 |
+
gr.Markdown("# Web Kameradan Canlı Filtreleme")
|
| 190 |
+
|
| 191 |
+
# Filtre seçenekleri
|
| 192 |
+
filter_type = gr.Dropdown(
|
| 193 |
+
label="Filtre Seçin",
|
| 194 |
+
choices=list(filter_descriptions.keys()),
|
| 195 |
+
value="Gaussian Blur"
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
# Filtre açıklaması
|
| 199 |
+
filter_description = gr.Markdown(label="Filtre Açıklaması", value=filter_descriptions["Gaussian Blur"])
|
| 200 |
+
|
| 201 |
+
# Görüntü yükleme alanı
|
| 202 |
+
input_image = gr.Image(label="Resim Yükle", type="numpy", height=200, width=200)
|
| 203 |
+
|
| 204 |
+
# Çıktı için görüntü
|
| 205 |
+
output_image = gr.Image(label="Filtre Uygulandı", height=200, width=200)
|
| 206 |
+
|
| 207 |
+
# Giriş ve çıkış resimlerini yan yana gösterme
|
| 208 |
+
with gr.Row():
|
| 209 |
+
with gr.Column(scale=1):
|
| 210 |
+
gr.Markdown("### Giriş Resmi")
|
| 211 |
+
input_image
|
| 212 |
+
with gr.Column(scale=1):
|
| 213 |
+
gr.Markdown("### Çıkış Resmi")
|
| 214 |
+
output_image
|
| 215 |
+
|
| 216 |
+
# Filtre seçildiğinde açıklamayı güncelleme
|
| 217 |
+
def update_description(filter_type):
|
| 218 |
+
return filter_descriptions[filter_type]
|
| 219 |
+
|
| 220 |
+
filter_type.change(fn=update_description, inputs=filter_type, outputs=filter_description)
|
| 221 |
|
| 222 |
+
# Resim yüklendiğinde veya filtre değiştirildiğinde otomatik olarak filtre uygulama
|
| 223 |
+
input_image.change(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
|
| 224 |
+
filter_type.change(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
|
| 225 |
|
| 226 |
+
# Gradio arayüzünü başlat
|
| 227 |
+
demo.launch()
|
|
|