Update app.py
Browse files
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
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 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 |
-
|
| 88 |
with gr.Row():
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
fn=
|
| 93 |
-
inputs=[input_image,
|
| 94 |
-
outputs=
|
| 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
|