Spaces:
Running
Running
import cv2 | |
import numpy as np | |
import gradio as gr | |
import random | |
def apply_cartoon_filter(frame): | |
"""Cartoon Filter""" | |
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, 11, 7) | |
color = cv2.bilateralFilter(frame, 9, 300, 300) | |
cartoon = cv2.bitwise_and(color, color, mask=edges) | |
return cartoon | |
def apply_neon_effect(frame): | |
"""Neon Light Filter""" | |
# Intensify colors | |
frame_neon = frame.copy().astype(np.float32) | |
frame_neon = np.clip(frame_neon * 1.5, 0, 255).astype(np.uint8) | |
# Highlight edges | |
edges = cv2.Canny(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), 100, 200) | |
edges_colored = cv2.applyColorMap(edges, cv2.COLORMAP_JET) | |
# Blend | |
result = cv2.addWeighted(frame_neon, 0.7, edges_colored, 0.3, 0) | |
return result | |
def apply_pixelate_effect(frame, pixel_size=15): | |
"""Pixelate Effect""" | |
h, w = frame.shape[:2] | |
small = cv2.resize(frame, (w//pixel_size, h//pixel_size), interpolation=cv2.INTER_LINEAR) | |
return cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST) | |
def apply_glitch_effect(frame): | |
"""Glitch Filter""" | |
glitched = frame.copy() | |
# Randomly shift color channels | |
glitched[:, :, 0] = np.roll(glitched[:, :, 0], random.randint(-50, 50), axis=0) | |
glitched[:, :, 1] = np.roll(glitched[:, :, 1], random.randint(-50, 50), axis=1) | |
# Add noise to random areas | |
noise = np.random.randint(0, 255, frame.shape, dtype=np.uint8) | |
glitched = cv2.addWeighted(glitched, 0.7, noise, 0.3, 0) | |
return glitched | |
def apply_watercolor_effect(frame): | |
"""Watercolor Effect""" | |
# Smooth using bilateral filtering | |
frame_soft = cv2.bilateralFilter(frame, 9, 75, 75) | |
# Highlight edges | |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
edges = cv2.Canny(gray, 100, 200) | |
edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR) | |
# Blend | |
result = cv2.addWeighted(frame_soft, 0.8, edges, 0.2, 0) | |
return result | |
def apply_upscale(frame, scale_factor=1.5): | |
""" | |
Upscaling Effect | |
Args: | |
frame (numpy.ndarray): Input Image | |
scale_factor (float): Scaling Factor (default 1.5) | |
Returns: | |
numpy.ndarray: Upscaled Image | |
""" | |
interpolation_methods = [ | |
cv2.INTER_CUBIC, | |
cv2.INTER_LANCZOS4 | |
] | |
method = random.choice(interpolation_methods) | |
height, width = frame.shape[:2] | |
new_height = int(height * scale_factor) | |
new_width = int(width * scale_factor) | |
upscaled = cv2.resize(frame, (new_width, new_height), interpolation=method) | |
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]]) | |
sharpened = cv2.filter2D(upscaled, -1, kernel) | |
return sharpened | |
def apply_filter(filter_type, input_image=None): | |
if input_image is None: | |
cap = cv2.VideoCapture(0) | |
ret, frame = cap.read() | |
cap.release() | |
if not ret: | |
return "Failed to capture image from webcam" | |
else: | |
frame = input_image | |
if filter_type == "Upscale": | |
return apply_upscale(frame) | |
elif filter_type == "Cartoon": | |
return apply_cartoon_filter(frame) | |
elif filter_type == "Neon Light": | |
return apply_neon_effect(frame) | |
elif filter_type == "Pixelate": | |
return apply_pixelate_effect(frame) | |
elif filter_type == "Glitch": | |
return apply_glitch_effect(frame) | |
elif filter_type == "Watercolor": | |
return apply_watercolor_effect(frame) | |
# Gradio interface | |
with gr.Blocks() as demo: | |
gr.Markdown('# <p align="center"> OpenCV Image Effects </p>') | |
# Filter options | |
filter_type = gr.Dropdown( | |
label="Select Filter", | |
choices=["Upscale","Cartoon", "Neon Light", "Pixelate", "Glitch", "Watercolor"], | |
value="Upscale" | |
) | |
with gr.Row(): | |
input_image = gr.Image(label="Upload Image", type="numpy") | |
output_image = gr.Image(label="Filtered Image") | |
# Apply filter button | |
apply_button = gr.Button("Apply Filter") | |
# Apply filter function on button click | |
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image) | |
demo.launch() | |