Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,138 +1,134 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import numpy as np
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import random
|
| 5 |
-
|
| 6 |
-
def apply_cartoon_filter(frame):
|
| 7 |
-
"""Cartoon Filter"""
|
| 8 |
-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 9 |
-
gray = cv2.medianBlur(gray, 5)
|
| 10 |
-
edges = cv2.adaptiveThreshold(gray, 255,
|
| 11 |
-
cv2.ADAPTIVE_THRESH_MEAN_C,
|
| 12 |
-
cv2.THRESH_BINARY, 11, 7)
|
| 13 |
-
color = cv2.bilateralFilter(frame, 9, 300, 300)
|
| 14 |
-
cartoon = cv2.bitwise_and(color, color, mask=edges)
|
| 15 |
-
return cartoon
|
| 16 |
-
|
| 17 |
-
def apply_neon_effect(frame):
|
| 18 |
-
"""Neon Light Filter"""
|
| 19 |
-
# Intensify colors
|
| 20 |
-
frame_neon = frame.copy().astype(np.float32)
|
| 21 |
-
frame_neon = np.clip(frame_neon * 1.5, 0, 255).astype(np.uint8)
|
| 22 |
-
|
| 23 |
-
# Highlight edges
|
| 24 |
-
edges = cv2.Canny(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), 100, 200)
|
| 25 |
-
edges_colored = cv2.applyColorMap(edges, cv2.COLORMAP_JET)
|
| 26 |
-
|
| 27 |
-
# Blend
|
| 28 |
-
result = cv2.addWeighted(frame_neon, 0.7, edges_colored, 0.3, 0)
|
| 29 |
-
return result
|
| 30 |
-
|
| 31 |
-
def apply_pixelate_effect(frame, pixel_size=15):
|
| 32 |
-
"""Pixelate Effect"""
|
| 33 |
-
h, w = frame.shape[:2]
|
| 34 |
-
small = cv2.resize(frame, (w//pixel_size, h//pixel_size), interpolation=cv2.INTER_LINEAR)
|
| 35 |
-
return cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)
|
| 36 |
-
|
| 37 |
-
def apply_glitch_effect(frame):
|
| 38 |
-
"""Glitch Filter"""
|
| 39 |
-
glitched = frame.copy()
|
| 40 |
-
|
| 41 |
-
# Randomly shift color channels
|
| 42 |
-
glitched[:, :, 0] = np.roll(glitched[:, :, 0], random.randint(-50, 50), axis=0)
|
| 43 |
-
glitched[:, :, 1] = np.roll(glitched[:, :, 1], random.randint(-50, 50), axis=1)
|
| 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 |
-
return
|
| 106 |
-
elif filter_type == "
|
| 107 |
-
return
|
| 108 |
-
elif filter_type == "
|
| 109 |
-
return
|
| 110 |
-
elif filter_type == "
|
| 111 |
-
return
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
# Apply filter function on button click
|
| 136 |
-
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
|
| 137 |
-
|
| 138 |
-
demo.launch()
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import random
|
| 5 |
+
|
| 6 |
+
def apply_cartoon_filter(frame):
|
| 7 |
+
"""Cartoon Filter"""
|
| 8 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 9 |
+
gray = cv2.medianBlur(gray, 5)
|
| 10 |
+
edges = cv2.adaptiveThreshold(gray, 255,
|
| 11 |
+
cv2.ADAPTIVE_THRESH_MEAN_C,
|
| 12 |
+
cv2.THRESH_BINARY, 11, 7)
|
| 13 |
+
color = cv2.bilateralFilter(frame, 9, 300, 300)
|
| 14 |
+
cartoon = cv2.bitwise_and(color, color, mask=edges)
|
| 15 |
+
return cartoon
|
| 16 |
+
|
| 17 |
+
def apply_neon_effect(frame):
|
| 18 |
+
"""Neon Light Filter"""
|
| 19 |
+
# Intensify colors
|
| 20 |
+
frame_neon = frame.copy().astype(np.float32)
|
| 21 |
+
frame_neon = np.clip(frame_neon * 1.5, 0, 255).astype(np.uint8)
|
| 22 |
+
|
| 23 |
+
# Highlight edges
|
| 24 |
+
edges = cv2.Canny(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), 100, 200)
|
| 25 |
+
edges_colored = cv2.applyColorMap(edges, cv2.COLORMAP_JET)
|
| 26 |
+
|
| 27 |
+
# Blend
|
| 28 |
+
result = cv2.addWeighted(frame_neon, 0.7, edges_colored, 0.3, 0)
|
| 29 |
+
return result
|
| 30 |
+
|
| 31 |
+
def apply_pixelate_effect(frame, pixel_size=15):
|
| 32 |
+
"""Pixelate Effect"""
|
| 33 |
+
h, w = frame.shape[:2]
|
| 34 |
+
small = cv2.resize(frame, (w//pixel_size, h//pixel_size), interpolation=cv2.INTER_LINEAR)
|
| 35 |
+
return cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)
|
| 36 |
+
|
| 37 |
+
def apply_glitch_effect(frame):
|
| 38 |
+
"""Glitch Filter"""
|
| 39 |
+
glitched = frame.copy()
|
| 40 |
+
|
| 41 |
+
# Randomly shift color channels
|
| 42 |
+
glitched[:, :, 0] = np.roll(glitched[:, :, 0], random.randint(-50, 50), axis=0)
|
| 43 |
+
glitched[:, :, 1] = np.roll(glitched[:, :, 1], random.randint(-50, 50), axis=1)
|
| 44 |
+
|
| 45 |
+
return glitched
|
| 46 |
+
|
| 47 |
+
def apply_watercolor_effect(frame):
|
| 48 |
+
"""Watercolor Effect"""
|
| 49 |
+
# Smooth using bilateral filtering
|
| 50 |
+
frame_soft = cv2.bilateralFilter(frame, 9, 75, 75)
|
| 51 |
+
|
| 52 |
+
# Highlight edges
|
| 53 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 54 |
+
edges = cv2.Canny(gray, 100, 200)
|
| 55 |
+
edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
|
| 56 |
+
|
| 57 |
+
# Blend
|
| 58 |
+
result = cv2.addWeighted(frame_soft, 0.8, edges, 0.2, 0)
|
| 59 |
+
return result
|
| 60 |
+
|
| 61 |
+
def apply_upscale(frame, scale_factor=1.5):
|
| 62 |
+
"""
|
| 63 |
+
Upscaling Effect
|
| 64 |
+
|
| 65 |
+
Args:
|
| 66 |
+
frame (numpy.ndarray): Input Image
|
| 67 |
+
scale_factor (float): Scaling Factor (default 1.5)
|
| 68 |
+
|
| 69 |
+
Returns:
|
| 70 |
+
numpy.ndarray: Upscaled Image
|
| 71 |
+
"""
|
| 72 |
+
interpolation_methods = [
|
| 73 |
+
cv2.INTER_CUBIC,
|
| 74 |
+
cv2.INTER_LANCZOS4
|
| 75 |
+
]
|
| 76 |
+
|
| 77 |
+
method = random.choice(interpolation_methods)
|
| 78 |
+
|
| 79 |
+
height, width = frame.shape[:2]
|
| 80 |
+
new_height = int(height * scale_factor)
|
| 81 |
+
new_width = int(width * scale_factor)
|
| 82 |
+
|
| 83 |
+
upscaled = cv2.resize(frame, (new_width, new_height), interpolation=method)
|
| 84 |
+
|
| 85 |
+
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
|
| 86 |
+
sharpened = cv2.filter2D(upscaled, -1, kernel)
|
| 87 |
+
|
| 88 |
+
return sharpened
|
| 89 |
+
|
| 90 |
+
def apply_filter(filter_type, input_image=None):
|
| 91 |
+
if input_image is None:
|
| 92 |
+
cap = cv2.VideoCapture(0)
|
| 93 |
+
ret, frame = cap.read()
|
| 94 |
+
cap.release()
|
| 95 |
+
if not ret:
|
| 96 |
+
return "Failed to capture image from webcam"
|
| 97 |
+
else:
|
| 98 |
+
frame = input_image
|
| 99 |
+
|
| 100 |
+
if filter_type == "Upscale":
|
| 101 |
+
return apply_upscale(frame)
|
| 102 |
+
elif filter_type == "Cartoon":
|
| 103 |
+
return apply_cartoon_filter(frame)
|
| 104 |
+
elif filter_type == "Neon Light":
|
| 105 |
+
return apply_neon_effect(frame)
|
| 106 |
+
elif filter_type == "Pixelate":
|
| 107 |
+
return apply_pixelate_effect(frame)
|
| 108 |
+
elif filter_type == "Glitch":
|
| 109 |
+
return apply_glitch_effect(frame)
|
| 110 |
+
elif filter_type == "Watercolor":
|
| 111 |
+
return apply_watercolor_effect(frame)
|
| 112 |
+
|
| 113 |
+
# Gradio interface
|
| 114 |
+
with gr.Blocks() as demo:
|
| 115 |
+
gr.Markdown('# <p align="center"> OpenCV Image Effects </p>')
|
| 116 |
+
|
| 117 |
+
# Filter options
|
| 118 |
+
filter_type = gr.Dropdown(
|
| 119 |
+
label="Select Filter",
|
| 120 |
+
choices=["Upscale","Cartoon", "Neon Light", "Pixelate", "Glitch", "Watercolor"],
|
| 121 |
+
value="Upscale"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
with gr.Row():
|
| 125 |
+
input_image = gr.Image(label="Upload Image", type="numpy")
|
| 126 |
+
output_image = gr.Image(label="Filtered Image")
|
| 127 |
+
|
| 128 |
+
# Apply filter button
|
| 129 |
+
apply_button = gr.Button("Apply Filter")
|
| 130 |
+
|
| 131 |
+
# Apply filter function on button click
|
| 132 |
+
apply_button.click(fn=apply_filter, inputs=[filter_type, input_image], outputs=output_image)
|
| 133 |
+
|
| 134 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|