Initial commit
Browse files- app.py +54 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
def segment_image(image, spatial_radius=20, color_radius=30):
|
| 6 |
+
segmented = cv2.pyrMeanShiftFiltering(image, spatial_radius, color_radius)
|
| 7 |
+
return segmented
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def edge_detection(image, threshold1=100, threshold2=200):
|
| 11 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 12 |
+
edges = cv2.Canny(gray, threshold1, threshold2)
|
| 13 |
+
return edges
|
| 14 |
+
|
| 15 |
+
def edge_detection_interface(image, threshold1, threshold2):
|
| 16 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Gradio 輸入為 RGB
|
| 17 |
+
edges = edge_detection(image, threshold1, threshold2)
|
| 18 |
+
return edges
|
| 19 |
+
|
| 20 |
+
def segmentation_interface(image, spatial_radius, color_radius):
|
| 21 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 22 |
+
segmented = segment_image(image, spatial_radius, color_radius)
|
| 23 |
+
return cv2.cvtColor(segmented, cv2.COLOR_BGR2RGB)
|
| 24 |
+
|
| 25 |
+
with gr.Blocks() as demo:
|
| 26 |
+
gr.Markdown("### OpenCV 基本影像處理工具")
|
| 27 |
+
|
| 28 |
+
with gr.Tab("邊緣檢測"):
|
| 29 |
+
with gr.Row():
|
| 30 |
+
input_image = gr.Image(label="上傳影像")
|
| 31 |
+
output_image = gr.Image(label="結果影像")
|
| 32 |
+
with gr.Row():
|
| 33 |
+
threshold1_slider = gr.Slider(0, 255, value=100, label="弱邊緣檢測閾值 (低)")
|
| 34 |
+
threshold2_slider = gr.Slider(0, 255, value=200, label="強邊緣檢測閾值 (高)")
|
| 35 |
+
edge_button = gr.Button("執行")
|
| 36 |
+
edge_button.click(edge_detection_interface,
|
| 37 |
+
inputs=[input_image, threshold1_slider, threshold2_slider],
|
| 38 |
+
outputs=output_image)
|
| 39 |
+
|
| 40 |
+
with gr.Tab("影像分割"):
|
| 41 |
+
with gr.Row():
|
| 42 |
+
input_image_seg = gr.Image(label="上傳影像")
|
| 43 |
+
output_image_seg = gr.Image(label="結果影像")
|
| 44 |
+
with gr.Row():
|
| 45 |
+
spatial_slider = gr.Slider(1, 50, value=20, label="Spatial Radius")
|
| 46 |
+
color_slider = gr.Slider(1, 50, value=30, label="Color Radius")
|
| 47 |
+
segment_button = gr.Button("執行")
|
| 48 |
+
segment_button.click(segmentation_interface,
|
| 49 |
+
inputs=[input_image_seg, spatial_slider, color_slider],
|
| 50 |
+
outputs=output_image_seg)
|
| 51 |
+
|
| 52 |
+
# 啟動介面
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
opencv-python
|
| 3 |
+
numpy
|