import cv2 import numpy as np import gradio as gr def apply_retro_filter(frame): retro_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, retro_filter) def apply_cartoon_filter(frame): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.medianBlur(gray, 5) edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 10) color = cv2.bilateralFilter(frame, 9, 250, 250) cartoon = cv2.bitwise_and(color, color, mask=edges) return cartoon def apply_oil_painting_filter(frame): oil_painting = cv2.edgePreservingFilter(frame, flags=2, sigma_s=50, sigma_r=0.4) return oil_painting def apply_emboss_filter(frame): kernel = np.array([[0, -1, -1], [1, 0, -1], [1, 1, 0]]) emboss = cv2.filter2D(frame, -1, kernel) return emboss def adjust_brightness_contrast(frame, alpha=1.2, beta=30): return cv2.convertScaleAbs(frame, alpha=alpha, beta=beta) def apply_pencil_drawing_filter(frame): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) inverted = cv2.bitwise_not(gray) blurred = cv2.GaussianBlur(inverted, (21, 21), 0) inverted_blurred = cv2.bitwise_not(blurred) pencil_drawing = cv2.divide(gray, inverted_blurred, scale=256.0) return pencil_drawing def apply_pastel_filter(frame): blurred = cv2.GaussianBlur(frame, (15, 15), 0) pastel_effect = cv2.addWeighted(frame, 0.5, blurred, 0.5, 0) return pastel_effect def apply_hdr_filter(frame): hdr = cv2.detailEnhance(frame, sigma_s=12, sigma_r=0.15) return hdr def apply_summer_filter(frame): summer_filter = np.array([[0.272, 0.534, 0.131], [0.349, 0.686, 0.168], [0.393, 0.769, 0.189]]) summer = cv2.transform(frame, summer_filter) return summer def apply_winter_filter(frame): winter_filter = np.array([[0.272, 0.534, 0.769], [0.349, 0.686, 0.534], [0.393, 0.131, 0.272]]) winter = cv2.transform(frame, winter_filter) return winter def apply_glitch_filter(frame): rows, cols, _ = frame.shape red_channel = frame[:, :, 0] green_channel = frame[:, :, 1] blue_channel = frame[:, :, 2] red_channel_shifted = np.roll(red_channel, 5, axis=0) green_channel_shifted = np.roll(green_channel, -5, axis=0) glitched_frame = np.zeros_like(frame) glitched_frame[:, :, 0] = red_channel_shifted glitched_frame[:, :, 1] = green_channel_shifted glitched_frame[:, :, 2] = blue_channel num_segments = np.random.randint(5, 10) for _ in range(num_segments): y_start = np.random.randint(0, rows) y_end = min(y_start + np.random.randint(5, 20), rows) x_start = np.random.randint(0, cols) x_end = min(x_start + np.random.randint(5, 20), cols) shift_value = np.random.randint(-10, 10) glitched_frame[y_start:y_end, x_start:x_end] = np.roll(glitched_frame[y_start:y_end, x_start:x_end], shift_value, axis=1) return glitched_frame def apply_glass_shatter_filter(frame): rows, cols, _ = frame.shape glitched_frame = frame.copy() num_segments = np.random.randint(5, 10) segment_width = cols // num_segments for i in range(num_segments): x_start = i * segment_width x_end = (i + 1) * segment_width if i < num_segments - 1 else cols shift_direction = np.random.choice([-1, 1]) shift_amount = np.random.randint(5, 20) glitched_segment = np.roll(glitched_frame[:, x_start:x_end], shift_amount * shift_direction, axis=0) glitched_frame[:, x_start:x_end] = glitched_segment return glitched_frame def apply_grayscale_filter(frame): return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) def apply_filter(filter_type, input_image=None): frame = input_image.copy() if input_image is not None else None if frame is None: return "Görüntü bulunamadı!" if filter_type == "Retro": return apply_retro_filter(frame) elif filter_type == "Cartoon": return apply_cartoon_filter(frame) elif filter_type == "Oil Painting": return apply_oil_painting_filter(frame) elif filter_type == "Emboss": return apply_emboss_filter(frame) elif filter_type == "Brightness & Contrast": return adjust_brightness_contrast(frame, alpha=1.2, beta=30) elif filter_type == "Pencil Drawing": return apply_pencil_drawing_filter(frame) elif filter_type == "Pastel": return apply_pastel_filter(frame) elif filter_type == "HDR": return apply_hdr_filter(frame) elif filter_type == "Summer": return apply_summer_filter(frame) elif filter_type == "Winter": return apply_winter_filter(frame) elif filter_type == "Glitch": return apply_glitch_filter(frame) elif filter_type == "Glass Shatter": return apply_glass_shatter_filter(frame) elif filter_type == "Grayscale": return apply_grayscale_filter(frame) def reset_images(): """Yüklenen ve filtreli görüntüleri temizlemek için kullanılır.""" return None, None # Gradio arayüz tanımlamaları with gr.Blocks(css=""" .main { background-color: #1f1f1f; } .gradio-container { background-color: #1f1f1f; } .custom-button { background-color: #00163a; color: white; border-radius: 10px; border: none; padding: 10px 20px; font-size: 16px; cursor: pointer; } .custom-button:hover { background-color: #001c4d; } #header { text-align: center; color: #ffffff; } body { background-color: #000f26; } #input-image, #output-image { background-color: #000f26; padding: 20px; border-radius: 10px; } #filter-dropdown { background-color: #00163a; /* Burada arka plan rengini değiştirdik */ color: #00ccff; border: 2px solid #00ccff; padding: 10px; border-radius: 10px; font-size: 16px; text-align: center; } """) as demo: gr.Markdown("

Görüntü Filtreleri Uygulaması

") filter_type = gr.Dropdown( label="Filtre Seçin", choices=["Retro", "Grayscale", "Cartoon", "Oil Painting", "Emboss", "Brightness & Contrast", "Pencil Drawing", "Pastel", "HDR", "Summer", "Winter", "Glass Shatter", "Glitch"], value="Retro", elem_id="filter-dropdown" ) with gr.Row(): input_image = gr.Image(label="Resim Yükle", type="numpy", elem_id="input-image", width=300, height=300) output_image = gr.Image(label="Filtre Uygulandı", elem_id="output-image", width=300, height=300) with gr.Row(): apply_button = gr.Button("Filtreyi Uygula", elem_id="apply-button", elem_classes="custom-button") reset_button = gr.Button("Sıfırla", elem_id="reset-button", elem_classes="custom-button") apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image) reset_button.click(fn=reset_images, inputs=[], outputs=[input_image, output_image]) demo.launch(share = True)