Commit
·
9086be1
1
Parent(s):
67c5687
Add HSV Color Filter tab and functionality to image processing app
Browse files
app.py
CHANGED
@@ -77,7 +77,7 @@ def apply_edge_detection(image, min_val, max_val, operation, kernel_size):
|
|
77 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
78 |
gr.Markdown("# OpenCV Image Processing with Gradio - Add Noise, Remove Noise, Morphological Operations and Edge Detection")
|
79 |
|
80 |
-
tab_names = ["Add Noise", "Remove Noise", "Morphological Operations", "Edge Detection"]
|
81 |
|
82 |
# ---- ADD NOISE TAB ----
|
83 |
with gr.Tab("Add Noise"):
|
@@ -165,6 +165,46 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
165 |
edge_operation.change(fn=on_edge_operation_change, inputs=edge_operation, outputs=[min_val_slider, max_val_slider, kernel_size_slider])
|
166 |
apply_edge_button.click(fn=apply_edge_detection, inputs=[edge_img_input, min_val_slider, max_val_slider, edge_operation, kernel_size_slider], outputs=edge_img_output)
|
167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
gr.Markdown("### To transfer the image to another tab for further processing, select the source and destination tabs and click the Transfer Image button.")
|
169 |
|
170 |
# ---- DYNAMIC TRANSFER BUTTON ----
|
@@ -173,7 +213,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
173 |
destination_tab_dropdown = gr.Dropdown(tab_names, label="Transfer To Tab")
|
174 |
transfer_image_button = gr.Button("Transfer Image")
|
175 |
|
176 |
-
def dynamic_image_transfer(add_noise_input, add_noise_output, denoise_input, denoise_output, morph_input, morph_output, edge_input, edge_output, source, destination):
|
177 |
image_to_send = None
|
178 |
if source == "Add Noise":
|
179 |
image_to_send = add_noise_output if add_noise_output else add_noise_input
|
@@ -183,20 +223,23 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
183 |
image_to_send = morph_output if morph_output else morph_input
|
184 |
elif source == "Edge Detection":
|
185 |
image_to_send = edge_output if edge_output else edge_input
|
|
|
|
|
186 |
|
187 |
updates = {
|
188 |
"Add Noise": gr.update(value=image_to_send) if destination == "Add Noise" else gr.update(),
|
189 |
"Remove Noise": gr.update(value=image_to_send) if destination == "Remove Noise" else gr.update(),
|
190 |
"Morphological Operations": gr.update(value=image_to_send) if destination == "Morphological Operations" else gr.update(),
|
191 |
"Edge Detection": gr.update(value=image_to_send) if destination == "Edge Detection" else gr.update(),
|
|
|
192 |
}
|
193 |
|
194 |
-
return [updates.get("Add Noise"), updates.get("Remove Noise"), updates.get("Morphological Operations"), updates.get("Edge Detection")]
|
195 |
|
196 |
transfer_image_button.click(
|
197 |
fn=dynamic_image_transfer,
|
198 |
-
inputs=[img_input, img_output, denoise_img_input, denoise_img_output, morph_img_input, morph_img_output, edge_img_input, edge_img_output, source_tab_dropdown, destination_tab_dropdown],
|
199 |
-
outputs=[img_input, denoise_img_input, morph_img_input, edge_img_input]
|
200 |
)
|
201 |
|
202 |
gr.Markdown("""
|
|
|
77 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
78 |
gr.Markdown("# OpenCV Image Processing with Gradio - Add Noise, Remove Noise, Morphological Operations and Edge Detection")
|
79 |
|
80 |
+
tab_names = ["Add Noise", "Remove Noise", "Morphological Operations", "Edge Detection", "HSV Color Filter"]
|
81 |
|
82 |
# ---- ADD NOISE TAB ----
|
83 |
with gr.Tab("Add Noise"):
|
|
|
165 |
edge_operation.change(fn=on_edge_operation_change, inputs=edge_operation, outputs=[min_val_slider, max_val_slider, kernel_size_slider])
|
166 |
apply_edge_button.click(fn=apply_edge_detection, inputs=[edge_img_input, min_val_slider, max_val_slider, edge_operation, kernel_size_slider], outputs=edge_img_output)
|
167 |
|
168 |
+
# ---- HSV COLOR FILTER TAB ----
|
169 |
+
with gr.Tab("HSV Color Filter"):
|
170 |
+
with gr.Row():
|
171 |
+
hsv_img_input = gr.Image(label="Input Image", type="pil")
|
172 |
+
hsv_img_output = gr.Image(label="Filtered Output", type="pil")
|
173 |
+
with gr.Row():
|
174 |
+
with gr.Column():
|
175 |
+
h_min = gr.Slider(0, 179, value=0, label="Hue Min")
|
176 |
+
s_min = gr.Slider(0, 255, value=0, label="Saturation Min")
|
177 |
+
v_min = gr.Slider(0, 255, value=0, label="Value Min")
|
178 |
+
with gr.Column():
|
179 |
+
h_max = gr.Slider(0, 179, value=179, label="Hue Max")
|
180 |
+
s_max = gr.Slider(0, 255, value=255, label="Saturation Max")
|
181 |
+
v_max = gr.Slider(0, 255, value=255, label="Value Max")
|
182 |
+
show_mask = gr.Checkbox(label="Show Binary Mask Only", value=False)
|
183 |
+
apply_hsv_button = gr.Button("Apply HSV Filter")
|
184 |
+
|
185 |
+
def apply_hsv_filter(image, hmin, hmax, smin, smax, vmin, vmax, show_mask_flag):
|
186 |
+
if image is None:
|
187 |
+
return None
|
188 |
+
img = np.array(image.convert('RGB'))
|
189 |
+
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
|
190 |
+
# Ensure min <= max for each channel
|
191 |
+
if hmin > hmax: hmin, hmax = hmax, hmin
|
192 |
+
if smin > smax: smin, smax = smax, smin
|
193 |
+
if vmin > vmax: vmin, vmax = vmax, vmin
|
194 |
+
lower = np.array([hmin, smin, vmin], dtype=np.uint8)
|
195 |
+
upper = np.array([hmax, smax, vmax], dtype=np.uint8)
|
196 |
+
mask = cv2.inRange(hsv, lower, upper)
|
197 |
+
if show_mask_flag:
|
198 |
+
return Image.fromarray(mask)
|
199 |
+
result = cv2.bitwise_and(img, img, mask=mask)
|
200 |
+
return Image.fromarray(result)
|
201 |
+
|
202 |
+
apply_hsv_button.click(
|
203 |
+
fn=apply_hsv_filter,
|
204 |
+
inputs=[hsv_img_input, h_min, h_max, s_min, s_max, v_min, v_max, show_mask],
|
205 |
+
outputs=hsv_img_output
|
206 |
+
)
|
207 |
+
|
208 |
gr.Markdown("### To transfer the image to another tab for further processing, select the source and destination tabs and click the Transfer Image button.")
|
209 |
|
210 |
# ---- DYNAMIC TRANSFER BUTTON ----
|
|
|
213 |
destination_tab_dropdown = gr.Dropdown(tab_names, label="Transfer To Tab")
|
214 |
transfer_image_button = gr.Button("Transfer Image")
|
215 |
|
216 |
+
def dynamic_image_transfer(add_noise_input, add_noise_output, denoise_input, denoise_output, morph_input, morph_output, edge_input, edge_output, hsv_input, hsv_output, source, destination):
|
217 |
image_to_send = None
|
218 |
if source == "Add Noise":
|
219 |
image_to_send = add_noise_output if add_noise_output else add_noise_input
|
|
|
223 |
image_to_send = morph_output if morph_output else morph_input
|
224 |
elif source == "Edge Detection":
|
225 |
image_to_send = edge_output if edge_output else edge_input
|
226 |
+
elif source == "HSV Color Filter":
|
227 |
+
image_to_send = hsv_output if hsv_output else hsv_input
|
228 |
|
229 |
updates = {
|
230 |
"Add Noise": gr.update(value=image_to_send) if destination == "Add Noise" else gr.update(),
|
231 |
"Remove Noise": gr.update(value=image_to_send) if destination == "Remove Noise" else gr.update(),
|
232 |
"Morphological Operations": gr.update(value=image_to_send) if destination == "Morphological Operations" else gr.update(),
|
233 |
"Edge Detection": gr.update(value=image_to_send) if destination == "Edge Detection" else gr.update(),
|
234 |
+
"HSV Color Filter": gr.update(value=image_to_send) if destination == "HSV Color Filter" else gr.update(),
|
235 |
}
|
236 |
|
237 |
+
return [updates.get("Add Noise"), updates.get("Remove Noise"), updates.get("Morphological Operations"), updates.get("Edge Detection"), updates.get("HSV Color Filter")]
|
238 |
|
239 |
transfer_image_button.click(
|
240 |
fn=dynamic_image_transfer,
|
241 |
+
inputs=[img_input, img_output, denoise_img_input, denoise_img_output, morph_img_input, morph_img_output, edge_img_input, edge_img_output, hsv_img_input, hsv_img_output, source_tab_dropdown, destination_tab_dropdown],
|
242 |
+
outputs=[img_input, denoise_img_input, morph_img_input, edge_img_input, hsv_img_input]
|
243 |
)
|
244 |
|
245 |
gr.Markdown("""
|