petertulip86 commited on
Commit
fba74bb
·
verified ·
1 Parent(s): 51b1139

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +192 -0
app.py ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import gradio as gr
4
+
5
+ def hsv_filter(image,
6
+ h_low=0, h_high=179,
7
+ s_low=0, s_high=255,
8
+ v_low=0, v_high=255):
9
+ """
10
+ 對輸入的圖像應用 HSV 過濾
11
+
12
+ Args:
13
+ image: 輸入的圖像(來自網路攝像頭或上傳)
14
+ h_low, h_high: Hue(色調)的最低和最高值
15
+ s_low, s_high: Saturation(飽和度)的最低和最高值
16
+ v_low, v_high: Value(亮度)的最低和最高值
17
+
18
+ Returns:
19
+ 原始圖像、HSV 過濾遮罩和過濾後的結果
20
+ """
21
+ if image is None:
22
+ return None, None, None
23
+
24
+ # 將圖像轉換為 HSV 色彩空間
25
+ hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
26
+
27
+ # 設置 HSV 過濾範圍
28
+ lower = np.array([h_low, s_low, v_low])
29
+ upper = np.array([h_high, s_high, v_high])
30
+
31
+ # 建立遮罩
32
+ mask = cv2.inRange(hsv, lower, upper)
33
+
34
+ # 使用遮罩從原始圖像中提取物體
35
+ result = cv2.bitwise_and(image, image, mask=mask)
36
+
37
+ # 將遮罩轉換為 RGB 以便在 Gradio 中顯示
38
+ mask_rgb = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
39
+
40
+ return image, mask_rgb, result
41
+
42
+ def process_webcam(image, h_low, h_high, s_low, s_high, v_low, v_high):
43
+ """
44
+ 處理網路攝像頭的輸入
45
+
46
+ Args:
47
+ image: 來自網路攝像頭的圖像
48
+ h_low, h_high, s_low, s_high, v_low, v_high: HSV 過濾參數
49
+
50
+ Returns:
51
+ 處理後的圖像結果(原始、遮罩、過濾後)
52
+ """
53
+ # Gradio 的 webcam 輸入是 RGB 格式,OpenCV 使用 BGR
54
+ # 所以需要轉換顏色順序
55
+ image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
56
+
57
+ # 應用 HSV 過濾
58
+ orig, mask, result = hsv_filter(
59
+ cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB), # 轉回 RGB 給過濾函數
60
+ h_low, h_high, s_low, s_high, v_low, v_high
61
+ )
62
+
63
+ return orig, mask, result
64
+
65
+ def process_image(image, h_low, h_high, s_low, s_high, v_low, v_high):
66
+ """
67
+ 處理上傳的圖像
68
+
69
+ Args:
70
+ image: 上傳的圖像
71
+ h_low, h_high, s_low, s_high, v_low, v_high: HSV 過濾參數
72
+
73
+ Returns:
74
+ 處理後的圖像結果(原始、遮罩、過濾後)
75
+ """
76
+ if image is None:
77
+ return None, None, None
78
+
79
+ return hsv_filter(image, h_low, h_high, s_low, s_high, v_low, v_high)
80
+
81
+ # 建立 Gradio 界面
82
+ with gr.Blocks(title="HSV 顏色過濾器") as demo:
83
+ gr.Markdown("# HSV 顏色過濾器")
84
+ gr.Markdown("使用滑桿調整 HSV 值範圍,實時過濾圖像中的顏色")
85
+
86
+ with gr.Tab("網路攝像頭"):
87
+ with gr.Row():
88
+ # 網路攝像頭輸入
89
+ webcam_input = gr.Image(source="webcam", streaming=True, label="攝像頭輸入")
90
+
91
+ with gr.Row():
92
+ # HSV 滑桿控制(用於網路攝像頭)
93
+ with gr.Column():
94
+ h_low_webcam = gr.Slider(0, 179, 0, step=1, label="H Low")
95
+ h_high_webcam = gr.Slider(0, 179, 179, step=1, label="H High")
96
+ with gr.Column():
97
+ s_low_webcam = gr.Slider(0, 255, 0, step=1, label="S Low")
98
+ s_high_webcam = gr.Slider(0, 255, 255, step=1, label="S High")
99
+ with gr.Column():
100
+ v_low_webcam = gr.Slider(0, 255, 0, step=1, label="V Low")
101
+ v_high_webcam = gr.Slider(0, 255, 255, step=1, label="V High")
102
+
103
+ with gr.Row():
104
+ # 顯示結果
105
+ webcam_original = gr.Image(label="原始圖像")
106
+ webcam_mask = gr.Image(label="遮罩")
107
+ webcam_result = gr.Image(label="過濾結果")
108
+
109
+ with gr.Tab("圖像上傳"):
110
+ with gr.Row():
111
+ # 圖像上傳區
112
+ upload_input = gr.Image(label="上傳圖像")
113
+
114
+ with gr.Row():
115
+ # HSV 滑桿控制(用於上傳圖像)
116
+ with gr.Column():
117
+ h_low_upload = gr.Slider(0, 179, 0, step=1, label="H Low")
118
+ h_high_upload = gr.Slider(0, 179, 179, step=1, label="H High")
119
+ with gr.Column():
120
+ s_low_upload = gr.Slider(0, 255, 0, step=1, label="S Low")
121
+ s_high_upload = gr.Slider(0, 255, 255, step=1, label="S High")
122
+ with gr.Column():
123
+ v_low_upload = gr.Slider(0, 255, 0, step=1, label="V Low")
124
+ v_high_upload = gr.Slider(0, 255, 255, step=1, label="V High")
125
+
126
+ with gr.Row():
127
+ # 顯示結果
128
+ upload_original = gr.Image(label="原始圖像")
129
+ upload_mask = gr.Image(label="遮罩")
130
+ upload_result = gr.Image(label="過濾結果")
131
+
132
+ # 處理上傳圖像的按鈕
133
+ process_btn = gr.Button("處理圖像")
134
+ process_btn.click(
135
+ fn=process_image,
136
+ inputs=[
137
+ upload_input,
138
+ h_low_upload, h_high_upload,
139
+ s_low_upload, s_high_upload,
140
+ v_low_upload, v_high_upload
141
+ ],
142
+ outputs=[upload_original, upload_mask, upload_result]
143
+ )
144
+
145
+ # 連接網路攝像頭輸入和滑桿變化到處理函數
146
+ webcam_input.change(
147
+ fn=process_webcam,
148
+ inputs=[
149
+ webcam_input,
150
+ h_low_webcam, h_high_webcam,
151
+ s_low_webcam, s_high_webcam,
152
+ v_low_webcam, v_high_webcam
153
+ ],
154
+ outputs=[webcam_original, webcam_mask, webcam_result]
155
+ )
156
+
157
+ # 當滑桿值改變時,重新處理網路攝像頭圖像
158
+ h_low_webcam.change(lambda *args: process_webcam(*args),
159
+ inputs=[webcam_input, h_low_webcam, h_high_webcam, s_low_webcam, s_high_webcam, v_low_webcam, v_high_webcam],
160
+ outputs=[webcam_original, webcam_mask, webcam_result])
161
+ h_high_webcam.change(lambda *args: process_webcam(*args),
162
+ inputs=[webcam_input, h_low_webcam, h_high_webcam, s_low_webcam, s_high_webcam, v_low_webcam, v_high_webcam],
163
+ outputs=[webcam_original, webcam_mask, webcam_result])
164
+ s_low_webcam.change(lambda *args: process_webcam(*args),
165
+ inputs=[webcam_input, h_low_webcam, h_high_webcam, s_low_webcam, s_high_webcam, v_low_webcam, v_high_webcam],
166
+ outputs=[webcam_original, webcam_mask, webcam_result])
167
+ s_high_webcam.change(lambda *args: process_webcam(*args),
168
+ inputs=[webcam_input, h_low_webcam, h_high_webcam, s_low_webcam, s_high_webcam, v_low_webcam, v_high_webcam],
169
+ outputs=[webcam_original, webcam_mask, webcam_result])
170
+ v_low_webcam.change(lambda *args: process_webcam(*args),
171
+ inputs=[webcam_input, h_low_webcam, h_high_webcam, s_low_webcam, s_high_webcam, v_low_webcam, v_high_webcam],
172
+ outputs=[webcam_original, webcam_mask, webcam_result])
173
+ v_high_webcam.change(lambda *args: process_webcam(*args),
174
+ inputs=[webcam_input, h_low_webcam, h_high_webcam, s_low_webcam, s_high_webcam, v_low_webcam, v_high_webcam],
175
+ outputs=[webcam_original, webcam_mask, webcam_result])
176
+
177
+ # 當滑桿值改變時,重新處理上傳的圖像
178
+ for slider in [h_low_upload, h_high_upload, s_low_upload, s_high_upload, v_low_upload, v_high_upload]:
179
+ slider.change(
180
+ fn=process_image,
181
+ inputs=[
182
+ upload_input,
183
+ h_low_upload, h_high_upload,
184
+ s_low_upload, s_high_upload,
185
+ v_low_upload, v_high_upload
186
+ ],
187
+ outputs=[upload_original, upload_mask, upload_result]
188
+ )
189
+
190
+ # 啟動 Gradio 應用
191
+ if __name__ == "__main__":
192
+ demo.launch()