Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,25 +21,25 @@ def resize_image(img, scale):
|
|
| 21 |
|
| 22 |
|
| 23 |
# Canny 邊緣偵測
|
| 24 |
-
def canny_edge1(img):
|
| 25 |
if img is None:
|
| 26 |
return None
|
| 27 |
global imageScale
|
| 28 |
-
img = resize_image(img, imageScale)
|
| 29 |
img_cv = np.array(img)
|
| 30 |
img_gray = cv2.cvtColor(img_cv, cv2.COLOR_RGB2GRAY)
|
| 31 |
-
edges = cv2.Canny(img_gray,
|
| 32 |
|
| 33 |
return Image.fromarray(edges)
|
| 34 |
|
| 35 |
-
def canny_edge2(img):
|
| 36 |
if img is None:
|
| 37 |
return None
|
| 38 |
global imageScale
|
| 39 |
-
img = resize_image(img, imageScale)
|
| 40 |
img_cv = np.array(img)
|
| 41 |
img_gray = cv2.cvtColor(img_cv, cv2.COLOR_RGB2GRAY)
|
| 42 |
-
edges = cv2.Canny(img_gray,
|
| 43 |
edges_colored = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
|
| 44 |
edges_colored[:, :, 1:] = 0
|
| 45 |
combined = cv2.addWeighted(img_cv, 0.8, edges_colored, 0.8, 0)
|
|
@@ -159,6 +159,9 @@ with gr.Blocks() as demo:
|
|
| 159 |
canny_button = gr.Button("Canny 邊緣偵測", elem_id="button1")
|
| 160 |
sobel_button = gr.Button("索伯算子 邊緣偵測", elem_id="button2")
|
| 161 |
laplacian_button = gr.Button("Laplacian 邊緣偵測", elem_id="button3")
|
|
|
|
|
|
|
|
|
|
| 162 |
with gr.Row():
|
| 163 |
img_input2 = gr.Image(type="pil", label="範例圖片1", value=default_image2, interactive=False)
|
| 164 |
img_input2 = gr.Image(type="pil", label="範例圖片2", value=default_image, interactive=False)
|
|
@@ -166,8 +169,8 @@ with gr.Blocks() as demo:
|
|
| 166 |
# 連結功能
|
| 167 |
resize_button.click(fn=resize_image, inputs=[img_input, slider], outputs=img_output2)
|
| 168 |
resize_button.click(fn=resize_image, inputs=[img_input, slider], outputs=img_output1)
|
| 169 |
-
canny_button.click(fn=canny_edge1, inputs=[img_input], outputs=img_output1)
|
| 170 |
-
canny_button.click(fn=canny_edge2, inputs=[img_input], outputs=img_output2)
|
| 171 |
sobel_button.click(fn=sobel_operators1, inputs=[img_input], outputs=img_output1)
|
| 172 |
sobel_button.click(fn=sobel_operators2, inputs=[img_input], outputs=img_output2)
|
| 173 |
laplacian_button.click(fn=Laplacian1, inputs=[img_input], outputs=img_output1)
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
# Canny 邊緣偵測
|
| 24 |
+
def canny_edge1(img,min_val,max_val):
|
| 25 |
if img is None:
|
| 26 |
return None
|
| 27 |
global imageScale
|
| 28 |
+
img = resize_image(img, imageScale)
|
| 29 |
img_cv = np.array(img)
|
| 30 |
img_gray = cv2.cvtColor(img_cv, cv2.COLOR_RGB2GRAY)
|
| 31 |
+
edges = cv2.Canny(img_gray, min_val, max_val)
|
| 32 |
|
| 33 |
return Image.fromarray(edges)
|
| 34 |
|
| 35 |
+
def canny_edge2(img,min_val,max_val):
|
| 36 |
if img is None:
|
| 37 |
return None
|
| 38 |
global imageScale
|
| 39 |
+
img = resize_image(img, imageScale)
|
| 40 |
img_cv = np.array(img)
|
| 41 |
img_gray = cv2.cvtColor(img_cv, cv2.COLOR_RGB2GRAY)
|
| 42 |
+
edges = cv2.Canny(img_gray, min_val, max_val)
|
| 43 |
edges_colored = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
|
| 44 |
edges_colored[:, :, 1:] = 0
|
| 45 |
combined = cv2.addWeighted(img_cv, 0.8, edges_colored, 0.8, 0)
|
|
|
|
| 159 |
canny_button = gr.Button("Canny 邊緣偵測", elem_id="button1")
|
| 160 |
sobel_button = gr.Button("索伯算子 邊緣偵測", elem_id="button2")
|
| 161 |
laplacian_button = gr.Button("Laplacian 邊緣偵測", elem_id="button3")
|
| 162 |
+
with gr.Row():
|
| 163 |
+
slider_canny1 = gr.Slider(1, 150, step=1, value=100, label="canny threshold 1 (min val)")
|
| 164 |
+
slider_canny2 = gr.Slider(150, 300, step=1, value=200, label="canny threshold 2 (max val)")
|
| 165 |
with gr.Row():
|
| 166 |
img_input2 = gr.Image(type="pil", label="範例圖片1", value=default_image2, interactive=False)
|
| 167 |
img_input2 = gr.Image(type="pil", label="範例圖片2", value=default_image, interactive=False)
|
|
|
|
| 169 |
# 連結功能
|
| 170 |
resize_button.click(fn=resize_image, inputs=[img_input, slider], outputs=img_output2)
|
| 171 |
resize_button.click(fn=resize_image, inputs=[img_input, slider], outputs=img_output1)
|
| 172 |
+
canny_button.click(fn=canny_edge1, inputs=[img_input,slider_canny1,slider_canny2], outputs=img_output1)
|
| 173 |
+
canny_button.click(fn=canny_edge2, inputs=[img_input,slider_canny1,slider_canny2], outputs=img_output2)
|
| 174 |
sobel_button.click(fn=sobel_operators1, inputs=[img_input], outputs=img_output1)
|
| 175 |
sobel_button.click(fn=sobel_operators2, inputs=[img_input], outputs=img_output2)
|
| 176 |
laplacian_button.click(fn=Laplacian1, inputs=[img_input], outputs=img_output1)
|