Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -277,7 +277,7 @@ def predict_breed(cropped_image):
|
|
277 |
|
278 |
def predict(image):
|
279 |
if image is None:
|
280 |
-
return "
|
281 |
|
282 |
try:
|
283 |
if isinstance(image, np.ndarray):
|
@@ -285,7 +285,7 @@ def predict(image):
|
|
285 |
|
286 |
dogs = detect_dogs(image)
|
287 |
if len(dogs) == 0:
|
288 |
-
return "
|
289 |
|
290 |
explanations = []
|
291 |
visible_buttons = []
|
@@ -296,29 +296,29 @@ def predict(image):
|
|
296 |
top1_prob, topk_breeds, topk_probs_percent = predict_breed(cropped_image)
|
297 |
|
298 |
draw.rectangle(box, outline="red", width=3)
|
299 |
-
draw.text((box[0], box[1]), f"
|
300 |
|
301 |
if top1_prob >= 0.5:
|
302 |
breed = topk_breeds[0]
|
303 |
description = get_dog_description(breed)
|
304 |
-
explanations.append(f"
|
305 |
elif 0.2 <= top1_prob < 0.5:
|
306 |
explanation = (
|
307 |
-
f"
|
308 |
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]})\n"
|
309 |
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]})\n"
|
310 |
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]})\n"
|
311 |
)
|
312 |
explanations.append(explanation)
|
313 |
-
visible_buttons.extend([f"
|
314 |
else:
|
315 |
-
explanations.append(f"
|
316 |
|
317 |
final_explanation = "\n\n".join(explanations)
|
318 |
return final_explanation, annotated_image, gr.update(visible=len(visible_buttons) >= 1, value=visible_buttons[0] if visible_buttons else ""), gr.update(visible=len(visible_buttons) >= 2, value=visible_buttons[1] if len(visible_buttons) >= 2 else ""), gr.update(visible=len(visible_buttons) >= 3, value=visible_buttons[2] if len(visible_buttons) >= 3 else "")
|
319 |
|
320 |
except Exception as e:
|
321 |
-
return f"
|
322 |
|
323 |
def format_description(description, breed):
|
324 |
if isinstance(description, dict):
|
@@ -327,18 +327,18 @@ def format_description(description, breed):
|
|
327 |
formatted_description = description
|
328 |
|
329 |
akc_link = get_akc_breeds_link()
|
330 |
-
formatted_description += f"\n\n
|
331 |
|
332 |
-
disclaimer = ("\n\n
|
333 |
-
"
|
334 |
-
"
|
335 |
-
"
|
336 |
formatted_description += disclaimer
|
337 |
|
338 |
return formatted_description
|
339 |
|
340 |
def show_details(breed):
|
341 |
-
breed_name = breed.split("
|
342 |
description = get_dog_description(breed_name)
|
343 |
return format_description(description, breed_name)
|
344 |
|
@@ -373,19 +373,19 @@ with gr.Blocks(css="""
|
|
373 |
}
|
374 |
""") as iface:
|
375 |
|
376 |
-
gr.HTML("<h1 style='font-family:Roboto; font-weight:bold; color:#2C3E50; text-align:center;'>🐶
|
377 |
-
gr.HTML("<p style='font-family:Open Sans; color:#34495E; text-align:center;'
|
378 |
|
379 |
with gr.Row():
|
380 |
-
input_image = gr.Image(label="
|
381 |
-
output_image = gr.Image(label="
|
382 |
|
383 |
-
output = gr.Markdown(label="
|
384 |
|
385 |
with gr.Row():
|
386 |
-
btn1 = gr.Button("
|
387 |
-
btn2 = gr.Button("
|
388 |
-
btn3 = gr.Button("
|
389 |
|
390 |
input_image.change(predict, inputs=input_image, outputs=[output, output_image, btn1, btn2, btn3])
|
391 |
|
@@ -398,7 +398,7 @@ with gr.Blocks(css="""
|
|
398 |
inputs=input_image
|
399 |
)
|
400 |
|
401 |
-
gr.HTML('
|
402 |
|
403 |
if __name__ == "__main__":
|
404 |
iface.launch()
|
|
|
277 |
|
278 |
def predict(image):
|
279 |
if image is None:
|
280 |
+
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
281 |
|
282 |
try:
|
283 |
if isinstance(image, np.ndarray):
|
|
|
285 |
|
286 |
dogs = detect_dogs(image)
|
287 |
if len(dogs) == 0:
|
288 |
+
return "No dogs detected or the image is unclear. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
289 |
|
290 |
explanations = []
|
291 |
visible_buttons = []
|
|
|
296 |
top1_prob, topk_breeds, topk_probs_percent = predict_breed(cropped_image)
|
297 |
|
298 |
draw.rectangle(box, outline="red", width=3)
|
299 |
+
draw.text((box[0], box[1]), f"Dog {i+1}", fill="red")
|
300 |
|
301 |
if top1_prob >= 0.5:
|
302 |
breed = topk_breeds[0]
|
303 |
description = get_dog_description(breed)
|
304 |
+
explanations.append(f"Dog {i+1}: **{breed}**\n{format_description(description, breed)}")
|
305 |
elif 0.2 <= top1_prob < 0.5:
|
306 |
explanation = (
|
307 |
+
f"Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds:\n"
|
308 |
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]})\n"
|
309 |
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]})\n"
|
310 |
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]})\n"
|
311 |
)
|
312 |
explanations.append(explanation)
|
313 |
+
visible_buttons.extend([f"More about {topk_breeds[0]}", f"More about {topk_breeds[1]}", f"More about {topk_breeds[2]}"])
|
314 |
else:
|
315 |
+
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
316 |
|
317 |
final_explanation = "\n\n".join(explanations)
|
318 |
return final_explanation, annotated_image, gr.update(visible=len(visible_buttons) >= 1, value=visible_buttons[0] if visible_buttons else ""), gr.update(visible=len(visible_buttons) >= 2, value=visible_buttons[1] if len(visible_buttons) >= 2 else ""), gr.update(visible=len(visible_buttons) >= 3, value=visible_buttons[2] if len(visible_buttons) >= 3 else "")
|
319 |
|
320 |
except Exception as e:
|
321 |
+
return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
322 |
|
323 |
def format_description(description, breed):
|
324 |
if isinstance(description, dict):
|
|
|
327 |
formatted_description = description
|
328 |
|
329 |
akc_link = get_akc_breeds_link()
|
330 |
+
formatted_description += f"\n\n**Want to learn more about dog breeds?** [Visit the AKC dog breeds page]({akc_link}) and search for {breed} to find detailed information."
|
331 |
|
332 |
+
disclaimer = ("\n\n*Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page. "
|
333 |
+
"You may need to search for the specific breed on that page. "
|
334 |
+
"I am not responsible for the content on external sites. "
|
335 |
+
"Please refer to the AKC's terms of use and privacy policy.*")
|
336 |
formatted_description += disclaimer
|
337 |
|
338 |
return formatted_description
|
339 |
|
340 |
def show_details(breed):
|
341 |
+
breed_name = breed.split("More about ")[-1]
|
342 |
description = get_dog_description(breed_name)
|
343 |
return format_description(description, breed_name)
|
344 |
|
|
|
373 |
}
|
374 |
""") as iface:
|
375 |
|
376 |
+
gr.HTML("<h1 style='font-family:Roboto; font-weight:bold; color:#2C3E50; text-align:center;'>🐶 Dog Breed Classifier 🔍</h1>")
|
377 |
+
gr.HTML("<p style='font-family:Open Sans; color:#34495E; 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>")
|
378 |
|
379 |
with gr.Row():
|
380 |
+
input_image = gr.Image(label="Upload a dog image", type="pil")
|
381 |
+
output_image = gr.Image(label="Annotated Image")
|
382 |
|
383 |
+
output = gr.Markdown(label="Prediction Results")
|
384 |
|
385 |
with gr.Row():
|
386 |
+
btn1 = gr.Button("View More 1", visible=False)
|
387 |
+
btn2 = gr.Button("View More 2", visible=False)
|
388 |
+
btn3 = gr.Button("View More 3", visible=False)
|
389 |
|
390 |
input_image.change(predict, inputs=input_image, outputs=[output, output_image, btn1, btn2, btn3])
|
391 |
|
|
|
398 |
inputs=input_image
|
399 |
)
|
400 |
|
401 |
+
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%20Breed%20Classifier">Dog Breed Classifier</a>')
|
402 |
|
403 |
if __name__ == "__main__":
|
404 |
iface.launch()
|