kigison commited on
Commit
c9ceaab
·
verified ·
1 Parent(s): c77ffc1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from skimage import io, color, restoration
5
+
6
+ # 圖像分割功能
7
+ def image_segmentation(image):
8
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
9
+ _, segmented = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)
10
+ return segmented
11
+
12
+ # 邊緣檢測功能
13
+ def edge_detection(image, threshold1, threshold2):
14
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
15
+ edges = cv2.Canny(gray, threshold1, threshold2)
16
+ return edges
17
+
18
+ # 圖像修復功能(Inpainting)
19
+ def image_inpainting(image, mask):
20
+ mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) if len(mask.shape) == 3 else mask
21
+ inpainted = cv2.inpaint(image, mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)
22
+ return inpainted
23
+
24
+ # 示例圖片列表
25
+ example_images = [
26
+ ("example1.jpg", "測試圖片 1"),
27
+ ("example2.jpg", "測試圖片 2"),
28
+ ]
29
+
30
+ # 定義 Gradio 接口
31
+ def main_interface(image, task, param1=50, param2=150, mask=None):
32
+ if task == "Image Segmentation":
33
+ return image_segmentation(image)
34
+ elif task == "Edge Detection":
35
+ return edge_detection(image, param1, param2)
36
+ elif task == "Image Inpainting" and mask is not None:
37
+ return image_inpainting(image, mask)
38
+ else:
39
+ return "Invalid Task or Missing Parameters!"
40
+
41
+ # UI 設計
42
+ with gr.Blocks() as app:
43
+ gr.Markdown("# 電腦視覺功能展示應用程式")
44
+ with gr.Row():
45
+ with gr.Column():
46
+ input_image = gr.Image(label="上傳圖片", type="numpy")
47
+ task = gr.Radio(["Image Segmentation", "Edge Detection", "Image Inpainting"], label="選擇功能")
48
+ param1 = gr.Slider(0, 200, value=50, step=1, label="參數 1 (Edge Detection Threshold 1)")
49
+ param2 = gr.Slider(0, 200, value=150, step=1, label="參數 2 (Edge Detection Threshold 2)")
50
+ mask_input = gr.Image(label="上傳遮罩 (僅適用於 Image Inpainting)", type="numpy", optional=True)
51
+ submit_button = gr.Button("執行")
52
+ with gr.Column():
53
+ output_image = gr.Image(label="輸出結果")
54
+ submit_button.click(main_interface, inputs=[input_image, task, param1, param2, mask_input], outputs=output_image)
55
+
56
+ # 啟動應用程式
57
+ if __name__ == "__main__":
58
+ app.launch()