Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -301,6 +301,82 @@ 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)
|
|
@@ -312,30 +388,13 @@ async def predict(image):
|
|
| 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()
|
|
@@ -346,25 +405,51 @@ async def predict(image):
|
|
| 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 |
-
|
| 350 |
-
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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 show_details(choice):
|
| 369 |
if not choice:
|
| 370 |
return "Please select a breed to view details."
|
|
@@ -376,6 +461,8 @@ def show_details(choice):
|
|
| 376 |
except Exception as e:
|
| 377 |
return f"An error occurred while showing details: {e}"
|
| 378 |
|
|
|
|
|
|
|
| 379 |
with gr.Blocks() as iface:
|
| 380 |
gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
|
| 381 |
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 |
async def predict(image):
|
| 381 |
if image is None:
|
| 382 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
|
|
|
| 388 |
# 嘗試檢測多隻狗
|
| 389 |
dogs = await detect_multiple_dogs(image)
|
| 390 |
if len(dogs) == 0:
|
| 391 |
+
# 單狗情境 - 保持不變
|
| 392 |
+
return process_single_dog(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
|
| 394 |
# 多狗情境
|
| 395 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
| 396 |
explanations = []
|
| 397 |
+
choices = []
|
| 398 |
annotated_image = image.copy()
|
| 399 |
draw = ImageDraw.Draw(annotated_image)
|
| 400 |
font = ImageFont.load_default()
|
|
|
|
| 405 |
draw.rectangle(box, outline=color, width=3)
|
| 406 |
draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
| 407 |
|
| 408 |
+
if top1_prob < 0.2:
|
| 409 |
+
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset. Please upload a clearer image of the dog.")
|
| 410 |
+
elif top1_prob >= 0.5:
|
| 411 |
+
breed = topk_breeds[0]
|
| 412 |
description = get_dog_description(breed)
|
| 413 |
formatted_description = format_description(description, breed)
|
| 414 |
explanations.append(f"Dog {i+1}: {formatted_description}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
else:
|
| 416 |
+
dog_explanation = f"Dog {i+1}: The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n"
|
| 417 |
+
dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
|
| 418 |
+
explanations.append(dog_explanation)
|
| 419 |
+
choices.extend([f"Dog {i+1}: More about {breed}" for breed in topk_breeds[:3]])
|
| 420 |
|
| 421 |
final_explanation = "\n\n".join(explanations)
|
| 422 |
+
if choices:
|
| 423 |
+
final_explanation += "\n\nClick on a button to view more information about the breed."
|
| 424 |
+
return final_explanation, annotated_image, gr.update(visible=True, choices=choices), gr.update(visible=False), gr.update(visible=False)
|
| 425 |
+
else:
|
| 426 |
+
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 427 |
|
| 428 |
except Exception as e:
|
| 429 |
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 430 |
|
| 431 |
+
def process_single_dog(image):
|
| 432 |
+
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
| 433 |
+
if top1_prob < 0.2:
|
| 434 |
+
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)
|
| 435 |
+
|
| 436 |
+
breed = topk_breeds[0]
|
| 437 |
+
description = get_dog_description(breed)
|
| 438 |
+
|
| 439 |
+
if top1_prob >= 0.5:
|
| 440 |
+
formatted_description = format_description(description, breed)
|
| 441 |
+
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 442 |
+
else:
|
| 443 |
+
explanation = (
|
| 444 |
+
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
| 445 |
+
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
| 446 |
+
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
| 447 |
+
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
| 448 |
+
"Click on a button to view more information about the breed."
|
| 449 |
+
)
|
| 450 |
+
choices = [f"More about {breed}" for breed in topk_breeds[:3]]
|
| 451 |
+
return explanation, image, gr.update(visible=True, choices=choices), gr.update(visible=False), gr.update(visible=False)
|
| 452 |
+
|
| 453 |
def show_details(choice):
|
| 454 |
if not choice:
|
| 455 |
return "Please select a breed to view details."
|
|
|
|
| 461 |
except Exception as e:
|
| 462 |
return f"An error occurred while showing details: {e}"
|
| 463 |
|
| 464 |
+
|
| 465 |
+
|
| 466 |
with gr.Blocks() as iface:
|
| 467 |
gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
|
| 468 |
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>")
|