Spaces:
Sleeping
Sleeping
| import cv2 | |
| import numpy as np | |
| import gradio as gr | |
| import matplotlib.pyplot as plt | |
| color_ranges = [ | |
| ([100, 150, 0], [140, 255, 255]), # Blue range | |
| ([35, 100, 100], [85, 255, 255]) # Green range | |
| ] | |
| # 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) | |
| # 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 == "Random Colorful Dots": | |
| return apply_porcelain_splatter_effect(frame) | |
| elif filter_type == "Picasso": | |
| return split_and_shuffle_image(frame) | |
| def apply_porcelain_splatter_effect(image): | |
| # Assume 'image' is an RGB NumPy array | |
| # Get image dimensions | |
| height, width, _ = image.shape | |
| # Create a blank layer for the porcelain splatter effect | |
| splatter_layer = np.zeros((height, width, 3), dtype=np.uint8) | |
| # Define colors for splatter pattern | |
| colors = [ | |
| (255, 140, 0), # orange | |
| (0, 128, 0), # green | |
| (0, 0, 255), # red | |
| (70, 130, 180) # matte blue | |
| ] | |
| # Add random splatter patterns | |
| for _ in range(1000): | |
| # Random position for splatter | |
| x, y = np.random.randint(0, width), np.random.randint(0, height) | |
| # Random color from defined palette | |
| color = colors[np.random.randint(0, len(colors))] | |
| # Random radius for splatter | |
| radius = np.random.randint(1, 10) | |
| # Draw a small circle on the splatter layer | |
| cv2.circle(splatter_layer, (x, y), radius, color, -1) | |
| # Blend the splatter layer with the original image to get the porcelain effect | |
| alpha = 0.4 # transparency for splatter layer | |
| porcelain_image = cv2.addWeighted(image, 1 - alpha, splatter_layer, alpha, 0) | |
| return porcelain_image | |
| def split_and_shuffle_image(image): | |
| # Determine the height and width of each piece | |
| h, w, _ = image.shape | |
| h_split, w_split = h // 3, w // 3 | |
| # Split the image into 9 pieces | |
| pieces = [] | |
| for i in range(3): | |
| for j in range(3): | |
| piece = image[i * h_split:(i + 1) * h_split, j * w_split:(j + 1) * w_split] | |
| pieces.append(piece) | |
| # Shuffle the pieces | |
| np.random.shuffle(pieces) | |
| # Reconstruct the shuffled image | |
| shuffled_image = np.zeros_like(image) | |
| idx = 0 | |
| for i in range(3): | |
| for j in range(3): | |
| shuffled_image[i * h_split:(i + 1) * h_split, j * w_split:(j + 1) * w_split] = pieces[idx] | |
| idx += 1 | |
| return shuffled_image | |
| # Gradio arayüzü | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Image Proccessing Exercise 01 - Tech Istanbul") | |
| gr.Markdown("# I would like to thank to Murat hoca & Tech Istanbul for teaching us image processing ") | |
| gr.Markdown("# I added 'Picasso' and 'Random Colorful Dots' filters to the options. They are at the top ") | |
| # Filtre seçenekleri | |
| filter_type = gr.Dropdown( | |
| label="Filtre Seçin", | |
| choices=["Picasso","Random Colorful Dots","Gaussian Blur", "Sharpen", "Edge Detection", "Invert", "Brightness", "Grayscale", "Sepia", "Sonbahar"], | |
| value="Picasso" | |
| ) | |
| # 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() | |