Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -303,29 +303,27 @@ def _detect_multiple_dogs(image, conf_threshold):
|
|
| 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)
|
| 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)
|
| 319 |
|
| 320 |
breed = topk_breeds[0]
|
| 321 |
description = get_dog_description(breed)
|
| 322 |
-
formatted_description = format_description(description, breed)
|
| 323 |
|
| 324 |
-
# 如果置信度高於 0.5,返回結果
|
| 325 |
if top1_prob >= 0.5:
|
| 326 |
-
|
|
|
|
| 327 |
else:
|
| 328 |
-
# 如果置信度不足,顯示前三個可能的品種
|
| 329 |
explanation = (
|
| 330 |
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
| 331 |
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
|
@@ -333,13 +331,13 @@ async def predict(image):
|
|
| 333 |
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
| 334 |
"Click on a button to view more information about the breed."
|
| 335 |
)
|
| 336 |
-
|
| 337 |
-
return explanation,
|
| 338 |
|
| 339 |
-
# 多狗情境
|
| 340 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
| 341 |
explanations = []
|
| 342 |
-
|
| 343 |
annotated_image = image.copy()
|
| 344 |
draw = ImageDraw.Draw(annotated_image)
|
| 345 |
font = ImageFont.load_default()
|
|
@@ -352,24 +350,28 @@ async def predict(image):
|
|
| 352 |
|
| 353 |
if top1_prob >= 0.5:
|
| 354 |
breed = topk_breeds[0]
|
| 355 |
-
|
| 356 |
-
explanations.append(f"Dog {i+1}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
else:
|
| 358 |
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
| 359 |
|
| 360 |
final_explanation = "\n\n".join(explanations)
|
| 361 |
-
return final_explanation, annotated_image, gr.update(visible=True, choices=
|
| 362 |
|
| 363 |
except Exception as e:
|
| 364 |
-
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False)
|
| 365 |
-
|
| 366 |
|
| 367 |
async def show_details(choice):
|
| 368 |
if not choice:
|
| 369 |
return "Please select a breed to view details."
|
| 370 |
|
| 371 |
try:
|
| 372 |
-
# 解析出用戶選擇��品種
|
| 373 |
if "Dog" in choice:
|
| 374 |
_, breed = choice.split(": ", 1)
|
| 375 |
else:
|
|
@@ -379,17 +381,7 @@ async def show_details(choice):
|
|
| 379 |
except Exception as e:
|
| 380 |
return f"An error occurred while showing details: {e}"
|
| 381 |
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
with gr.Blocks(css="""
|
| 386 |
-
.container { max-width: 900px; margin: auto; padding: 20px; }
|
| 387 |
-
.gr-box { border-radius: 15px; }
|
| 388 |
-
.output-markdown { margin-top: 20px; padding: 15px; background-color: #f5f5f5; border-radius: 10px; }
|
| 389 |
-
.examples { display: flex; justify-content: center; flex-wrap: wrap; gap: 10px; margin-top: 20px; }
|
| 390 |
-
.examples img { width: 100px; height: 100px; object-fit: cover; }
|
| 391 |
-
""") as iface:
|
| 392 |
-
|
| 393 |
gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
|
| 394 |
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>")
|
| 395 |
|
|
@@ -401,14 +393,8 @@ with gr.Blocks(css="""
|
|
| 401 |
breed_buttons = gr.Radio([], label="Select breed for more details", visible=False)
|
| 402 |
breed_details = gr.Markdown(label="Breed Details")
|
| 403 |
|
| 404 |
-
async def safe_predict(image):
|
| 405 |
-
try:
|
| 406 |
-
return await predict(image)
|
| 407 |
-
except Exception as e:
|
| 408 |
-
return str(e), None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 409 |
-
|
| 410 |
input_image.change(
|
| 411 |
-
|
| 412 |
inputs=input_image,
|
| 413 |
outputs=[output, output_image, breed_buttons, breed_details]
|
| 414 |
)
|
|
@@ -424,7 +410,6 @@ with gr.Blocks(css="""
|
|
| 424 |
inputs=input_image
|
| 425 |
)
|
| 426 |
|
| 427 |
-
|
| 428 |
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>')
|
| 429 |
|
| 430 |
if __name__ == "__main__":
|
|
|
|
| 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)
|
| 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)
|
| 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)
|
| 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"
|
|
|
|
| 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 |
+
choices = [f"More about {breed}" for breed in topk_breeds[:3]]
|
| 335 |
+
return explanation, image, gr.update(visible=True, choices=choices), gr.update(visible=False)
|
| 336 |
|
| 337 |
+
# 多狗情境
|
| 338 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
| 339 |
explanations = []
|
| 340 |
+
choices = []
|
| 341 |
annotated_image = image.copy()
|
| 342 |
draw = ImageDraw.Draw(annotated_image)
|
| 343 |
font = ImageFont.load_default()
|
|
|
|
| 350 |
|
| 351 |
if top1_prob >= 0.5:
|
| 352 |
breed = topk_breeds[0]
|
| 353 |
+
choices.append(f"Dog {i+1}: {breed}")
|
| 354 |
+
explanations.append(f"Dog {i+1}: **{breed}** ({topk_probs_percent[0]} confidence)")
|
| 355 |
+
elif top1_prob >= 0.2:
|
| 356 |
+
explanations.append(f"Dog {i+1}: Top 3 possible breeds:\n"
|
| 357 |
+
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
| 358 |
+
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
| 359 |
+
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)")
|
| 360 |
+
choices.extend([f"Dog {i+1}: {breed}" for breed in topk_breeds[:3]])
|
| 361 |
else:
|
| 362 |
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
| 363 |
|
| 364 |
final_explanation = "\n\n".join(explanations)
|
| 365 |
+
return final_explanation, annotated_image, gr.update(visible=True, choices=choices), gr.update(visible=False)
|
| 366 |
|
| 367 |
except Exception as e:
|
| 368 |
+
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False)
|
|
|
|
| 369 |
|
| 370 |
async def show_details(choice):
|
| 371 |
if not choice:
|
| 372 |
return "Please select a breed to view details."
|
| 373 |
|
| 374 |
try:
|
|
|
|
| 375 |
if "Dog" in choice:
|
| 376 |
_, breed = choice.split(": ", 1)
|
| 377 |
else:
|
|
|
|
| 381 |
except Exception as e:
|
| 382 |
return f"An error occurred while showing details: {e}"
|
| 383 |
|
| 384 |
+
with gr.Blocks() as iface:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
|
| 386 |
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>")
|
| 387 |
|
|
|
|
| 393 |
breed_buttons = gr.Radio([], label="Select breed for more details", visible=False)
|
| 394 |
breed_details = gr.Markdown(label="Breed Details")
|
| 395 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
input_image.change(
|
| 397 |
+
predict,
|
| 398 |
inputs=input_image,
|
| 399 |
outputs=[output, output_image, breed_buttons, breed_details]
|
| 400 |
)
|
|
|
|
| 410 |
inputs=input_image
|
| 411 |
)
|
| 412 |
|
|
|
|
| 413 |
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>')
|
| 414 |
|
| 415 |
if __name__ == "__main__":
|