AaronWu901225 commited on
Commit
61e169d
·
verified ·
1 Parent(s): 9945008

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -1,44 +1,43 @@
1
  import gradio as gr
2
- from skimage import io, filters, color
 
3
  import cv2
4
  import numpy as np
5
- from skimage.restoration import inpaint
6
 
7
  # 邊緣檢測函式
8
  def edge_detection(image, threshold1, threshold2):
9
- # 轉為灰階圖片
10
  gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
11
- # 使用 Canny 邊緣檢測
12
  edges = cv2.Canny(gray_image, threshold1, threshold2)
13
  return edges
14
 
15
  # 圖像分割函式
16
  def image_segmentation(image, compactness):
17
  from skimage.segmentation import slic
18
- # 使用 SLIC 方法進行圖像分割
19
  segments = slic(image, compactness=compactness, n_segments=200)
20
  return color.label2rgb(segments, image, kind='avg')
21
 
22
  # 圖像修復函式
23
  def inpaint_image(image, mask_threshold):
24
  # 確保圖片格式為 RGB
25
- if image.shape[-1] == 4: # 若圖片有 alpha 通道
26
  image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
27
  elif image.shape[-1] == 3:
28
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
29
 
 
30
  gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
 
31
  _, mask = cv2.threshold(gray_image, mask_threshold, 255, cv2.THRESH_BINARY)
32
  mask = mask.astype(bool)
33
 
 
34
  if not mask.any():
35
  raise ValueError("遮罩生成失敗,請調整遮罩閾值參數。")
36
 
 
37
  inpainted = inpaint.inpaint_biharmonic(image, mask, multichannel=True)
38
  return inpainted
39
 
40
-
41
-
42
  # 主應用程式
43
  def app():
44
  with gr.Blocks() as demo:
@@ -53,7 +52,11 @@ def app():
53
  threshold1 = gr.Slider(0, 255, value=100, step=1, label="閾值1")
54
  threshold2 = gr.Slider(0, 255, value=200, step=1, label="閾值2")
55
  edge_button = gr.Button("執行邊緣檢測")
56
- edge_button.click(edge_detection, inputs=[input_image, threshold1, threshold2], outputs=edge_result)
 
 
 
 
57
 
58
  with gr.Tab("圖像分割"):
59
  with gr.Row():
@@ -62,7 +65,11 @@ def app():
62
  with gr.Row():
63
  compactness = gr.Slider(0.1, 100, value=10, step=0.1, label="分割緊湊度")
64
  seg_button = gr.Button("執行圖像分割")
65
- seg_button.click(image_segmentation, inputs=[input_image, compactness], outputs=seg_result)
 
 
 
 
66
 
67
  with gr.Tab("圖像修復"):
68
  with gr.Row():
@@ -75,12 +82,11 @@ def app():
75
  fn=inpaint_image,
76
  inputs=[input_image, mask_threshold],
77
  outputs=inpaint_result,
78
- show_error=True # 這會將錯誤訊息顯示在 UI
79
  )
80
-
81
 
82
  return demo
83
 
84
  # 啟動應用程式
85
  if __name__ == "__main__":
86
- app().launch()
 
1
  import gradio as gr
2
+ from skimage.restoration import inpaint
3
+ from skimage import color
4
  import cv2
5
  import numpy as np
 
6
 
7
  # 邊緣檢測函式
8
  def edge_detection(image, threshold1, threshold2):
 
9
  gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
 
10
  edges = cv2.Canny(gray_image, threshold1, threshold2)
11
  return edges
12
 
13
  # 圖像分割函式
14
  def image_segmentation(image, compactness):
15
  from skimage.segmentation import slic
 
16
  segments = slic(image, compactness=compactness, n_segments=200)
17
  return color.label2rgb(segments, image, kind='avg')
18
 
19
  # 圖像修復函式
20
  def inpaint_image(image, mask_threshold):
21
  # 確保圖片格式為 RGB
22
+ if image.shape[-1] == 4: # 如果圖片有 alpha 通道
23
  image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
24
  elif image.shape[-1] == 3:
25
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
26
 
27
+ # 轉為灰階圖片
28
  gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
29
+ # 根據遮罩閾值生成遮罩
30
  _, mask = cv2.threshold(gray_image, mask_threshold, 255, cv2.THRESH_BINARY)
31
  mask = mask.astype(bool)
32
 
33
+ # 如果遮罩無效,拋出錯誤
34
  if not mask.any():
35
  raise ValueError("遮罩生成失敗,請調整遮罩閾值參數。")
36
 
37
+ # 使用 biharmonic 方法修復圖像
38
  inpainted = inpaint.inpaint_biharmonic(image, mask, multichannel=True)
39
  return inpainted
40
 
 
 
41
  # 主應用程式
42
  def app():
43
  with gr.Blocks() as demo:
 
52
  threshold1 = gr.Slider(0, 255, value=100, step=1, label="閾值1")
53
  threshold2 = gr.Slider(0, 255, value=200, step=1, label="閾值2")
54
  edge_button = gr.Button("執行邊緣檢測")
55
+ edge_button.click(
56
+ fn=edge_detection,
57
+ inputs=[input_image, threshold1, threshold2],
58
+ outputs=edge_result
59
+ )
60
 
61
  with gr.Tab("圖像分割"):
62
  with gr.Row():
 
65
  with gr.Row():
66
  compactness = gr.Slider(0.1, 100, value=10, step=0.1, label="分割緊湊度")
67
  seg_button = gr.Button("執行圖像分割")
68
+ seg_button.click(
69
+ fn=image_segmentation,
70
+ inputs=[input_image, compactness],
71
+ outputs=seg_result
72
+ )
73
 
74
  with gr.Tab("圖像修復"):
75
  with gr.Row():
 
82
  fn=inpaint_image,
83
  inputs=[input_image, mask_threshold],
84
  outputs=inpaint_result,
85
+ show_error=True # UI 上顯示錯誤訊息
86
  )
 
87
 
88
  return demo
89
 
90
  # 啟動應用程式
91
  if __name__ == "__main__":
92
+ app().launch()