AaronWu901225 commited on
Commit
7ceff64
·
verified ·
1 Parent(s): e0a242f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -10
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  import cv2
3
  import numpy as np
4
 
@@ -11,22 +12,19 @@ def edge_detection(image, threshold1, threshold2):
11
  # 圖像分割函式
12
  def image_segmentation(image, compactness):
13
  from skimage.segmentation import slic
14
- from skimage import color
15
  segments = slic(image, compactness=compactness, n_segments=200)
16
  return color.label2rgb(segments, image, kind='avg')
17
 
18
  # 顏色範圍調整函式
19
  def adjust_color(image, r_min, r_max, g_min, g_max, b_min, b_max):
20
- # 確保圖片格式為 RGB
21
  if image.shape[-1] == 4: # 如果圖片有 alpha 通道
22
  image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
23
 
24
- # 調整 R、G、B 通道的範圍
25
- image = image.astype(np.float32) # 將圖像轉為浮點數進行處理
26
  image[:, :, 0] = np.clip(image[:, :, 0], r_min, r_max) # 調整 R 通道
27
  image[:, :, 1] = np.clip(image[:, :, 1], g_min, g_max) # 調整 G 通道
28
  image[:, :, 2] = np.clip(image[:, :, 2], b_min, b_max) # 調整 B 通道
29
- image = image.astype(np.uint8) # 轉回整數型圖像
30
 
31
  return image
32
 
@@ -34,11 +32,12 @@ def adjust_color(image, r_min, r_max, g_min, g_max, b_min, b_max):
34
  def app():
35
  with gr.Blocks() as demo:
36
  gr.Markdown("# 影像處理功能展示")
37
- gr.Markdown("本應用程式展示了使用 OpenCV 實現的影像處理功能,包括邊緣檢測、圖像分割以及動態調整 RGB 範圍的功能。")
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")
@@ -49,10 +48,18 @@ def app():
49
  inputs=[input_image, threshold1, threshold2],
50
  outputs=edge_result
51
  )
52
-
 
 
 
 
 
 
 
 
53
  with gr.Tab("圖像分割"):
54
  with gr.Row():
55
- input_image = gr.Image(label="輸入圖片")
56
  seg_result = gr.Image(label="分割結果")
57
  with gr.Row():
58
  compactness = gr.Slider(0.1, 100, value=10, step=0.1, label="分割緊湊度")
@@ -62,10 +69,18 @@ def app():
62
  inputs=[input_image, compactness],
63
  outputs=seg_result
64
  )
 
 
 
 
 
 
 
65
 
 
66
  with gr.Tab("顏色範圍調整"):
67
  with gr.Row():
68
- input_image = gr.Image(label="輸入圖片")
69
  adjusted_result = gr.Image(label="調整後的圖片")
70
  with gr.Row():
71
  r_min = gr.Slider(0, 255, value=0, step=1, label="R 最小值")
@@ -80,6 +95,13 @@ def app():
80
  inputs=[input_image, r_min, r_max, g_min, g_max, b_min, b_max],
81
  outputs=adjusted_result
82
  )
 
 
 
 
 
 
 
83
 
84
  return demo
85
 
 
1
  import gradio as gr
2
+ from skimage import color
3
  import cv2
4
  import numpy as np
5
 
 
12
  # 圖像分割函式
13
  def image_segmentation(image, compactness):
14
  from skimage.segmentation import slic
 
15
  segments = slic(image, compactness=compactness, n_segments=200)
16
  return color.label2rgb(segments, image, kind='avg')
17
 
18
  # 顏色範圍調整函式
19
  def adjust_color(image, r_min, r_max, g_min, g_max, b_min, b_max):
 
20
  if image.shape[-1] == 4: # 如果圖片有 alpha 通道
21
  image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
22
 
23
+ image = image.astype(np.float32)
 
24
  image[:, :, 0] = np.clip(image[:, :, 0], r_min, r_max) # 調整 R 通道
25
  image[:, :, 1] = np.clip(image[:, :, 1], g_min, g_max) # 調整 G 通道
26
  image[:, :, 2] = np.clip(image[:, :, 2], b_min, b_max) # 調整 B 通道
27
+ image = image.astype(np.uint8)
28
 
29
  return image
30
 
 
32
  def app():
33
  with gr.Blocks() as demo:
34
  gr.Markdown("# 影像處理功能展示")
35
+ gr.Markdown("本應用程式展示了邊緣檢測、圖像分割和顏色範圍調整功能,每個功能附帶範例圖片和測試結果。")
36
 
37
+ # 邊緣檢測功能
38
  with gr.Tab("邊緣檢測"):
39
  with gr.Row():
40
+ input_image = gr.Image(label="輸入圖片", value="simple_plain_pikachu_by_titanplakinside_dexx027.png") # 範例圖片
41
  edge_result = gr.Image(label="邊緣檢測結果")
42
  with gr.Row():
43
  threshold1 = gr.Slider(0, 255, value=100, step=1, label="閾值1")
 
48
  inputs=[input_image, threshold1, threshold2],
49
  outputs=edge_result
50
  )
51
+ gr.Examples(
52
+ examples=["simple_plain_pikachu_by_titanplakinside_dexx027.png"],
53
+ inputs=[input_image],
54
+ outputs=[edge_result],
55
+ fn=lambda x: edge_detection(x, 100, 200),
56
+ label="測試範例"
57
+ )
58
+
59
+ # 圖像分割功能
60
  with gr.Tab("圖像分割"):
61
  with gr.Row():
62
+ input_image = gr.Image(label="輸入圖片", value="simple_plain_pikachu_by_titanplakinside_dexx027.png") # 範例圖片
63
  seg_result = gr.Image(label="分割結果")
64
  with gr.Row():
65
  compactness = gr.Slider(0.1, 100, value=10, step=0.1, label="分割緊湊度")
 
69
  inputs=[input_image, compactness],
70
  outputs=seg_result
71
  )
72
+ gr.Examples(
73
+ examples=["simple_plain_pikachu_by_titanplakinside_dexx027.png"],
74
+ inputs=[input_image],
75
+ outputs=[seg_result],
76
+ fn=lambda x: image_segmentation(x, 10),
77
+ label="測試範例"
78
+ )
79
 
80
+ # 顏色範圍調整功能
81
  with gr.Tab("顏色範圍調整"):
82
  with gr.Row():
83
+ input_image = gr.Image(label="輸入圖片", value="simple_plain_pikachu_by_titanplakinside_dexx027.png") # 範例圖片
84
  adjusted_result = gr.Image(label="調整後的圖片")
85
  with gr.Row():
86
  r_min = gr.Slider(0, 255, value=0, step=1, label="R 最小值")
 
95
  inputs=[input_image, r_min, r_max, g_min, g_max, b_min, b_max],
96
  outputs=adjusted_result
97
  )
98
+ gr.Examples(
99
+ examples=["simple_plain_pikachu_by_titanplakinside_dexx027.png"],
100
+ inputs=[input_image],
101
+ outputs=[adjusted_result],
102
+ fn=lambda x: adjust_color(x, 50, 200, 50, 200, 50, 200),
103
+ label="測試範例"
104
+ )
105
 
106
  return demo
107