AaronWu901225 commited on
Commit
80bc771
·
verified ·
1 Parent(s): 868c22c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -40
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import gradio as gr
2
- from skimage.restoration import inpaint
3
  from skimage import color
4
  import cv2
5
  import numpy as np
@@ -16,37 +15,33 @@ def image_segmentation(image, compactness):
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
- try:
22
- # 確保圖片格式為 RGB
23
- if image.shape[-1] == 4: # 如果圖片有 alpha 通道
24
- image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
25
- elif image.shape[-1] == 3:
26
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
27
-
28
- # 轉為灰階圖片
29
- gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
30
- # 根據遮罩閾值生成遮罩
31
- _, mask = cv2.threshold(gray_image, mask_threshold, 255, cv2.THRESH_BINARY)
32
- mask = mask.astype(bool)
33
-
34
- # 如果遮罩無效,返回遮罩檢查圖片
35
- if not mask.any():
36
- error_image = np.zeros_like(image)
37
- cv2.putText(error_image, "Invalid Mask! Adjust Threshold", (10, 50),
38
- cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
39
- return error_image
40
-
41
- # 使用 biharmonic 方法修復圖像
42
- inpainted = inpaint.inpaint_biharmonic(image, mask, multichannel=True)
43
- return inpainted
44
- except Exception as e:
45
- # 如果發生錯誤,返回錯誤圖片
46
- error_image = np.zeros((image.shape[0], image.shape[1], 3), dtype=np.uint8)
47
- cv2.putText(error_image, f"Error: {str(e)}", (10, 50),
48
- cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
49
- return error_image
50
 
51
  # 主應用程式
52
  def app():
@@ -81,17 +76,17 @@ def app():
81
  outputs=seg_result
82
  )
83
 
84
- with gr.Tab("圖像修復"):
85
  with gr.Row():
86
  input_image = gr.Image(label="輸入圖片")
87
- inpaint_result = gr.Image(label="修復結果或錯誤訊息")
88
  with gr.Row():
89
- mask_threshold = gr.Slider(0, 255, value=128, step=1, label="遮罩閾值")
90
- inpaint_button = gr.Button("執行圖像修復")
91
- inpaint_button.click(
92
- fn=inpaint_image,
93
- inputs=[input_image, mask_threshold],
94
- outputs=inpaint_result # 錯誤訊息或結果會顯示在此
95
  )
96
 
97
  return demo
 
1
  import gradio as gr
 
2
  from skimage import color
3
  import cv2
4
  import numpy as np
 
15
  segments = slic(image, compactness=compactness, n_segments=200)
16
  return color.label2rgb(segments, image, kind='avg')
17
 
18
+ # 色彩濾鏡函式
19
+ def color_filter(image, color_choice):
20
+ # 將圖像轉換為 HSV 格式
21
+ hsv_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
22
+
23
+ # 定義顏色範圍(H: 色調,S: 飽和度,V: 明度)
24
+ if color_choice == "紅色":
25
+ lower_bound = np.array([0, 50, 50]) # 紅色範圍
26
+ upper_bound = np.array([10, 255, 255])
27
+ elif color_choice == "綠色":
28
+ lower_bound = np.array([40, 50, 50]) # 綠色範圍
29
+ upper_bound = np.array([80, 255, 255])
30
+ elif color_choice == "藍色":
31
+ lower_bound = np.array([100, 50, 50]) # 藍色範圍
32
+ upper_bound = np.array([140, 255, 255])
33
+ else:
34
+ return "未支援的顏色範圍選擇"
35
+
36
+ # 創建遮罩,過濾選定的顏色範圍
37
+ mask = cv2.inRange(hsv_image, lower_bound, upper_bound)
38
+
39
+ # 保留選定顏色,將其他部分設為灰階
40
+ result = image.copy()
41
+ gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
42
+ result[mask == 0] = np.stack([gray_image] * 3, axis=-1)[mask == 0]
43
+
44
+ return result
 
 
 
 
45
 
46
  # 主應用程式
47
  def app():
 
76
  outputs=seg_result
77
  )
78
 
79
+ with gr.Tab("色彩濾鏡"):
80
  with gr.Row():
81
  input_image = gr.Image(label="輸入圖片")
82
+ filter_result = gr.Image(label="濾鏡結果")
83
  with gr.Row():
84
+ color_choice = gr.Radio(["紅色", "綠色", "藍色"], label="選擇顏色", value="紅色")
85
+ filter_button = gr.Button("應用濾鏡")
86
+ filter_button.click(
87
+ fn=color_filter,
88
+ inputs=[input_image, color_choice],
89
+ outputs=filter_result
90
  )
91
 
92
  return demo