Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,70 +3,67 @@ import numpy as np
|
|
3 |
import cv2
|
4 |
|
5 |
filters = {
|
6 |
-
|
|
|
|
|
|
|
7 |
[0.349, 0.686, 0.168],
|
8 |
-
[0.
|
9 |
-
|
10 |
-
|
11 |
-
[0.393, 0.769, 0.889]]),
|
12 |
-
"Warm Orange": np.array([[1.0, 0.5, 0.0],
|
13 |
-
[0.5, 0.8, 0.2],
|
14 |
-
[0.2, 0.3, 0.5]]),
|
15 |
-
"Vintage": np.array([[0.6, 0.4, 0.2],
|
16 |
-
[0.3, 0.7, 0.3],
|
17 |
[0.2, 0.3, 0.5]]),
|
18 |
-
|
|
|
|
|
|
|
19 |
[0.4, 0.6, 0.2],
|
20 |
[0.2, 0.3, 0.5]]),
|
21 |
-
|
22 |
[0.3, 0.5, 0.7],
|
23 |
[0.4, 0.6, 0.8]]),
|
24 |
-
|
25 |
[0.7, 0.5, 0.2],
|
26 |
[0.5, 0.3, 0.1]]),
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
[0.0, 1.2, 0.0],
|
41 |
[0.0, 0.0, 1.2]]),
|
42 |
-
|
43 |
[0.5, 0.6, 0.5],
|
44 |
[0.4, 0.5, 0.6]]),
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
|
53 |
def apply_filter(image, filter_type):
|
54 |
if image is None:
|
55 |
-
gr.error("Upload an image first!")
|
56 |
return None
|
57 |
|
58 |
if filter_type in filters:
|
59 |
-
|
60 |
-
|
|
|
61 |
elif filter_type == "High Contrast":
|
62 |
return cv2.convertScaleAbs(image, alpha=1.5, beta=0)
|
63 |
-
|
64 |
elif filter_type == "Grayscale":
|
65 |
return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
66 |
-
|
67 |
elif filter_type == "Invert":
|
68 |
return 255 - image
|
69 |
-
|
70 |
elif filter_type == "Vignette":
|
71 |
rows, cols = image.shape[:2]
|
72 |
kernel_x = cv2.getGaussianKernel(cols, cols/4)
|
@@ -77,21 +74,30 @@ def apply_filter(image, filter_type):
|
|
77 |
for i in range(3):
|
78 |
vignette[:,:,i] = vignette[:,:,i] * mask
|
79 |
return vignette.astype(np.uint8)
|
80 |
-
|
81 |
-
elif filter_type not in ["Original", "High Contrast", "Grayscale", "Invert", "Vignette"]:
|
82 |
-
gr.error("Unknown filter...")
|
83 |
-
return None
|
84 |
-
|
85 |
else: # Original
|
86 |
return image
|
87 |
|
88 |
-
def create_sample_image(filter_name):
|
89 |
-
sample = np.ones((50, 50, 3), dtype=np.uint8) * 128
|
90 |
-
filtered = apply_filter(sample, filter_name)
|
91 |
-
return filtered if filtered is not None else sample
|
92 |
-
|
93 |
filter_options = ["Original"] + list(filters.keys()) + ["High Contrast", "Grayscale", "Invert", "Vignette"]
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
with gr.Blocks() as demo:
|
96 |
gr.Markdown("# Color Filter Application")
|
97 |
gr.Markdown("Upload an image and apply various color filters to it.")
|
@@ -102,21 +108,28 @@ with gr.Blocks() as demo:
|
|
102 |
output_image = gr.Image(label="Filtered Image")
|
103 |
|
104 |
with gr.Column(scale=1):
|
|
|
105 |
filter_choice = gr.Radio(
|
106 |
choices=filter_options,
|
107 |
value="Original",
|
108 |
label="Select a filter",
|
109 |
-
|
110 |
-
)
|
111 |
-
gr.Examples(
|
112 |
-
examples=[[create_sample_image(f) for f in filter_options]],
|
113 |
-
inputs=filter_choice,
|
114 |
-
label="Filter Samples",
|
115 |
-
elem_id="filter_samples"
|
116 |
)
|
117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
filter_choice.change(
|
119 |
-
fn=
|
120 |
inputs=[input_image, filter_choice],
|
121 |
outputs=output_image
|
122 |
)
|
|
|
3 |
import cv2
|
4 |
|
5 |
filters = {
|
6 |
+
"Sepia": np.array([[0.393, 0.769, 0.189],
|
7 |
+
[0.349, 0.686, 0.168],
|
8 |
+
[0.272, 0.534, 0.131]]),
|
9 |
+
"Cool Blue": np.array([[0.272, 0.534, 0.131],
|
10 |
[0.349, 0.686, 0.168],
|
11 |
+
[0.393, 0.769, 0.889]]),
|
12 |
+
"Warm Orange": np.array([[1.0, 0.5, 0.0],
|
13 |
+
[0.5, 0.8, 0.2],
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
[0.2, 0.3, 0.5]]),
|
15 |
+
"Vintage": np.array([[0.6, 0.4, 0.2],
|
16 |
+
[0.3, 0.7, 0.3],
|
17 |
+
[0.2, 0.3, 0.5]]),
|
18 |
+
"Vintage Film": np.array([[0.7, 0.5, 0.3],
|
19 |
[0.4, 0.6, 0.2],
|
20 |
[0.2, 0.3, 0.5]]),
|
21 |
+
"Moonlight": np.array([[0.2, 0.4, 0.6],
|
22 |
[0.3, 0.5, 0.7],
|
23 |
[0.4, 0.6, 0.8]]),
|
24 |
+
"Desert Heat": np.array([[0.9, 0.7, 0.3],
|
25 |
[0.7, 0.5, 0.2],
|
26 |
[0.5, 0.3, 0.1]]),
|
27 |
+
"Purple Dream": np.array([[0.5, 0.3, 0.5],
|
28 |
+
[0.2, 0.4, 0.6],
|
29 |
+
[0.4, 0.2, 0.8]]),
|
30 |
+
"Forest Green": np.array([[0.2, 0.6, 0.1],
|
31 |
+
[0.3, 0.7, 0.2],
|
32 |
+
[0.1, 0.5, 0.3]]),
|
33 |
+
"Cyberpunk": np.array([[0.8, 0.3, 0.9],
|
34 |
+
[0.2, 0.7, 0.8],
|
35 |
+
[0.9, 0.2, 0.7]]),
|
36 |
+
"Pastel Dream": np.array([[0.8, 0.7, 0.9],
|
37 |
+
[0.7, 0.9, 0.8],
|
38 |
+
[0.9, 0.8, 0.7]]),
|
39 |
+
"Dramatic Contrast": np.array([[1.2, 0.0, 0.0],
|
40 |
[0.0, 1.2, 0.0],
|
41 |
[0.0, 0.0, 1.2]]),
|
42 |
+
"Faded Polaroid": np.array([[0.7, 0.6, 0.5],
|
43 |
[0.5, 0.6, 0.5],
|
44 |
[0.4, 0.5, 0.6]]),
|
45 |
+
"Neon Nights": np.array([[0.7, 0.2, 0.9],
|
46 |
+
[0.3, 0.8, 0.2],
|
47 |
+
[0.9, 0.3, 0.7]]),
|
48 |
+
"Underwater": np.array([[0.2, 0.4, 0.6],
|
49 |
+
[0.3, 0.5, 0.7],
|
50 |
+
[0.4, 0.6, 0.8]])
|
51 |
+
}
|
52 |
|
53 |
def apply_filter(image, filter_type):
|
54 |
if image is None:
|
|
|
55 |
return None
|
56 |
|
57 |
if filter_type in filters:
|
58 |
+
filtered_img = image.dot(filters[filter_type].T)
|
59 |
+
np.clip(filtered_img, 0, 255, out=filtered_img)
|
60 |
+
return filtered_img.astype(np.uint8)
|
61 |
elif filter_type == "High Contrast":
|
62 |
return cv2.convertScaleAbs(image, alpha=1.5, beta=0)
|
|
|
63 |
elif filter_type == "Grayscale":
|
64 |
return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
|
|
65 |
elif filter_type == "Invert":
|
66 |
return 255 - image
|
|
|
67 |
elif filter_type == "Vignette":
|
68 |
rows, cols = image.shape[:2]
|
69 |
kernel_x = cv2.getGaussianKernel(cols, cols/4)
|
|
|
74 |
for i in range(3):
|
75 |
vignette[:,:,i] = vignette[:,:,i] * mask
|
76 |
return vignette.astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
|
77 |
else: # Original
|
78 |
return image
|
79 |
|
|
|
|
|
|
|
|
|
|
|
80 |
filter_options = ["Original"] + list(filters.keys()) + ["High Contrast", "Grayscale", "Invert", "Vignette"]
|
81 |
|
82 |
+
def update_filter_samples(image):
|
83 |
+
if image is None:
|
84 |
+
return None, gr.Gallery(), gr.Radio(choices=filter_options, value="Original")
|
85 |
+
|
86 |
+
filtered_images = [apply_filter(image, f) for f in filter_options]
|
87 |
+
return (filtered_images[0],
|
88 |
+
gr.Gallery(value=[(img, name) for img, name in zip(filtered_images, filter_options)]),
|
89 |
+
gr.Radio(choices=filter_options, value="Original"))
|
90 |
+
|
91 |
+
def update_output(image, filter_type):
|
92 |
+
if image is None or filter_type is None:
|
93 |
+
return None
|
94 |
+
return apply_filter(image, filter_type)
|
95 |
+
|
96 |
+
def gallery_select(evt: gr.SelectData, filter_choice):
|
97 |
+
if evt is None:
|
98 |
+
return filter_choice
|
99 |
+
return filter_options[evt.index]
|
100 |
+
|
101 |
with gr.Blocks() as demo:
|
102 |
gr.Markdown("# Color Filter Application")
|
103 |
gr.Markdown("Upload an image and apply various color filters to it.")
|
|
|
108 |
output_image = gr.Image(label="Filtered Image")
|
109 |
|
110 |
with gr.Column(scale=1):
|
111 |
+
filter_gallery = gr.Gallery(label="Filter Samples")
|
112 |
filter_choice = gr.Radio(
|
113 |
choices=filter_options,
|
114 |
value="Original",
|
115 |
label="Select a filter",
|
116 |
+
interactive=True
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
)
|
118 |
|
119 |
+
input_image.change(
|
120 |
+
fn=update_filter_samples,
|
121 |
+
inputs=[input_image],
|
122 |
+
outputs=[output_image, filter_gallery, filter_choice]
|
123 |
+
)
|
124 |
+
|
125 |
+
filter_gallery.select(
|
126 |
+
fn=gallery_select,
|
127 |
+
inputs=[filter_choice],
|
128 |
+
outputs=filter_choice
|
129 |
+
)
|
130 |
+
|
131 |
filter_choice.change(
|
132 |
+
fn=update_output,
|
133 |
inputs=[input_image, filter_choice],
|
134 |
outputs=output_image
|
135 |
)
|