Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -308,10 +308,10 @@ async def predict(image):
|
|
308 |
if isinstance(image, np.ndarray):
|
309 |
image = Image.fromarray(image)
|
310 |
|
311 |
-
#
|
312 |
-
dogs = await detect_multiple_dogs(image) #
|
313 |
if len(dogs) == 0:
|
314 |
-
# 單狗情境
|
315 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
316 |
if top1_prob < 0.2:
|
317 |
return "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
@@ -333,7 +333,7 @@ async def predict(image):
|
|
333 |
)
|
334 |
return explanation, gr.update(visible=True, value=f"More about {topk_breeds[0]}"), gr.update(visible=True, value=f"More about {topk_breeds[1]}"), gr.update(visible=True, value=f"More about {topk_breeds[2]}")
|
335 |
|
336 |
-
#
|
337 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
338 |
explanations = []
|
339 |
visible_buttons = []
|
@@ -341,7 +341,7 @@ async def predict(image):
|
|
341 |
draw = ImageDraw.Draw(annotated_image)
|
342 |
font = ImageFont.load_default()
|
343 |
|
344 |
-
#
|
345 |
for i, (cropped_image, _, box) in enumerate(dogs):
|
346 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
347 |
|
@@ -350,7 +350,7 @@ async def predict(image):
|
|
350 |
draw.rectangle(box, outline=color, width=3)
|
351 |
draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
352 |
|
353 |
-
#
|
354 |
if top1_prob >= 0.5:
|
355 |
breed = topk_breeds[0]
|
356 |
description = get_dog_description(breed)
|
@@ -372,10 +372,10 @@ Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds
|
|
372 |
return final_explanation, annotated_image, gr.update(visible=True, choices=visible_buttons), gr.update(visible=False), gr.update(visible=False)
|
373 |
|
374 |
except Exception as e:
|
375 |
-
# 更具體的錯誤訊息
|
376 |
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
377 |
|
378 |
|
|
|
379 |
async def show_details(choice):
|
380 |
if not choice:
|
381 |
return "Please select a breed to view details."
|
|
|
308 |
if isinstance(image, np.ndarray):
|
309 |
image = Image.fromarray(image)
|
310 |
|
311 |
+
# 嘗試檢測多隻狗
|
312 |
+
dogs = await detect_multiple_dogs(image) # 使用 YOLO 檢測多狗
|
313 |
if len(dogs) == 0:
|
314 |
+
# 單狗情境 - 不使用 YOLO,直接進行單狗預測
|
315 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
316 |
if top1_prob < 0.2:
|
317 |
return "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
|
|
333 |
)
|
334 |
return explanation, gr.update(visible=True, value=f"More about {topk_breeds[0]}"), gr.update(visible=True, value=f"More about {topk_breeds[1]}"), gr.update(visible=True, value=f"More about {topk_breeds[2]}")
|
335 |
|
336 |
+
# 處理多狗的情況 - 使用 YOLO 偵測並繪製邊框
|
337 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
338 |
explanations = []
|
339 |
visible_buttons = []
|
|
|
341 |
draw = ImageDraw.Draw(annotated_image)
|
342 |
font = ImageFont.load_default()
|
343 |
|
344 |
+
# 遍歷每一隻狗,繪製邊框並預測
|
345 |
for i, (cropped_image, _, box) in enumerate(dogs):
|
346 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
347 |
|
|
|
350 |
draw.rectangle(box, outline=color, width=3)
|
351 |
draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
352 |
|
353 |
+
# 高置信度返回
|
354 |
if top1_prob >= 0.5:
|
355 |
breed = topk_breeds[0]
|
356 |
description = get_dog_description(breed)
|
|
|
372 |
return final_explanation, annotated_image, gr.update(visible=True, choices=visible_buttons), gr.update(visible=False), gr.update(visible=False)
|
373 |
|
374 |
except Exception as e:
|
|
|
375 |
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
376 |
|
377 |
|
378 |
+
|
379 |
async def show_details(choice):
|
380 |
if not choice:
|
381 |
return "Please select a breed to view details."
|