Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -301,6 +301,117 @@ def _detect_multiple_dogs(image, conf_threshold):
|
|
301 |
return dogs
|
302 |
|
303 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
async def predict(image):
|
305 |
if image is None:
|
306 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
@@ -309,33 +420,16 @@ async def predict(image):
|
|
309 |
if isinstance(image, np.ndarray):
|
310 |
image = Image.fromarray(image)
|
311 |
|
312 |
-
#
|
313 |
-
dogs = await detect_multiple_dogs(image)
|
314 |
if len(dogs) == 0:
|
315 |
# 單狗情境
|
316 |
-
|
317 |
-
if top1_prob < 0.2:
|
318 |
-
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)
|
319 |
-
|
320 |
-
breed = topk_breeds[0]
|
321 |
-
description = get_dog_description(breed)
|
322 |
-
|
323 |
-
if top1_prob >= 0.5:
|
324 |
-
formatted_description = format_description(description, breed)
|
325 |
-
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
326 |
-
else:
|
327 |
-
explanation = (
|
328 |
-
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
329 |
-
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
330 |
-
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
331 |
-
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
332 |
-
"Click on a button to view more information about the breed."
|
333 |
-
)
|
334 |
-
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]}")
|
335 |
|
336 |
# 多狗情境
|
337 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
338 |
explanations = []
|
|
|
339 |
annotated_image = image.copy()
|
340 |
draw = ImageDraw.Draw(annotated_image)
|
341 |
font = ImageFont.load_default()
|
@@ -352,31 +446,51 @@ async def predict(image):
|
|
352 |
formatted_description = format_description(description, breed)
|
353 |
explanations.append(f"Dog {i+1}: {formatted_description}")
|
354 |
elif top1_prob >= 0.2:
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
else:
|
360 |
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
361 |
|
362 |
final_explanation = "\n\n".join(explanations)
|
363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
364 |
|
365 |
except Exception as e:
|
366 |
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
367 |
|
368 |
-
def
|
369 |
-
|
370 |
-
|
|
|
371 |
|
372 |
-
|
373 |
-
|
374 |
-
description = get_dog_description(breed)
|
375 |
-
return format_description(description, breed)
|
376 |
-
except Exception as e:
|
377 |
-
return f"An error occurred while showing details: {e}"
|
378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
379 |
|
|
|
380 |
with gr.Blocks() as iface:
|
381 |
gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
|
382 |
gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>")
|
|
|
301 |
return dogs
|
302 |
|
303 |
|
304 |
+
# async def predict(image):
|
305 |
+
# if image is None:
|
306 |
+
# return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
307 |
+
|
308 |
+
# try:
|
309 |
+
# if isinstance(image, np.ndarray):
|
310 |
+
# image = Image.fromarray(image)
|
311 |
+
|
312 |
+
# # 嘗試檢測多隻狗
|
313 |
+
# dogs = await detect_multiple_dogs(image)
|
314 |
+
# if len(dogs) == 0:
|
315 |
+
# # 單狗情境
|
316 |
+
# top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
317 |
+
# if top1_prob < 0.2:
|
318 |
+
# 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)
|
319 |
+
|
320 |
+
# breed = topk_breeds[0]
|
321 |
+
# description = get_dog_description(breed)
|
322 |
+
|
323 |
+
# if top1_prob >= 0.5:
|
324 |
+
# formatted_description = format_description(description, breed)
|
325 |
+
# return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
326 |
+
# else:
|
327 |
+
# explanation = (
|
328 |
+
# f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
329 |
+
# f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
330 |
+
# f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
331 |
+
# f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
332 |
+
# "Click on a button to view more information about the breed."
|
333 |
+
# )
|
334 |
+
# 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]}")
|
335 |
+
|
336 |
+
# # 多狗情境
|
337 |
+
# color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
338 |
+
# explanations = []
|
339 |
+
# annotated_image = image.copy()
|
340 |
+
# draw = ImageDraw.Draw(annotated_image)
|
341 |
+
# font = ImageFont.load_default()
|
342 |
+
|
343 |
+
# for i, (cropped_image, _, box) in enumerate(dogs):
|
344 |
+
# top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
345 |
+
# color = color_list[i % len(color_list)]
|
346 |
+
# draw.rectangle(box, outline=color, width=3)
|
347 |
+
# draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
348 |
+
|
349 |
+
# breed = topk_breeds[0]
|
350 |
+
# if top1_prob >= 0.5:
|
351 |
+
# description = get_dog_description(breed)
|
352 |
+
# formatted_description = format_description(description, breed)
|
353 |
+
# explanations.append(f"Dog {i+1}: {formatted_description}")
|
354 |
+
# elif top1_prob >= 0.2:
|
355 |
+
# explanations.append(f"Dog {i+1}: Top 3 possible breeds:\n"
|
356 |
+
# f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
357 |
+
# f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
358 |
+
# f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)")
|
359 |
+
# else:
|
360 |
+
# explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
361 |
+
|
362 |
+
# final_explanation = "\n\n".join(explanations)
|
363 |
+
# return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
364 |
+
|
365 |
+
# except Exception as e:
|
366 |
+
# return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
367 |
+
|
368 |
+
# def show_details(choice):
|
369 |
+
# if not choice:
|
370 |
+
# return "Please select a breed to view details."
|
371 |
+
|
372 |
+
# try:
|
373 |
+
# breed = choice.split("More about ")[-1]
|
374 |
+
# description = get_dog_description(breed)
|
375 |
+
# return format_description(description, breed)
|
376 |
+
# except Exception as e:
|
377 |
+
# return f"An error occurred while showing details: {e}"
|
378 |
+
|
379 |
+
|
380 |
+
# with gr.Blocks() as iface:
|
381 |
+
# gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
|
382 |
+
# gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>")
|
383 |
+
|
384 |
+
# with gr.Row():
|
385 |
+
# input_image = gr.Image(label="Upload a dog image", type="pil")
|
386 |
+
# output_image = gr.Image(label="Annotated Image")
|
387 |
+
|
388 |
+
# output = gr.Markdown(label="Prediction Results")
|
389 |
+
|
390 |
+
# with gr.Row():
|
391 |
+
# btn1 = gr.Button("View More 1", visible=False)
|
392 |
+
# btn2 = gr.Button("View More 2", visible=False)
|
393 |
+
# btn3 = gr.Button("View More 3", visible=False)
|
394 |
+
|
395 |
+
# input_image.change(
|
396 |
+
# predict,
|
397 |
+
# inputs=input_image,
|
398 |
+
# outputs=[output, output_image, btn1, btn2, btn3]
|
399 |
+
# )
|
400 |
+
|
401 |
+
# btn1.click(show_details, inputs=btn1, outputs=output)
|
402 |
+
# btn2.click(show_details, inputs=btn2, outputs=output)
|
403 |
+
# btn3.click(show_details, inputs=btn3, outputs=output)
|
404 |
+
|
405 |
+
# gr.Examples(
|
406 |
+
# examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
|
407 |
+
# inputs=input_image
|
408 |
+
# )
|
409 |
+
|
410 |
+
# gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
|
411 |
+
|
412 |
+
# if __name__ == "__main__":
|
413 |
+
# iface.launch()
|
414 |
+
|
415 |
async def predict(image):
|
416 |
if image is None:
|
417 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
|
|
420 |
if isinstance(image, np.ndarray):
|
421 |
image = Image.fromarray(image)
|
422 |
|
423 |
+
# 嘗試檢測多隻狗,降低閾值以提高檢測率
|
424 |
+
dogs = await detect_multiple_dogs(image, conf_threshold=0.2)
|
425 |
if len(dogs) == 0:
|
426 |
# 單狗情境
|
427 |
+
return await process_single_dog(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
428 |
|
429 |
# 多狗情境
|
430 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
431 |
explanations = []
|
432 |
+
buttons = []
|
433 |
annotated_image = image.copy()
|
434 |
draw = ImageDraw.Draw(annotated_image)
|
435 |
font = ImageFont.load_default()
|
|
|
446 |
formatted_description = format_description(description, breed)
|
447 |
explanations.append(f"Dog {i+1}: {formatted_description}")
|
448 |
elif top1_prob >= 0.2:
|
449 |
+
dog_explanation = f"Dog {i+1}: Top 3 possible breeds:\n"
|
450 |
+
dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
|
451 |
+
explanations.append(dog_explanation)
|
452 |
+
buttons.extend([gr.update(visible=True, value=f"Dog {i+1}: More about {breed}") for breed in topk_breeds[:3]])
|
453 |
else:
|
454 |
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
455 |
|
456 |
final_explanation = "\n\n".join(explanations)
|
457 |
+
if buttons:
|
458 |
+
final_explanation += "\n\nClick on a button to view more information about the breed."
|
459 |
+
return (final_explanation, annotated_image,
|
460 |
+
buttons[0] if len(buttons) > 0 else gr.update(visible=False),
|
461 |
+
buttons[1] if len(buttons) > 1 else gr.update(visible=False),
|
462 |
+
buttons[2] if len(buttons) > 2 else gr.update(visible=False))
|
463 |
+
else:
|
464 |
+
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
465 |
|
466 |
except Exception as e:
|
467 |
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
468 |
|
469 |
+
async def process_single_dog(image):
|
470 |
+
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
471 |
+
if top1_prob < 0.2:
|
472 |
+
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)
|
473 |
|
474 |
+
breed = topk_breeds[0]
|
475 |
+
description = get_dog_description(breed)
|
|
|
|
|
|
|
|
|
476 |
|
477 |
+
if top1_prob >= 0.5:
|
478 |
+
formatted_description = format_description(description, breed)
|
479 |
+
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
480 |
+
else:
|
481 |
+
explanation = (
|
482 |
+
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
483 |
+
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
484 |
+
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
485 |
+
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
486 |
+
"Click on a button to view more information about the breed."
|
487 |
+
)
|
488 |
+
return (explanation, image,
|
489 |
+
gr.update(visible=True, value=f"More about {topk_breeds[0]}"),
|
490 |
+
gr.update(visible=True, value=f"More about {topk_breeds[1]}"),
|
491 |
+
gr.update(visible=True, value=f"More about {topk_breeds[2]}"))
|
492 |
|
493 |
+
# 介面部分
|
494 |
with gr.Blocks() as iface:
|
495 |
gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
|
496 |
gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>")
|