Spaces:
Sleeping
Sleeping
File size: 5,471 Bytes
a52a7c3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
import cv2 as cv
import numpy as np
import gradio as gr
def apply_gaussian_blur(frame, density):
ksize = int(density) * 2 + 1
return cv.GaussianBlur(frame, (ksize, ksize), 0)
def apply_sharpening_filter(frame, density):
kernel = np.array([[-1, -1, -1], [-1, 8 * density, -1], [-1, -1, -1]])
return cv.filter2D(frame, -1, kernel)
def apply_edge_detection(frame, density):
return cv.Canny(frame, 100, 100 * density)
def apply_invert_filter(frame, density):
return cv.bitwise_not(frame)
def adjust_brightness_contrast(frame, density):
alpha = 1.0 + density / 50.0
beta = density * 2
return cv.convertScaleAbs(frame, alpha=alpha, beta=beta)
def apply_grayscale_filter(frame, density):
return cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
def apply_sepia_filter(frame, density):
sepia_filter = np.array([[0.272 * density, 0.534 * density, 0.131 * density],
[0.349 * density, 0.686 * density, 0.168 * density],
[0.393 * density, 0.769 * density, 0.189 * density]])
return cv.transform(frame, sepia_filter)
def apply_sketch_filter(frame, density):
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
inv = cv.bitwise_not(gray)
blur = cv.GaussianBlur(inv, (21, 21), 0)
sketch = cv.divide(gray, 255 - blur, scale=256)
return cv.multiply(sketch, density)
def apply_cartoon_filter(frame, density):
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
blur = cv.medianBlur(gray, 7)
edges = cv.adaptiveThreshold(blur, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 9, 10)
color = cv.bilateralFilter(frame, 9, 300, 300)
cartoon = cv.bitwise_and(color, color, mask=edges)
return cv.multiply(cartoon, density)
def apply_pixelate_filter(frame, density):
pixel_size = int(density) + 1
h, w = frame.shape[:2]
temp = cv.resize(frame, (w // pixel_size, h // pixel_size), interpolation=cv.INTER_LINEAR)
return cv.resize(temp, (w, h), interpolation=cv.INTER_NEAREST)
def apply_emboss_filter(frame, density):
kernel = np.array([[-2, -1, 0], [-1, 1 * density, 1], [0, 1, 2]])
return cv.filter2D(frame, -1, kernel)
def apply_brightness_contrast_adjustment(frame, density):
return adjust_brightness_contrast(frame, density)
def apply_sepia_tone(frame, density):
return apply_sepia_filter(frame, density)
def apply_filter(input_image, filter_type, density):
frame = np.array(input_image)
if filter_type == "Gaussian Blur":
result = apply_gaussian_blur(frame, density)
elif filter_type == "Sharpen":
result = apply_sharpening_filter(frame, density)
elif filter_type == "Edge Detection":
result = apply_edge_detection(frame, density)
elif filter_type == "Invert":
result = apply_invert_filter(frame, density)
elif filter_type == "Brightness":
result = adjust_brightness_contrast(frame, density)
elif filter_type == "GrayScale":
result = apply_grayscale_filter(frame, density)
elif filter_type == "Sepia":
result = apply_sepia_filter(frame, density)
elif filter_type == "Sketch":
result = apply_sketch_filter(frame, density)
elif filter_type == "Cartoon":
result = apply_cartoon_filter(frame, density)
elif filter_type == "Pixelate":
result = apply_pixelate_filter(frame, density)
elif filter_type == "Emboss":
result = apply_emboss_filter(frame, density)
elif filter_type == "Brightness/Contrast":
result = apply_brightness_contrast_adjustment(frame, density)
elif filter_type == "Sepia Tone":
result = apply_sepia_tone(frame, density)
else:
result = frame
return result
with gr.Blocks(css="""
#filter-dropdown {
width: 300px;
margin: 0 auto;
}
#apply-button {
background-color: #8B4513;
color: white;
font-weight: bold;
margin-top: 20px;
}
#apply-button:hover {
background-color: #8B4513;
}
#input-image, #output-image {
width: 100%;
border-radius: 10px;
}
h1 {
text-align: center;
color: #8B4513;
}
p {
text-align: center;
font-size: 20px;
}
""") as demo:
gr.Markdown("""
<h1>🖼️ Image Filter Application 🖼️</h1>
<p>Select a filter and apply it to your image :) Enjoy!</p>
""")
with gr.Row():
with gr.Column():
filter_type = gr.Radio(
label="Choose a filter:",
choices=["Gaussian Blur", "Sharpen", "Edge Detection", "Invert", "Brightness", "GrayScale", "Sepia", "Sketch", "Cartoon", "Pixelate", "Emboss", "Brightness/Contrast", "Sepia Tone"],
value="Gaussian Blur",
elem_id="filter-radio"
)
density_slider = gr.Slider(
minimum=1,
maximum=5,
step=0.1,
label="Filter Intensity (Density)",
value=3,
elem_id="density-slider"
)
input_image = gr.Image(label="Upload Image", elem_id="input-image")
apply_button = gr.Button("Apply Filter", elem_id="apply-button")
with gr.Column():
output_image = gr.Image(label="Filtered Image", elem_id="output-image")
apply_button.click(fn=apply_filter, inputs=[input_image, filter_type, density_slider], outputs=output_image)
demo.launch(share=True)
|