AaronWu901225 commited on
Commit
f513d1e
·
verified ·
1 Parent(s): e868bf3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -18
app.py CHANGED
@@ -4,53 +4,68 @@ import cv2
4
  import numpy as np
5
  from skimage.restoration import inpaint
6
 
 
7
  def edge_detection(image, threshold1, threshold2):
 
8
  gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
 
9
  edges = cv2.Canny(gray_image, threshold1, threshold2)
10
  return edges
11
 
 
12
  def image_segmentation(image, compactness):
13
  from skimage.segmentation import slic
 
14
  segments = slic(image, compactness=compactness, n_segments=200)
15
  return color.label2rgb(segments, image, kind='avg')
16
 
 
17
  def inpaint_image(image, mask_threshold):
 
18
  gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
 
19
  _, mask = cv2.threshold(gray_image, mask_threshold, 255, cv2.THRESH_BINARY)
20
  mask = mask.astype(bool)
 
21
  inpainted = inpaint.inpaint_biharmonic(image, mask, multichannel=True)
22
  return inpainted
23
 
 
24
  def app():
25
  with gr.Blocks() as demo:
26
- gr.Markdown("# 计算机视觉功能展示")
27
- gr.Markdown("使用 Scikit-Image OpenCV 实现的一些基本功能")
28
- with gr.Tab("边缘检测"):
 
29
  with gr.Row():
30
- input_image = gr.Image(label="输入图像")
31
- edge_result = gr.Image(label="检测结果")
32
  with gr.Row():
33
- threshold1 = gr.Slider(0, 255, value=100, step=1, label="阈值1")
34
- threshold2 = gr.Slider(0, 255, value=200, step=1, label="阈值2")
35
- edge_button = gr.Button("运行边缘检测")
36
  edge_button.click(edge_detection, inputs=[input_image, threshold1, threshold2], outputs=edge_result)
37
- with gr.Tab("图像分割"):
 
38
  with gr.Row():
39
- input_image = gr.Image(label="输入图像")
40
- seg_result = gr.Image(label="分割结果")
41
  with gr.Row():
42
- compactness = gr.Slider(0.1, 100, value=10, step=0.1, label="紧凑度")
43
- seg_button = gr.Button("运行图像分割")
44
  seg_button.click(image_segmentation, inputs=[input_image, compactness], outputs=seg_result)
45
- with gr.Tab("图像修复"):
 
46
  with gr.Row():
47
- input_image = gr.Image(label="输入图像")
48
- inpaint_result = gr.Image(label="修复结果")
49
  with gr.Row():
50
- mask_threshold = gr.Slider(0, 255, value=128, step=1, label="遮罩阈值")
51
- inpaint_button = gr.Button("运行图像修复")
52
  inpaint_button.click(inpaint_image, inputs=[input_image, mask_threshold], outputs=inpaint_result)
 
53
  return demo
54
 
 
55
  if __name__ == "__main__":
56
  app().launch()
 
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
+ # 轉為灰階圖片
25
  gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
26
+ # 根據遮罩閾值生成遮罩
27
  _, mask = cv2.threshold(gray_image, mask_threshold, 255, cv2.THRESH_BINARY)
28
  mask = mask.astype(bool)
29
+ # 使用 biharmonic 方法修復圖像
30
  inpainted = inpaint.inpaint_biharmonic(image, mask, multichannel=True)
31
  return inpainted
32
 
33
+ # 主應用程式
34
  def app():
35
  with gr.Blocks() as demo:
36
+ gr.Markdown("# 影像處理功能展示")
37
+ gr.Markdown("本應用程式展示了使用 Scikit-Image OpenCV 實現的基本影像處理功能。")
38
+
39
+ with gr.Tab("邊緣檢測"):
40
  with gr.Row():
41
+ input_image = gr.Image(label="輸入圖片")
42
+ edge_result = gr.Image(label="邊緣檢測結果")
43
  with gr.Row():
44
+ threshold1 = gr.Slider(0, 255, value=100, step=1, label="閾值1")
45
+ threshold2 = gr.Slider(0, 255, value=200, step=1, label="閾值2")
46
+ edge_button = gr.Button("執行邊緣檢測")
47
  edge_button.click(edge_detection, inputs=[input_image, threshold1, threshold2], outputs=edge_result)
48
+
49
+ with gr.Tab("圖像分割"):
50
  with gr.Row():
51
+ input_image = gr.Image(label="輸入圖片")
52
+ seg_result = gr.Image(label="分割結果")
53
  with gr.Row():
54
+ compactness = gr.Slider(0.1, 100, value=10, step=0.1, label="分割緊湊度")
55
+ seg_button = gr.Button("執行圖像分割")
56
  seg_button.click(image_segmentation, inputs=[input_image, compactness], outputs=seg_result)
57
+
58
+ with gr.Tab("圖像修復"):
59
  with gr.Row():
60
+ input_image = gr.Image(label="輸入圖片")
61
+ inpaint_result = gr.Image(label="修復結果")
62
  with gr.Row():
63
+ mask_threshold = gr.Slider(0, 255, value=128, step=1, label="遮罩閾值")
64
+ inpaint_button = gr.Button("執行圖像修復")
65
  inpaint_button.click(inpaint_image, inputs=[input_image, mask_threshold], outputs=inpaint_result)
66
+
67
  return demo
68
 
69
+ # 啟動應用程式
70
  if __name__ == "__main__":
71
  app().launch()