import cv2 as cv import gradio as gr import numpy as np #Farklı filtreler için fonksiyon tanımlama #1 def gaussian_blur_filter(frame): return cv.GaussianBlur(frame, (15, 15), 0) #2 def median_blur_filter(frame): return cv.medianBlur(frame,5) #3 def black_and_white_filter(frame): return cv.cvtColor(frame, cv.COLOR_BGR2GRAY) #4 def vertical_mirror_filter(frame): return cv.flip(frame,0) #5 def horizontal_mirror_filter(frame): return cv.flip(frame,1) #6 def edge_detection(frame): return cv.Canny(frame, 100, 200) #7 def sharpening_filter(frame): kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) return cv.filter2D(frame, -1, kernel) #8 def 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 cv.transform(frame, sepia_filter) #9 def adjust_brightness_contrast(frame, alpha=1.0, beta=50): return cv.convertScaleAbs(frame, alpha=alpha, beta=beta) #10 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 cv.transform(frame, fall_filter) #11 def invert_filter(frame): return cv.bitwise_not(frame) # Resim Filtre uygulama fonksiyonu def apply_filter(filter_type, input_image=None): if input_image is not None: frame = input_image else: cap = cv.VideoCapture(0) ret, frame = cap.read() cap.release() if not ret: return "Web kameradan görüntü alınamadı" #Seçime göre fonksiyonları çağırma if filter_type == "Gaussian Blur": return gaussian_blur_filter(frame) elif filter_type == "Median Blur": return median_blur_filter(frame) elif filter_type == "Gray Scale": return black_and_white_filter(frame) elif filter_type == "Vertical Mirror": return vertical_mirror_filter(frame) elif filter_type == "Horizontal Mirror": return horizontal_mirror_filter(frame) elif filter_type == "Edge Detection": return edge_detection(frame) elif filter_type == "Sharpen": return sharpening_filter(frame) elif filter_type == "Sepia": return sepia_filter(frame) elif filter_type == "Brightness": return adjust_brightness_contrast(frame, alpha=1.0, beta=50) elif filter_type == "Sonbahar": return apply_fall_filter(frame) elif filter_type == "Invert": return invert_filter(frame) #Gradio Arayüzü with gr.Blocks() as demo: gr.Markdown("# Fotoğraf Filtreleme") # Filtre seçeneklerini dropboza tanımlama filter_type = gr.Dropdown( label="Lütfen Aşağıdan Filtre Seçiniz", choices=["Gaussian Blur","Median Blur","Gray Scale","Vertical Mirror","Horizontal Mirror", "Edge Detection", "Sharpen", "Sepia", "Brightness", "Invert", "Sonbahar"], value="Gaussian Blur" ) # Görüntü yükleme alanı input_image = gr.Image(label="Bir Fotoğraf Yükleyiniz", type="numpy") # Çıktı için görüntü output_image = gr.Image(label="Filtre Uygulama Alanı") # Resim Filtre uygula butonu apply_button = gr.Button("Filtreyi Uygula") # Butona tıklanınca filtre uygulama fonksiyonunu çalıştırır. apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image) #Resim yüklenince filtereyi otomatik uygular. input_image.change(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image) #dropdown değiştiğinde filtereyi otomatik uygular. filter_type.change(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image) # Gradio arayüzünü başlat demo.launch()