Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2 as cv
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
def apply_gaussian_blur(frame, density):
|
6 |
+
ksize = int(density) * 2 + 1
|
7 |
+
return cv.GaussianBlur(frame, (ksize, ksize), 0)
|
8 |
+
|
9 |
+
def apply_sharpening_filter(frame, density):
|
10 |
+
kernel = np.array([[-1, -1, -1], [-1, 8 * density, -1], [-1, -1, -1]])
|
11 |
+
return cv.filter2D(frame, -1, kernel)
|
12 |
+
|
13 |
+
def apply_edge_detection(frame, density):
|
14 |
+
return cv.Canny(frame, 100, 100 * density)
|
15 |
+
|
16 |
+
def apply_invert_filter(frame, density):
|
17 |
+
return cv.bitwise_not(frame)
|
18 |
+
|
19 |
+
def adjust_brightness_contrast(frame, density):
|
20 |
+
alpha = 1.0 + density / 50.0
|
21 |
+
beta = density * 2
|
22 |
+
return cv.convertScaleAbs(frame, alpha=alpha, beta=beta)
|
23 |
+
|
24 |
+
def apply_grayscale_filter(frame, density):
|
25 |
+
return cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
|
26 |
+
|
27 |
+
def apply_sepia_filter(frame, density):
|
28 |
+
sepia_filter = np.array([[0.272 * density, 0.534 * density, 0.131 * density],
|
29 |
+
[0.349 * density, 0.686 * density, 0.168 * density],
|
30 |
+
[0.393 * density, 0.769 * density, 0.189 * density]])
|
31 |
+
return cv.transform(frame, sepia_filter)
|
32 |
+
|
33 |
+
def apply_sketch_filter(frame, density):
|
34 |
+
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
|
35 |
+
inv = cv.bitwise_not(gray)
|
36 |
+
blur = cv.GaussianBlur(inv, (21, 21), 0)
|
37 |
+
sketch = cv.divide(gray, 255 - blur, scale=256)
|
38 |
+
return cv.multiply(sketch, density)
|
39 |
+
|
40 |
+
def apply_cartoon_filter(frame, density):
|
41 |
+
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
|
42 |
+
blur = cv.medianBlur(gray, 7)
|
43 |
+
edges = cv.adaptiveThreshold(blur, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 9, 10)
|
44 |
+
color = cv.bilateralFilter(frame, 9, 300, 300)
|
45 |
+
cartoon = cv.bitwise_and(color, color, mask=edges)
|
46 |
+
return cv.multiply(cartoon, density)
|
47 |
+
|
48 |
+
def apply_pixelate_filter(frame, density):
|
49 |
+
pixel_size = int(density) + 1
|
50 |
+
h, w = frame.shape[:2]
|
51 |
+
temp = cv.resize(frame, (w // pixel_size, h // pixel_size), interpolation=cv.INTER_LINEAR)
|
52 |
+
return cv.resize(temp, (w, h), interpolation=cv.INTER_NEAREST)
|
53 |
+
|
54 |
+
def apply_emboss_filter(frame, density):
|
55 |
+
kernel = np.array([[-2, -1, 0], [-1, 1 * density, 1], [0, 1, 2]])
|
56 |
+
return cv.filter2D(frame, -1, kernel)
|
57 |
+
|
58 |
+
def apply_brightness_contrast_adjustment(frame, density):
|
59 |
+
return adjust_brightness_contrast(frame, density)
|
60 |
+
|
61 |
+
def apply_sepia_tone(frame, density):
|
62 |
+
return apply_sepia_filter(frame, density)
|
63 |
+
|
64 |
+
|
65 |
+
def apply_filter(input_image, filter_type, density):
|
66 |
+
|
67 |
+
frame = np.array(input_image)
|
68 |
+
|
69 |
+
if filter_type == "Gaussian Blur":
|
70 |
+
result = apply_gaussian_blur(frame, density)
|
71 |
+
elif filter_type == "Sharpen":
|
72 |
+
result = apply_sharpening_filter(frame, density)
|
73 |
+
elif filter_type == "Edge Detection":
|
74 |
+
result = apply_edge_detection(frame, density)
|
75 |
+
elif filter_type == "Invert":
|
76 |
+
result = apply_invert_filter(frame, density)
|
77 |
+
elif filter_type == "Brightness":
|
78 |
+
result = adjust_brightness_contrast(frame, density)
|
79 |
+
elif filter_type == "GrayScale":
|
80 |
+
result = apply_grayscale_filter(frame, density)
|
81 |
+
elif filter_type == "Sepia":
|
82 |
+
result = apply_sepia_filter(frame, density)
|
83 |
+
elif filter_type == "Sketch":
|
84 |
+
result = apply_sketch_filter(frame, density)
|
85 |
+
elif filter_type == "Cartoon":
|
86 |
+
result = apply_cartoon_filter(frame, density)
|
87 |
+
elif filter_type == "Pixelate":
|
88 |
+
result = apply_pixelate_filter(frame, density)
|
89 |
+
elif filter_type == "Emboss":
|
90 |
+
result = apply_emboss_filter(frame, density)
|
91 |
+
elif filter_type == "Brightness/Contrast":
|
92 |
+
result = apply_brightness_contrast_adjustment(frame, density)
|
93 |
+
elif filter_type == "Sepia Tone":
|
94 |
+
result = apply_sepia_tone(frame, density)
|
95 |
+
else:
|
96 |
+
result = frame
|
97 |
+
|
98 |
+
return result
|
99 |
+
|
100 |
+
with gr.Blocks(css="""
|
101 |
+
#filter-dropdown {
|
102 |
+
width: 300px;
|
103 |
+
margin: 0 auto;
|
104 |
+
}
|
105 |
+
#apply-button {
|
106 |
+
background-color: #8B4513;
|
107 |
+
color: white;
|
108 |
+
font-weight: bold;
|
109 |
+
margin-top: 20px;
|
110 |
+
}
|
111 |
+
#apply-button:hover {
|
112 |
+
background-color: #8B4513;
|
113 |
+
}
|
114 |
+
#input-image, #output-image {
|
115 |
+
width: 100%;
|
116 |
+
border-radius: 10px;
|
117 |
+
}
|
118 |
+
h1 {
|
119 |
+
text-align: center;
|
120 |
+
color: #8B4513;
|
121 |
+
}
|
122 |
+
p {
|
123 |
+
text-align: center;
|
124 |
+
font-size: 20px;
|
125 |
+
}
|
126 |
+
""") as demo:
|
127 |
+
|
128 |
+
gr.Markdown("""
|
129 |
+
<h1>🖼️ Image Filter Application 🖼️</h1>
|
130 |
+
<p>Select a filter and apply it to your image :) Enjoy!</p>
|
131 |
+
""")
|
132 |
+
|
133 |
+
with gr.Row():
|
134 |
+
with gr.Column():
|
135 |
+
|
136 |
+
filter_type = gr.Radio(
|
137 |
+
label="Choose a filter:",
|
138 |
+
choices=["Gaussian Blur", "Sharpen", "Edge Detection", "Invert", "Brightness", "GrayScale", "Sepia", "Sketch", "Cartoon", "Pixelate", "Emboss", "Brightness/Contrast", "Sepia Tone"],
|
139 |
+
value="Gaussian Blur",
|
140 |
+
elem_id="filter-radio"
|
141 |
+
)
|
142 |
+
|
143 |
+
density_slider = gr.Slider(
|
144 |
+
minimum=1,
|
145 |
+
maximum=5,
|
146 |
+
step=0.1,
|
147 |
+
label="Filter Intensity (Density)",
|
148 |
+
value=3,
|
149 |
+
elem_id="density-slider"
|
150 |
+
)
|
151 |
+
|
152 |
+
input_image = gr.Image(label="Upload Image", elem_id="input-image")
|
153 |
+
|
154 |
+
apply_button = gr.Button("Apply Filter", elem_id="apply-button")
|
155 |
+
|
156 |
+
with gr.Column():
|
157 |
+
output_image = gr.Image(label="Filtered Image", elem_id="output-image")
|
158 |
+
|
159 |
+
apply_button.click(fn=apply_filter, inputs=[input_image, filter_type, density_slider], outputs=output_image)
|
160 |
+
|
161 |
+
demo.launch(share=True)
|