Spaces:
Sleeping
Sleeping
Commit
·
d0fe5a6
0
Parent(s):
Init project
Browse files
coin.png
ADDED
|
flower.png
ADDED
|
lenna.png
ADDED
|
main.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from skimage import filters, color
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
|
| 6 |
+
# 邊緣檢測函數
|
| 7 |
+
def edge_detection(image, threshold1, threshold2, method):
|
| 8 |
+
if method == "Canny (OpenCV)":
|
| 9 |
+
edges = cv2.Canny(image, threshold1, threshold2)
|
| 10 |
+
elif method == "Sobel (skimage)":
|
| 11 |
+
gray = color.rgb2gray(image) if len(image.shape) == 3 else image
|
| 12 |
+
edges = filters.sobel(gray)
|
| 13 |
+
edges = (edges * 255).astype(np.uint8) # 轉換為 0-255 範圍
|
| 14 |
+
else:
|
| 15 |
+
raise ValueError("Unknown method selected.")
|
| 16 |
+
return edges
|
| 17 |
+
|
| 18 |
+
# 動態更新閾值參數的顯示狀態
|
| 19 |
+
def update_threshold_visibility(method):
|
| 20 |
+
if method == "Canny (OpenCV)":
|
| 21 |
+
return gr.update(visible=True), gr.update(visible=True)
|
| 22 |
+
else:
|
| 23 |
+
return gr.update(visible=False), gr.update(visible=False)
|
| 24 |
+
|
| 25 |
+
# 設定 Gradio UI
|
| 26 |
+
def main():
|
| 27 |
+
with gr.Blocks() as demo:
|
| 28 |
+
gr.Markdown("# Edge Detection App")
|
| 29 |
+
gr.Markdown("### Upload an image or select an example to detect edges.")
|
| 30 |
+
|
| 31 |
+
with gr.Row():
|
| 32 |
+
with gr.Column():
|
| 33 |
+
input_image = gr.Image(label="Input Image", type="numpy")
|
| 34 |
+
method = gr.Radio(
|
| 35 |
+
["Canny (OpenCV)", "Sobel (skimage)"],
|
| 36 |
+
value="Canny (OpenCV)",
|
| 37 |
+
label="Edge Detection Method"
|
| 38 |
+
)
|
| 39 |
+
low_threshold = gr.Slider(0, 255, value=100, step=1, label="Low Threshold", visible=True)
|
| 40 |
+
high_threshold = gr.Slider(0, 255, value=200, step=1, label="High Threshold", visible=True)
|
| 41 |
+
with gr.Column():
|
| 42 |
+
output_image = gr.Image(label="Output Image")
|
| 43 |
+
|
| 44 |
+
# 示例圖片與參數
|
| 45 |
+
examples = gr.Examples(
|
| 46 |
+
examples=[
|
| 47 |
+
["lenna.png", "Canny (OpenCV)", 100, 200],
|
| 48 |
+
["coin.png", "Sobel (skimage)", 0, 0],
|
| 49 |
+
["flower.png", "Canny (OpenCV)", 50, 150],
|
| 50 |
+
],
|
| 51 |
+
inputs=[input_image, method, low_threshold, high_threshold],
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# 動態更新閾值參數的顯示
|
| 55 |
+
method.change(
|
| 56 |
+
update_threshold_visibility,
|
| 57 |
+
inputs=[method],
|
| 58 |
+
outputs=[low_threshold, high_threshold]
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# 自動執行邊緣檢測
|
| 62 |
+
input_image.change(
|
| 63 |
+
edge_detection,
|
| 64 |
+
inputs=[input_image, low_threshold, high_threshold, method],
|
| 65 |
+
outputs=[output_image]
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# 手動按鈕執行(備選方式)
|
| 69 |
+
submit_btn = gr.Button("Detect Edges")
|
| 70 |
+
submit_btn.click(
|
| 71 |
+
edge_detection,
|
| 72 |
+
inputs=[input_image, low_threshold, high_threshold, method],
|
| 73 |
+
outputs=[output_image]
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
return demo
|
| 77 |
+
|
| 78 |
+
# 啟動應用
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
app = main()
|
| 81 |
+
app.launch()
|