Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,67 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 7 |
edges = cv2.Canny(gray, threshold1, threshold2)
|
| 8 |
return edges
|
| 9 |
|
| 10 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 12 |
_, segmented = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 13 |
return segmented
|
| 14 |
|
| 15 |
-
def image_inpainting(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
if mask.shape[:2] != image.shape[:2]:
|
| 17 |
mask = cv2.resize(mask, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 18 |
|
|
@@ -22,54 +71,147 @@ def image_inpainting(image, mask):
|
|
| 22 |
inpainted = cv2.inpaint(image, mask, 3, cv2.INPAINT_TELEA)
|
| 23 |
return inpainted
|
| 24 |
|
| 25 |
-
exmaples = [
|
| 26 |
-
'176788.jpg'
|
| 27 |
-
'182862.jpg'
|
| 28 |
-
]
|
| 29 |
-
masks = [
|
| 30 |
-
'mask1.jpg'
|
| 31 |
-
'mask2.jpg'
|
| 32 |
-
]
|
| 33 |
-
|
| 34 |
with gr.Blocks() as demo:
|
| 35 |
-
gr.Markdown("Computer Vision")
|
| 36 |
with gr.Tab("Edge Detection"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
inputs = [
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
gr.Interface(
|
| 44 |
fn=edge_detection,
|
| 45 |
inputs=inputs,
|
| 46 |
outputs=output,
|
| 47 |
-
description="Upload an image and adjust the thresholds to perform edge detection"
|
|
|
|
| 48 |
)
|
| 49 |
with gr.Tab("Image Segmentation"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
inputs = [
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
gr.Interface(
|
| 55 |
fn=image_segmentation,
|
| 56 |
inputs=inputs,
|
| 57 |
outputs=output,
|
| 58 |
-
description="Upload an image to perform
|
|
|
|
| 59 |
)
|
| 60 |
with gr.Tab("Image Inpainting"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
inputs = [
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
gr.Interface(
|
| 67 |
fn=image_inpainting,
|
| 68 |
inputs=inputs,
|
| 69 |
outputs=output,
|
| 70 |
-
description="Upload an image and adjust the thresholds to perform
|
| 71 |
flagging_mode='never'
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
+
examples = [
|
| 7 |
+
'/home/teng_aicv/Desktop/176788.jpg',
|
| 8 |
+
'/home/teng_aicv/Desktop/182862.jpg'
|
| 9 |
+
]
|
| 10 |
+
masks = [
|
| 11 |
+
'/home/teng_aicv/Desktop/mask1.jpg',
|
| 12 |
+
'/home/teng_aicv/Desktop/mask2.jpg'
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
def edge_detection(mode_selector, upload_input, image_selector, image_selected, threshold1, threshold2):
|
| 16 |
+
if mode_selector == "Upload":
|
| 17 |
+
image = upload_input
|
| 18 |
+
else:
|
| 19 |
+
image = image_selected
|
| 20 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 21 |
edges = cv2.Canny(gray, threshold1, threshold2)
|
| 22 |
return edges
|
| 23 |
|
| 24 |
+
def mode_selector_changed(mode_selector, image_selector):
|
| 25 |
+
if mode_selector == "Upload":
|
| 26 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
| 27 |
+
else:
|
| 28 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=image_selector is not None, value=image_selector)
|
| 29 |
+
|
| 30 |
+
def mode_selector_changed_inpainting(mode_selector, image_selector, mask_selector):
|
| 31 |
+
if mode_selector == "Upload":
|
| 32 |
+
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 33 |
+
else:
|
| 34 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=image_selector is not None, value=image_selector), gr.update(visible=True), gr.update(visible=image_selector is not None, value=mask_selector)
|
| 35 |
+
|
| 36 |
+
def close_selected_image(image_selected):
|
| 37 |
+
if image_selected is None:
|
| 38 |
+
return gr.update(visible=True, value=None), gr.update(visible=False, value=None)
|
| 39 |
+
else:
|
| 40 |
+
return gr.update(visible=True), gr.update(visible=True)
|
| 41 |
+
|
| 42 |
+
def close_selected_mask(mask_selected):
|
| 43 |
+
if mask_selected is None:
|
| 44 |
+
return gr.update(visible=True, value=None), gr.update(visible=False, value=None)
|
| 45 |
+
else:
|
| 46 |
+
return gr.update(visible=True), gr.update(visible=True)
|
| 47 |
+
|
| 48 |
+
def image_segmentation(mode_selector, upload_input, image_selector, image_selected):
|
| 49 |
+
if mode_selector == "Upload":
|
| 50 |
+
image = upload_input
|
| 51 |
+
else:
|
| 52 |
+
image = image_selected
|
| 53 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 54 |
_, segmented = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 55 |
return segmented
|
| 56 |
|
| 57 |
+
def image_inpainting(mode_selector, upload_input, upload_mask, image_selector, image_selected, mask_selector, mask_selected):
|
| 58 |
+
if mode_selector == "Upload":
|
| 59 |
+
image = upload_input
|
| 60 |
+
mask = upload_mask
|
| 61 |
+
else:
|
| 62 |
+
image = image_selected
|
| 63 |
+
mask = mask_selected
|
| 64 |
+
|
| 65 |
if mask.shape[:2] != image.shape[:2]:
|
| 66 |
mask = cv2.resize(mask, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 67 |
|
|
|
|
| 71 |
inpainted = cv2.inpaint(image, mask, 3, cv2.INPAINT_TELEA)
|
| 72 |
return inpainted
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
with gr.Blocks() as demo:
|
|
|
|
| 75 |
with gr.Tab("Edge Detection"):
|
| 76 |
+
mode_selector = gr.Radio(["Upload", "Example"], value="Upload", label="Upload an image or using example image")
|
| 77 |
+
|
| 78 |
+
# Mode 1
|
| 79 |
+
upload_input = gr.Image(type="numpy", label="Upload Image", visible=True)
|
| 80 |
+
|
| 81 |
+
# Mode 2
|
| 82 |
+
image_selector = gr.Radio(examples, label="Select an example image", visible=False)
|
| 83 |
+
image_selected = gr.Image(None, label="Selected Image", visible=False, sources=[])
|
| 84 |
+
|
| 85 |
+
threshold1 = gr.Slider(0, 255, value=50, step=1, label="Threshold 1")
|
| 86 |
+
threshold2 = gr.Slider(0, 255, value=100, step=1, label="Threshold 2")
|
| 87 |
+
|
| 88 |
+
output = gr.Image(type="numpy", label="Edge Image")
|
| 89 |
inputs = [
|
| 90 |
+
mode_selector,
|
| 91 |
+
upload_input,
|
| 92 |
+
image_selector,
|
| 93 |
+
image_selected,
|
| 94 |
+
threshold1,
|
| 95 |
+
threshold2
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
+
mode_selector.change(
|
| 99 |
+
mode_selector_changed,
|
| 100 |
+
inputs=[mode_selector, image_selector],
|
| 101 |
+
outputs=[upload_input, image_selector, image_selected]
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
image_selector.change(
|
| 105 |
+
mode_selector_changed,
|
| 106 |
+
inputs=[mode_selector, image_selector],
|
| 107 |
+
outputs=[upload_input, image_selector, image_selected]
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
image_selected.change(
|
| 111 |
+
close_selected_image,
|
| 112 |
+
inputs=[image_selected],
|
| 113 |
+
outputs=[image_selector, image_selected]
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
gr.Interface(
|
| 117 |
fn=edge_detection,
|
| 118 |
inputs=inputs,
|
| 119 |
outputs=output,
|
| 120 |
+
description="Upload an image and adjust the thresholds to perform edge detection",
|
| 121 |
+
flagging_mode='never'
|
| 122 |
)
|
| 123 |
with gr.Tab("Image Segmentation"):
|
| 124 |
+
mode_selector = gr.Radio(["Upload", "Example"], value="Upload", label="Upload an image or using example image")
|
| 125 |
+
|
| 126 |
+
# Mode 1
|
| 127 |
+
upload_input = gr.Image(type="numpy", label="Upload Image", visible=True)
|
| 128 |
+
# Mode 2
|
| 129 |
+
image_selector = gr.Radio(examples, label="Select an example image", visible=False)
|
| 130 |
+
image_selected = gr.Image(None, label="Selected Image", visible=False, sources=[])
|
| 131 |
+
|
| 132 |
+
output = gr.Image(type="numpy", label="Edge Image")
|
| 133 |
inputs = [
|
| 134 |
+
mode_selector,
|
| 135 |
+
upload_input,
|
| 136 |
+
image_selector,
|
| 137 |
+
image_selected,
|
| 138 |
+
]
|
| 139 |
+
mode_selector.change(
|
| 140 |
+
mode_selector_changed,
|
| 141 |
+
inputs=[mode_selector, image_selector],
|
| 142 |
+
outputs=[upload_input, image_selector, image_selected]
|
| 143 |
+
)
|
| 144 |
+
image_selector.change(
|
| 145 |
+
mode_selector_changed,
|
| 146 |
+
inputs=[mode_selector, image_selector],
|
| 147 |
+
outputs=[upload_input, image_selector, image_selected]
|
| 148 |
+
)
|
| 149 |
+
image_selected.change(
|
| 150 |
+
close_selected_image,
|
| 151 |
+
inputs=[image_selected],
|
| 152 |
+
outputs=[image_selector, image_selected]
|
| 153 |
+
)
|
| 154 |
gr.Interface(
|
| 155 |
fn=image_segmentation,
|
| 156 |
inputs=inputs,
|
| 157 |
outputs=output,
|
| 158 |
+
description="Upload an image and adjust the thresholds to perform edge detection",
|
| 159 |
+
flagging_mode='never'
|
| 160 |
)
|
| 161 |
with gr.Tab("Image Inpainting"):
|
| 162 |
+
mode_selector = gr.Radio(["Upload", "Example"], value="Upload", label="Upload an image or using example image")
|
| 163 |
+
|
| 164 |
+
# Mode 1
|
| 165 |
+
upload_input = gr.Image(type="numpy", label="Upload Image", visible=True)
|
| 166 |
+
upload_mask = gr.Image(type="numpy", label="Upload Mask", visible=True)
|
| 167 |
+
# Mode 2
|
| 168 |
+
image_selector = gr.Radio(examples, label="Select an example image", visible=False)
|
| 169 |
+
image_selected = gr.Image(None, label="Selected Image", visible=False, sources=[])
|
| 170 |
+
mask_selector = gr.Radio(masks, label="Select an example mask", visible=False)
|
| 171 |
+
mask_selected = gr.Image(None, label="Selected Mask", visible=False, sources=[])
|
| 172 |
+
|
| 173 |
+
output = gr.Image(type="numpy", label="Edge Image")
|
| 174 |
inputs = [
|
| 175 |
+
mode_selector,
|
| 176 |
+
upload_input,
|
| 177 |
+
upload_mask,
|
| 178 |
+
image_selector,
|
| 179 |
+
image_selected,
|
| 180 |
+
mask_selector,
|
| 181 |
+
mask_selected
|
| 182 |
+
]
|
| 183 |
+
mode_selector.change(
|
| 184 |
+
mode_selector_changed_inpainting,
|
| 185 |
+
inputs=[mode_selector, image_selector, mask_selector],
|
| 186 |
+
outputs=[upload_input, upload_mask, image_selector, image_selected, mask_selector, mask_selected]
|
| 187 |
+
)
|
| 188 |
+
image_selector.change(
|
| 189 |
+
mode_selector_changed,
|
| 190 |
+
inputs=[mode_selector, image_selector],
|
| 191 |
+
outputs=[upload_input, image_selector, image_selected]
|
| 192 |
+
)
|
| 193 |
+
mask_selector.change(
|
| 194 |
+
mode_selector_changed,
|
| 195 |
+
inputs=[mode_selector, mask_selector],
|
| 196 |
+
outputs=[upload_mask, mask_selector, mask_selected]
|
| 197 |
+
)
|
| 198 |
+
image_selected.change(
|
| 199 |
+
close_selected_image,
|
| 200 |
+
inputs=[image_selected],
|
| 201 |
+
outputs=[image_selector, image_selected]
|
| 202 |
+
)
|
| 203 |
+
mask_selected.change(
|
| 204 |
+
close_selected_mask,
|
| 205 |
+
inputs=[mask_selected],
|
| 206 |
+
outputs=[mask_selector, mask_selected]
|
| 207 |
+
)
|
| 208 |
gr.Interface(
|
| 209 |
fn=image_inpainting,
|
| 210 |
inputs=inputs,
|
| 211 |
outputs=output,
|
| 212 |
+
description="Upload an image and adjust the thresholds to perform edge detection",
|
| 213 |
flagging_mode='never'
|
| 214 |
)
|
| 215 |
|
| 216 |
if __name__ == "__main__":
|
| 217 |
+
demo.launch(allowed_paths=["/home/teng_aicv/Desktop/"])
|