DawnC commited on
Commit
2c3db84
·
verified ·
1 Parent(s): e83f96e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -20
app.py CHANGED
@@ -363,15 +363,11 @@ async def predict(image):
363
  draw = ImageDraw.Draw(annotated_image)
364
  font = ImageFont.load_default()
365
 
366
- dogs_info_parts = []
 
367
 
368
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
369
- try:
370
- top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
371
- except Exception as e:
372
- print(f"Error predicting dog breed: {e}")
373
- continue
374
-
375
  color = color_list[i % len(color_list)]
376
  draw.rectangle(box, outline=color, width=3)
377
  draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
@@ -384,16 +380,12 @@ async def predict(image):
384
 
385
  if top1_prob >= 0.45:
386
  breed = topk_breeds[0]
387
- try:
388
- description = get_dog_description(breed)
389
- dog_info.append(format_description_html(description, breed))
390
- except Exception as e:
391
- print(f"Error getting dog description: {e}")
392
- dog_info.append("<p>Unable to fetch breed description. Please try again later.</p>")
393
 
394
  elif combined_confidence >= 0.15:
395
  dog_info.append("<p>Top 3 possible breeds:</p><ul>")
396
- for breed, prob in zip(topk_breeds[:3], topk_probs_percent[:3]):
397
  prob = float(prob.replace('%', ''))
398
  dog_info.append(f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>")
399
  dog_info.append("</ul>")
@@ -401,7 +393,7 @@ async def predict(image):
401
  dog_info.append('<div class="breed-buttons">')
402
  for breed in topk_breeds[:3]:
403
  button_id = f"Dog {i+1}: More about {breed}"
404
- dog_info.append(f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')" aria-label="More information about {breed}">{breed}</button>')
405
  buttons.append(button_id)
406
  dog_info.append('</div>')
407
 
@@ -409,10 +401,8 @@ async def predict(image):
409
  dog_info.append("<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>")
410
 
411
  dog_info.append('</div>')
412
- dogs_info_parts.append(''.join(dog_info))
413
-
414
- dogs_info = ''.join(dogs_info_parts)
415
-
416
  html_output = f"""
417
  <style>
418
  .dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }}
@@ -420,7 +410,7 @@ async def predict(image):
420
  .breed-buttons {{ margin-top: 10px; }}
421
  .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
422
  </style>
423
- {dogs_info}
424
  """
425
 
426
  if buttons:
 
363
  draw = ImageDraw.Draw(annotated_image)
364
  font = ImageFont.load_default()
365
 
366
+ dogs_info = []
367
+ buttons = []
368
 
369
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
370
+ top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
 
 
 
 
 
371
  color = color_list[i % len(color_list)]
372
  draw.rectangle(box, outline=color, width=3)
373
  draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
 
380
 
381
  if top1_prob >= 0.45:
382
  breed = topk_breeds[0]
383
+ description = get_dog_description(breed)
384
+ dog_info.append(format_description_html(description, breed))
 
 
 
 
385
 
386
  elif combined_confidence >= 0.15:
387
  dog_info.append("<p>Top 3 possible breeds:</p><ul>")
388
+ for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
389
  prob = float(prob.replace('%', ''))
390
  dog_info.append(f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>")
391
  dog_info.append("</ul>")
 
393
  dog_info.append('<div class="breed-buttons">')
394
  for breed in topk_breeds[:3]:
395
  button_id = f"Dog {i+1}: More about {breed}"
396
+ dog_info.append(f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>')
397
  buttons.append(button_id)
398
  dog_info.append('</div>')
399
 
 
401
  dog_info.append("<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>")
402
 
403
  dog_info.append('</div>')
404
+ dogs_info.append(''.join(dog_info))
405
+
 
 
406
  html_output = f"""
407
  <style>
408
  .dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }}
 
410
  .breed-buttons {{ margin-top: 10px; }}
411
  .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
412
  </style>
413
+ {''.join(dogs_info)}
414
  """
415
 
416
  if buttons: