Spaces:
Running
on
Zero
Running
on
Zero
Update breed_detection.py
Browse files- breed_detection.py +13 -11
breed_detection.py
CHANGED
@@ -35,8 +35,9 @@ def create_detection_tab(predict_fn, example_images):
|
|
35 |
</div>
|
36 |
""")
|
37 |
|
|
|
38 |
with gr.Tabs():
|
39 |
-
#
|
40 |
with gr.TabItem("Upload Image"):
|
41 |
input_image = gr.Image(label="Upload a dog image", type="pil")
|
42 |
gr.Examples(
|
@@ -44,47 +45,48 @@ def create_detection_tab(predict_fn, example_images):
|
|
44 |
inputs=input_image
|
45 |
)
|
46 |
|
47 |
-
#
|
48 |
with gr.TabItem("Take Photo"):
|
49 |
-
camera_input = gr.
|
50 |
-
source="webcam",
|
51 |
-
label="Take a photo of a dog",
|
52 |
-
type="pil",
|
53 |
-
tool="editor" # 允許用戶編輯拍攝的照片
|
54 |
-
)
|
55 |
|
56 |
-
#
|
57 |
with gr.Row():
|
58 |
output_image = gr.Image(label="Annotated Image")
|
59 |
output = gr.HTML(label="Prediction Results")
|
60 |
|
61 |
-
#
|
62 |
initial_state = gr.State()
|
63 |
|
|
|
64 |
def detect_from_inputs(upload_image, camera_image):
|
|
|
65 |
image_to_use = camera_image if camera_image is not None else upload_image
|
66 |
|
67 |
if image_to_use is None:
|
68 |
return "Please upload an image or take a photo first.", None, None
|
69 |
|
|
|
70 |
return predict_fn(image_to_use)
|
71 |
|
|
|
72 |
input_image.change(
|
73 |
predict_fn,
|
74 |
inputs=input_image,
|
75 |
outputs=[output, output_image, initial_state]
|
76 |
)
|
77 |
|
78 |
-
#
|
79 |
camera_input.change(
|
80 |
predict_fn,
|
81 |
inputs=camera_input,
|
82 |
outputs=[output, output_image, initial_state]
|
83 |
)
|
84 |
|
|
|
85 |
with gr.Row():
|
86 |
detect_btn = gr.Button("Detect Breed", variant="primary")
|
87 |
|
|
|
88 |
detect_btn.click(
|
89 |
detect_from_inputs,
|
90 |
inputs=[input_image, camera_input],
|
|
|
35 |
</div>
|
36 |
""")
|
37 |
|
38 |
+
# 將輸入方法放在標籤頁中
|
39 |
with gr.Tabs():
|
40 |
+
# 標籤頁 1: 上傳圖片 (保留原有功能)
|
41 |
with gr.TabItem("Upload Image"):
|
42 |
input_image = gr.Image(label="Upload a dog image", type="pil")
|
43 |
gr.Examples(
|
|
|
45 |
inputs=input_image
|
46 |
)
|
47 |
|
48 |
+
# 標籤頁 2: 拍照功能 (使用 gr.Webcam 而非 gr.Image(source="webcam"))
|
49 |
with gr.TabItem("Take Photo"):
|
50 |
+
camera_input = gr.Webcam(label="Take a photo of a dog")
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
# 輸出區域
|
53 |
with gr.Row():
|
54 |
output_image = gr.Image(label="Annotated Image")
|
55 |
output = gr.HTML(label="Prediction Results")
|
56 |
|
57 |
+
# 使用 State 保存預測結果
|
58 |
initial_state = gr.State()
|
59 |
|
60 |
+
# 輔助函數,在函數內部定義避免循環導入
|
61 |
def detect_from_inputs(upload_image, camera_image):
|
62 |
+
# 使用最後修改的圖片(優先相機拍攝的圖片)
|
63 |
image_to_use = camera_image if camera_image is not None else upload_image
|
64 |
|
65 |
if image_to_use is None:
|
66 |
return "Please upload an image or take a photo first.", None, None
|
67 |
|
68 |
+
# 使用作為參數傳入的 predict_fn
|
69 |
return predict_fn(image_to_use)
|
70 |
|
71 |
+
# 修改輸入圖片事件處理
|
72 |
input_image.change(
|
73 |
predict_fn,
|
74 |
inputs=input_image,
|
75 |
outputs=[output, output_image, initial_state]
|
76 |
)
|
77 |
|
78 |
+
# 添加相機拍攝事件處理 (針對 gr.Webcam)
|
79 |
camera_input.change(
|
80 |
predict_fn,
|
81 |
inputs=camera_input,
|
82 |
outputs=[output, output_image, initial_state]
|
83 |
)
|
84 |
|
85 |
+
# 添加按鈕以便使用者可以主動觸發分析
|
86 |
with gr.Row():
|
87 |
detect_btn = gr.Button("Detect Breed", variant="primary")
|
88 |
|
89 |
+
# 為按鈕設置事件處理
|
90 |
detect_btn.click(
|
91 |
detect_from_inputs,
|
92 |
inputs=[input_image, camera_input],
|