Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -296,81 +296,80 @@ async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.5):
|
|
296 |
return dogs
|
297 |
|
298 |
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
#
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
#
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
# return f"An error occurred while showing details: {e}"
|
374 |
|
375 |
|
376 |
# with gr.Blocks() as iface:
|
@@ -409,7 +408,6 @@ async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.5):
|
|
409 |
# iface.launch()
|
410 |
|
411 |
|
412 |
-
|
413 |
# 介面部分
|
414 |
with gr.Blocks() as iface:
|
415 |
gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
|
|
|
296 |
return dogs
|
297 |
|
298 |
|
299 |
+
async def predict(image):
|
300 |
+
if image is None:
|
301 |
+
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
302 |
+
|
303 |
+
try:
|
304 |
+
if isinstance(image, np.ndarray):
|
305 |
+
image = Image.fromarray(image)
|
306 |
+
|
307 |
+
# 嘗試檢測多隻狗
|
308 |
+
dogs = await detect_multiple_dogs(image)
|
309 |
+
if len(dogs) == 0:
|
310 |
+
# 單狗情境
|
311 |
+
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
312 |
+
if top1_prob < 0.2:
|
313 |
+
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)
|
314 |
+
|
315 |
+
breed = topk_breeds[0]
|
316 |
+
description = get_dog_description(breed)
|
317 |
+
|
318 |
+
if top1_prob >= 0.5:
|
319 |
+
formatted_description = format_description(description, breed)
|
320 |
+
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
321 |
+
else:
|
322 |
+
explanation = (
|
323 |
+
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
324 |
+
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
325 |
+
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
326 |
+
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
327 |
+
"Click on a button to view more information about the breed."
|
328 |
+
)
|
329 |
+
return explanation, image, 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]}")
|
330 |
+
|
331 |
+
# 多狗情境
|
332 |
+
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
333 |
+
explanations = []
|
334 |
+
annotated_image = image.copy()
|
335 |
+
draw = ImageDraw.Draw(annotated_image)
|
336 |
+
font = ImageFont.load_default()
|
337 |
+
|
338 |
+
for i, (cropped_image, _, box) in enumerate(dogs):
|
339 |
+
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
340 |
+
color = color_list[i % len(color_list)]
|
341 |
+
draw.rectangle(box, outline=color, width=3)
|
342 |
+
draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
343 |
+
|
344 |
+
breed = topk_breeds[0]
|
345 |
+
if top1_prob >= 0.5:
|
346 |
+
description = get_dog_description(breed)
|
347 |
+
formatted_description = format_description(description, breed)
|
348 |
+
explanations.append(f"Dog {i+1}: {formatted_description}")
|
349 |
+
elif top1_prob >= 0.2:
|
350 |
+
explanations.append(f"Dog {i+1}: Top 3 possible breeds:\n"
|
351 |
+
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
352 |
+
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
353 |
+
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)")
|
354 |
+
else:
|
355 |
+
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
356 |
+
|
357 |
+
final_explanation = "\n\n".join(explanations)
|
358 |
+
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
359 |
+
|
360 |
+
except Exception as e:
|
361 |
+
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
362 |
+
|
363 |
+
def show_details(choice):
|
364 |
+
if not choice:
|
365 |
+
return "Please select a breed to view details."
|
366 |
+
|
367 |
+
try:
|
368 |
+
breed = choice.split("More about ")[-1]
|
369 |
+
description = get_dog_description(breed)
|
370 |
+
return format_description(description, breed)
|
371 |
+
except Exception as e:
|
372 |
+
return f"An error occurred while showing details: {e}"
|
|
|
373 |
|
374 |
|
375 |
# with gr.Blocks() as iface:
|
|
|
408 |
# iface.launch()
|
409 |
|
410 |
|
|
|
411 |
# 介面部分
|
412 |
with gr.Blocks() as iface:
|
413 |
gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
|