Spaces:
Sleeping
Sleeping
File size: 7,208 Bytes
fb070f7 bc9e164 0101638 bc9e164 0101638 bc9e164 0101638 bc9e164 0101638 bc9e164 0101638 aa03475 0101638 aa03475 0101638 bc9e164 fb070f7 bc9e164 fb070f7 bc9e164 fb070f7 bc9e164 fb070f7 bc9e164 fb070f7 bc9e164 982d35f bc9e164 982d35f bc9e164 982d35f bc9e164 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
import cv2
import numpy as np
import gradio as gr
# Farklı filtre fonksiyonları
def apply_gaussian_blur(frame):
return cv2.GaussianBlur(frame, (15, 15), 0)
def apply_sharpening_filter(frame):
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
return cv2.filter2D(frame, -1, kernel)
def apply_edge_detection(frame):
return cv2.Canny(frame, 100, 200)
def apply_invert_filter(frame):
return cv2.bitwise_not(frame)
def adjust_brightness_contrast(frame, alpha=1.0, beta=50):
return cv2.convertScaleAbs(frame, alpha=alpha, beta=beta)
def apply_grayscale_filter(frame):
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
def apply_sepia_filter(frame):
sepia_filter = np.array([[0.272, 0.534, 0.131],
[0.349, 0.686, 0.168],
[0.393, 0.769, 0.189]])
return cv2.transform(frame, sepia_filter)
def apply_fall_filter(frame):
fall_filter = np.array([[0.393, 0.769, 0.189],
[0.349, 0.686, 0.168],
[0.272, 0.534, 0.131]])
return cv2.transform(frame, fall_filter)
def apply_emboss_filter(frame):
kernel = np.array([[2, 0, 0], [0, -1, 0], [0, 0, -2]])
return cv2.filter2D(frame, -1, kernel)
def apply_cartoon_filter(frame):
# Renkleri yumuşatmak için bilateral filtre
color = cv2.bilateralFilter(frame, d=9, sigmaColor=75, sigmaSpace=75)
# Kenar tespiti için gri tonlamalı görüntü ve adaptive threshold kullanımı
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.adaptiveThreshold(
gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9
)
# Kenarları renklere uygula
cartoon = cv2.bitwise_and(color, color, mask=edges)
return cartoon
def apply_motion_blur(frame):
kernel_size = 15
kernel = np.zeros((kernel_size, kernel_size))
kernel[int((kernel_size - 1)/2), :] = np.ones(kernel_size)
kernel = kernel / kernel_size
return cv2.filter2D(frame, -1, kernel)
def apply_vignette_filter(frame):
# Vignette maskesi oluştur
rows, cols = frame.shape[:2]
kernel_x = cv2.getGaussianKernel(cols, cols / 2)
kernel_y = cv2.getGaussianKernel(rows, rows / 2)
kernel = kernel_y * kernel_x.T
mask = 255 * kernel / np.max(kernel)
vignette = np.zeros_like(frame, dtype=np.float32) # Daha hassas türde işlem yapma
for i in range(3): # Her renk kanalı için vignette uygulama
vignette[:, :, i] = frame[:, :, i] * mask
# Değerleri uint8 formatına dönüştürme
vignette = np.clip(vignette, 0, 255).astype(np.uint8)
return vignette
def apply_pencil_sketch(frame):
gray, sketch = cv2.pencilSketch(frame, sigma_s=60, sigma_r=0.07, shade_factor=0.05)
return sketch
def apply_hdr_filter(frame):
return cv2.detailEnhance(frame, sigma_s=12, sigma_r=0.15)
def apply_summer_filter(frame):
summer_filter = np.array([[0.272, 0.543, 0.131],
[0.349, 0.686, 0.168],
[0.393, 0.769, 0.189]])
return cv2.transform(frame, summer_filter)
def apply_warm_filter(frame):
increase_red = cv2.addWeighted(frame, 1, np.array([0, 20, 0]), 0.5, 0)
return increase_red
def apply_cold_filter(frame):
decrease_red = cv2.addWeighted(frame, 1, np.array([0, -20, 0]), 0.5, 0)
return decrease_red
def apply_solarize_filter(frame):
return cv2.bitwise_not(frame)
def apply_color_boost(frame):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
hsv[...,1] = cv2.add(hsv[...,1], 50)
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
def apply_dilation_filter(frame):
kernel = np.ones((5, 5), np.uint8)
return cv2.dilate(frame, kernel, iterations=1)
# Filtre uygulama fonksiyonu
def apply_filter(filter_type, input_image=None):
if input_image is not None:
frame = input_image
else:
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cap.release()
if not ret:
return "Web kameradan görüntü alınamadı"
if filter_type == "Gaussian Blur":
return apply_gaussian_blur(frame)
elif filter_type == "Sharpen":
return apply_sharpening_filter(frame)
elif filter_type == "Edge Detection":
return apply_edge_detection(frame)
elif filter_type == "Invert":
return apply_invert_filter(frame)
elif filter_type == "Brightness":
return adjust_brightness_contrast(frame, alpha=1.0, beta=50)
elif filter_type == "Grayscale":
return apply_grayscale_filter(frame)
elif filter_type == "Sepia":
return apply_sepia_filter(frame)
elif filter_type == "Sonbahar":
return apply_fall_filter(frame)
elif filter_type == "Emboss":
return apply_emboss_filter(frame)
elif filter_type == "Cartoon":
return apply_cartoon_filter(frame)
elif filter_type == "Motion Blur":
return apply_motion_blur(frame)
elif filter_type == "Vignette":
return apply_vignette_filter(frame)
elif filter_type == "Pencil Sketch":
return apply_pencil_sketch(frame)
elif filter_type == "HDR":
return apply_hdr_filter(frame)
elif filter_type == "Summer":
return apply_summer_filter(frame)
elif filter_type == "Warm":
return apply_warm_filter(frame)
elif filter_type == "Cold":
return apply_cold_filter(frame)
elif filter_type == "Solarize":
return apply_solarize_filter(frame)
elif filter_type == "Color Boost":
return apply_color_boost(frame)
elif filter_type == "Dilation":
return apply_dilation_filter(frame)
# Gradio arayüzü
with gr.Blocks(css="""
.gr-button {
width: 80px; /* Buton genişliği */
height: 30px; /* Buton yüksekliği */
font-size: 12px; /* Yazı boyutu */
}
.gr-image {
width: 400px; /* Sabit genişlik */
height: 300px; /* Sabit yükseklik */
}
#image-upload {
width: 400px; /* Sabit genişlik */
height: 300px; /* Sabit yükseklik */
}
#output-image {
width: 400px; /* Sabit genişlik */
height: 300px; /* Sabit yükseklik */
}
""") as demo:
gr.Markdown("# Resim Filtreleme")
# Filtre seçenekleri
filter_type = gr.Dropdown(
label="Filtre Seçin",
choices=["Gaussian Blur", "Sharpen", "Edge Detection", "Invert", "Brightness", "Grayscale", "Sepia", "Sonbahar",
"Emboss", "Cartoon", "Motion Blur", "Vignette", "Pencil Sketch", "HDR", "Summer", "Warm", "Cold",
"Solarize", "Color Boost", "Dilation"],
value="Gaussian Blur"
)
with gr.Row():
# Görüntü yükleme alanı
input_image = gr.Image(label="Resim Yükle", type="numpy")
# Çıktı için görüntü
output_image = gr.Image(label="Filtre Uygulandı")
# Filtre uygula butonu
apply_button = gr.Button("Filtreyi Uygula")
# Butona tıklanınca filtre uygulama fonksiyonu
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
# Gradio arayüzünü başlat
demo.launch()
|