Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -251,9 +251,9 @@ def get_akc_breeds_link():
|
|
251 |
|
252 |
|
253 |
|
254 |
-
# Update the format_description to handle descriptions more cleanly
|
255 |
def format_description(description, breed):
|
256 |
if isinstance(description, dict):
|
|
|
257 |
formatted_description = "\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
258 |
else:
|
259 |
formatted_description = description
|
@@ -271,6 +271,7 @@ Please refer to the AKC's terms of use and privacy policy.*
|
|
271 |
return formatted_description
|
272 |
|
273 |
|
|
|
274 |
async def predict_single_dog(image):
|
275 |
return await asyncio.to_thread(_predict_single_dog, image)
|
276 |
|
@@ -307,6 +308,7 @@ def _detect_multiple_dogs(image, conf_threshold=0.3):
|
|
307 |
return dogs
|
308 |
|
309 |
|
|
|
310 |
async def predict(image):
|
311 |
if image is None:
|
312 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
@@ -315,8 +317,10 @@ async def predict(image):
|
|
315 |
if isinstance(image, np.ndarray):
|
316 |
image = Image.fromarray(image)
|
317 |
|
|
|
318 |
dogs = await detect_multiple_dogs(image)
|
319 |
|
|
|
320 |
if len(dogs) == 0:
|
321 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
322 |
if top1_prob < 0.2:
|
@@ -326,6 +330,7 @@ async def predict(image):
|
|
326 |
formatted_description = format_description(description, breed)
|
327 |
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
328 |
|
|
|
329 |
if len(dogs) == 1:
|
330 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
331 |
breed = topk_breeds[0]
|
@@ -333,7 +338,7 @@ async def predict(image):
|
|
333 |
formatted_description = format_description(description, breed)
|
334 |
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
335 |
|
336 |
-
#
|
337 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
338 |
explanations = []
|
339 |
visible_buttons = []
|
@@ -341,14 +346,16 @@ 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 |
|
348 |
-
|
|
|
349 |
draw.rectangle(box, outline=color, width=3)
|
350 |
draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
351 |
|
|
|
352 |
if top1_prob >= 0.5:
|
353 |
breed = topk_breeds[0]
|
354 |
description = get_dog_description(breed)
|
@@ -366,12 +373,14 @@ Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds
|
|
366 |
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
367 |
|
368 |
final_explanation = "\n\n".join(explanations)
|
|
|
369 |
return final_explanation, annotated_image, gr.update(visible=True, choices=visible_buttons), gr.update(visible=False), gr.update(visible=False)
|
370 |
|
371 |
except Exception as e:
|
372 |
return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
373 |
|
374 |
|
|
|
375 |
async def show_details(choice):
|
376 |
if not choice:
|
377 |
return "Please select a breed to view details."
|
|
|
251 |
|
252 |
|
253 |
|
|
|
254 |
def format_description(description, breed):
|
255 |
if isinstance(description, dict):
|
256 |
+
# 確保每一個描述項目換行顯示
|
257 |
formatted_description = "\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
258 |
else:
|
259 |
formatted_description = description
|
|
|
271 |
return formatted_description
|
272 |
|
273 |
|
274 |
+
|
275 |
async def predict_single_dog(image):
|
276 |
return await asyncio.to_thread(_predict_single_dog, image)
|
277 |
|
|
|
308 |
return dogs
|
309 |
|
310 |
|
311 |
+
|
312 |
async def predict(image):
|
313 |
if image is None:
|
314 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
|
|
317 |
if isinstance(image, np.ndarray):
|
318 |
image = Image.fromarray(image)
|
319 |
|
320 |
+
# 偵測圖片中的多個狗
|
321 |
dogs = await detect_multiple_dogs(image)
|
322 |
|
323 |
+
# 單一狗的情況
|
324 |
if len(dogs) == 0:
|
325 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
326 |
if top1_prob < 0.2:
|
|
|
330 |
formatted_description = format_description(description, breed)
|
331 |
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
332 |
|
333 |
+
# 單隻狗時,直接返回結果
|
334 |
if len(dogs) == 1:
|
335 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
336 |
breed = topk_breeds[0]
|
|
|
338 |
formatted_description = format_description(description, breed)
|
339 |
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
340 |
|
341 |
+
# 處理多隻狗的情況
|
342 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
343 |
explanations = []
|
344 |
visible_buttons = []
|
|
|
346 |
draw = ImageDraw.Draw(annotated_image)
|
347 |
font = ImageFont.load_default()
|
348 |
|
349 |
+
# 遍歷每一隻狗並返回結果
|
350 |
for i, (cropped_image, _, box) in enumerate(dogs):
|
351 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
352 |
|
353 |
+
# 畫框及標籤顏色
|
354 |
+
color = color_list[i % len(color_list)]
|
355 |
draw.rectangle(box, outline=color, width=3)
|
356 |
draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
357 |
|
358 |
+
# 高置信度情況
|
359 |
if top1_prob >= 0.5:
|
360 |
breed = topk_breeds[0]
|
361 |
description = get_dog_description(breed)
|
|
|
373 |
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
374 |
|
375 |
final_explanation = "\n\n".join(explanations)
|
376 |
+
|
377 |
return final_explanation, annotated_image, gr.update(visible=True, choices=visible_buttons), gr.update(visible=False), gr.update(visible=False)
|
378 |
|
379 |
except Exception as e:
|
380 |
return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
381 |
|
382 |
|
383 |
+
|
384 |
async def show_details(choice):
|
385 |
if not choice:
|
386 |
return "Please select a breed to view details."
|