shriarul5273
commited on
Commit
·
0eab673
1
Parent(s):
cd95635
initial commit
Browse files- app.py +203 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
from skimage.util import random_noise
|
5 |
+
import cv2
|
6 |
+
|
7 |
+
# Assuming ImageSlider is a custom or extended component of Gradio
|
8 |
+
from gradio_imageslider import ImageSlider
|
9 |
+
|
10 |
+
# Function to add noise to the image
|
11 |
+
def add_noise(image, noise_type, mean=0, var=0.01, amount=0.05, salt_vs_pepper=0.5):
|
12 |
+
# Convert image to float for processing
|
13 |
+
image = np.array(image).astype(float) / 255.0 # Normalize the image
|
14 |
+
kwargs = {}
|
15 |
+
|
16 |
+
# Set noise parameters based on the selected noise type
|
17 |
+
if noise_type in ['gaussian', 'speckle']:
|
18 |
+
kwargs['mean'] = mean
|
19 |
+
kwargs['var'] = var
|
20 |
+
elif noise_type in ['salt', 'pepper', 's&p']:
|
21 |
+
kwargs['amount'] = amount
|
22 |
+
if noise_type == 's&p':
|
23 |
+
kwargs['salt_vs_pepper'] = salt_vs_pepper
|
24 |
+
elif noise_type == 'localvar':
|
25 |
+
kwargs['local_vars'] = np.full(image.shape, var)
|
26 |
+
|
27 |
+
# Add noise to the image
|
28 |
+
noisy_image = random_noise(image, mode=noise_type.replace("s&p", "salt&pepper"), **kwargs, clip=True)
|
29 |
+
return Image.fromarray((noisy_image * 255).astype(np.uint8))
|
30 |
+
|
31 |
+
# Function to apply denoising to the image
|
32 |
+
def apply_denoising(image, method, gaussian_kernel, median_kernel, bilateral_diameter, bilateral_sigma_color, bilateral_sigma_space, nlm_h, nlm_template_window_size, nlm_search_window_size):
|
33 |
+
# Convert image to array for processing
|
34 |
+
image = np.array(image)
|
35 |
+
# Apply the selected denoising method
|
36 |
+
if method == "Gaussian Blur":
|
37 |
+
denoised = cv2.GaussianBlur(image, (gaussian_kernel, gaussian_kernel), 0)
|
38 |
+
elif method == "Median Blur":
|
39 |
+
denoised = cv2.medianBlur(image, median_kernel)
|
40 |
+
elif method == "Bilateral Filter":
|
41 |
+
denoised = cv2.bilateralFilter(image, bilateral_diameter, bilateral_sigma_color, bilateral_sigma_space)
|
42 |
+
elif method == "Non-Local Means":
|
43 |
+
denoised = cv2.fastNlMeansDenoisingColored(image, None, nlm_h, nlm_h, nlm_template_window_size, nlm_search_window_size)
|
44 |
+
return Image.fromarray(denoised)
|
45 |
+
|
46 |
+
# Function to apply morphological operations
|
47 |
+
def apply_morphological_operation(image, kernel_size, iterations, operation):
|
48 |
+
image = np.array(image)
|
49 |
+
kernel = np.ones((kernel_size, kernel_size), np.uint8)
|
50 |
+
if operation == "Erosion":
|
51 |
+
result = cv2.erode(image, kernel, iterations=iterations)
|
52 |
+
elif operation == "Dilation":
|
53 |
+
result = cv2.dilate(image, kernel, iterations=iterations)
|
54 |
+
elif operation == "Opening":
|
55 |
+
result = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel, iterations=iterations)
|
56 |
+
elif operation == "Closing":
|
57 |
+
result = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel, iterations=iterations)
|
58 |
+
return Image.fromarray(result)
|
59 |
+
|
60 |
+
# Function to apply edge detection
|
61 |
+
def apply_edge_detection(image, min_val, max_val, operation, kernel_size):
|
62 |
+
image = np.array(image.convert('L'))
|
63 |
+
if operation == "Canny":
|
64 |
+
edges = cv2.Canny(image, min_val, max_val)
|
65 |
+
elif operation == "Sobel-X":
|
66 |
+
edges = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=kernel_size)
|
67 |
+
elif operation == "Sobel-Y":
|
68 |
+
edges = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=kernel_size)
|
69 |
+
elif operation == "Sobel-XY":
|
70 |
+
edges_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=kernel_size)
|
71 |
+
edges_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=kernel_size)
|
72 |
+
edges = cv2.addWeighted(edges_x, 0.5, edges_y, 0.5, 0)
|
73 |
+
elif operation == "Laplacian":
|
74 |
+
edges = cv2.Laplacian(image, cv2.CV_64F, ksize=kernel_size)
|
75 |
+
edges = np.clip(edges, 0, 255).astype(np.uint8)
|
76 |
+
return Image.fromarray(edges)
|
77 |
+
|
78 |
+
# Gradio interface setup
|
79 |
+
with gr.Blocks() as demo:
|
80 |
+
gr.Markdown("# OpenCV Image Processing with Gradio - Add Noise, Remove Noise, Morphological Operations and Edge Detection")
|
81 |
+
|
82 |
+
tab_names = ["Add Noise", "Remove Noise", "Morphological Operations", "Edge Detection"]
|
83 |
+
|
84 |
+
# ---- ADD NOISE TAB ----
|
85 |
+
with gr.Tab("Add Noise"):
|
86 |
+
with gr.Row():
|
87 |
+
img_input = gr.Image(label="Input Image", type="pil")
|
88 |
+
img_output = gr.Image(label="Output Image", type="pil")
|
89 |
+
noise_type = gr.Radio(["gaussian", "localvar", "poisson", "salt", "pepper", "s&p", "speckle"], label="Type of Noise", value="gaussian")
|
90 |
+
mean_slider = gr.Slider(0, 1, value=0, label="Mean (for Gaussian/Speckle)", visible=True)
|
91 |
+
var_slider = gr.Slider(0, 0.1, value=0.01, label="Variance", visible=True)
|
92 |
+
amount_slider = gr.Slider(0, 1, value=0.05, label="Amount (for Salt/Pepper/S&P)", visible=False)
|
93 |
+
salt_vs_pepper_slider = gr.Slider(0, 1, value=0.5, label="Salt vs Pepper (for S&P)", visible=False)
|
94 |
+
|
95 |
+
noise_button = gr.Button("Add Noise")
|
96 |
+
|
97 |
+
def on_noise_type_change(noise_type):
|
98 |
+
if noise_type in ['gaussian', 'speckle']:
|
99 |
+
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
100 |
+
elif noise_type in ['salt', 'pepper']:
|
101 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
|
102 |
+
elif noise_type == 's&p':
|
103 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
|
104 |
+
elif noise_type == 'localvar':
|
105 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
106 |
+
|
107 |
+
noise_type.change(fn=on_noise_type_change, inputs=noise_type, outputs=[mean_slider, var_slider, amount_slider, salt_vs_pepper_slider])
|
108 |
+
noise_button.click(fn=add_noise, inputs=[img_input, noise_type, mean_slider, var_slider, amount_slider, salt_vs_pepper_slider], outputs=img_output)
|
109 |
+
|
110 |
+
# ---- REMOVE NOISE TAB ----
|
111 |
+
with gr.Tab("Remove Noise"):
|
112 |
+
with gr.Row():
|
113 |
+
denoise_img_input = gr.Image(label="Input Noisy Image", type="pil")
|
114 |
+
denoise_img_output = gr.Image(label="Output Image", type="pil")
|
115 |
+
denoise_method = gr.Radio(["Gaussian Blur", "Median Blur", "Bilateral Filter", "Non-Local Means"], label="Denoising Method", value="Gaussian Blur")
|
116 |
+
gaussian_kernel = gr.Slider(1, 31, step=2, value=5, label="Gaussian Kernel Size", visible=True)
|
117 |
+
median_kernel = gr.Slider(1, 31, step=2, value=5, label="Median Kernel Size", visible=False)
|
118 |
+
bilateral_diameter = gr.Slider(1, 31, step=2, value=9, label="Bilateral Filter Diameter", visible=False)
|
119 |
+
bilateral_sigma_color = gr.Slider(1, 150, value=75, label="Bilateral Filter Sigma Color", visible=False)
|
120 |
+
bilateral_sigma_space = gr.Slider(1, 150, value=75, label="Bilateral Filter Sigma Space", visible=False)
|
121 |
+
nlm_h = gr.Slider(1, 20, value=10, label="Non-Local Means h", visible=False)
|
122 |
+
nlm_template_window_size = gr.Slider(1, 21, step=2, value=7, label="Non-Local Means Template Window Size", visible=False)
|
123 |
+
nlm_search_window_size = gr.Slider(1, 51, step=2, value=21, label="Non-Local Means Search Window Size", visible=False)
|
124 |
+
denoise_button = gr.Button("Remove Noise")
|
125 |
+
|
126 |
+
def on_denoise_method_change(method):
|
127 |
+
if method == "Gaussian Blur":
|
128 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
129 |
+
elif method == "Median Blur":
|
130 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
131 |
+
elif method == "Bilateral Filter":
|
132 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
133 |
+
elif method == "Non-Local Means":
|
134 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
135 |
+
|
136 |
+
denoise_method.change(fn=on_denoise_method_change, inputs=denoise_method, outputs=[gaussian_kernel, median_kernel, bilateral_diameter, bilateral_sigma_color, bilateral_sigma_space, nlm_h, nlm_template_window_size, nlm_search_window_size])
|
137 |
+
denoise_button.click(fn=apply_denoising, inputs=[denoise_img_input, denoise_method, gaussian_kernel, median_kernel, bilateral_diameter, bilateral_sigma_color, bilateral_sigma_space, nlm_h, nlm_template_window_size, nlm_search_window_size], outputs=denoise_img_output)
|
138 |
+
|
139 |
+
# ---- MORPHOLOGICAL OPERATIONS TAB ----
|
140 |
+
with gr.Tab("Morphological Operations"):
|
141 |
+
with gr.Row():
|
142 |
+
morph_img_input = gr.Image(label="Input Image", type="pil")
|
143 |
+
morph_img_output = gr.Image(label="Output Image", type="pil")
|
144 |
+
kernel_slider = gr.Slider(1, 11, value=3, step=2, label="Kernel Size")
|
145 |
+
iter_slider = gr.Slider(1, 10, value=1, step=1, label="Iterations")
|
146 |
+
morph_operation = gr.Radio(["Erosion", "Dilation", "Opening", "Closing"], label="Morphological Operation", value="Erosion")
|
147 |
+
apply_morph_button = gr.Button("Apply Morphological Operation")
|
148 |
+
apply_morph_button.click(fn=apply_morphological_operation, inputs=[morph_img_input, kernel_slider, iter_slider, morph_operation], outputs=morph_img_output)
|
149 |
+
|
150 |
+
# ---- EDGE DETECTION TAB ----
|
151 |
+
with gr.Tab("Edge Detection"):
|
152 |
+
with gr.Row():
|
153 |
+
edge_img_input = gr.Image(label="Input Image", type="pil")
|
154 |
+
edge_img_output = gr.Image(label="Output Image", type="pil")
|
155 |
+
min_val_slider = gr.Slider(50, 150, label="Min Threshold", visible=True)
|
156 |
+
max_val_slider = gr.Slider(100, 200, label="Max Threshold", visible=True)
|
157 |
+
kernel_size_slider = gr.Slider(1, 11, value=3, step=2, label="Kernel Size", visible=True)
|
158 |
+
edge_operation = gr.Radio(["Canny", "Sobel-X", "Sobel-Y", "Sobel-XY", "Laplacian"], label="Edge Operation", value="Canny")
|
159 |
+
apply_edge_button = gr.Button("Apply Edge Detection")
|
160 |
+
|
161 |
+
def on_edge_operation_change(operation):
|
162 |
+
if operation == "Canny":
|
163 |
+
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
|
164 |
+
else:
|
165 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
166 |
+
|
167 |
+
edge_operation.change(fn=on_edge_operation_change, inputs=edge_operation, outputs=[min_val_slider, max_val_slider, kernel_size_slider])
|
168 |
+
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)
|
169 |
+
|
170 |
+
# ---- DYNAMIC TRANSFER BUTTON ----
|
171 |
+
with gr.Row():
|
172 |
+
source_tab_dropdown = gr.Dropdown(tab_names, label="Transfer From Tab")
|
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
|
180 |
+
elif source == "Remove Noise":
|
181 |
+
image_to_send = denoise_output if denoise_output else denoise_input
|
182 |
+
elif source == "Morphological Operations":
|
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 |
+
# Launch the Gradio interface
|
203 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
opencv-python
|
2 |
+
numpy
|
3 |
+
PIL
|
4 |
+
scikit-image
|
5 |
+
gradio
|