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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -363,42 +363,56 @@ async def predict(image):
363
  draw = ImageDraw.Draw(annotated_image)
364
  font = ImageFont.load_default()
365
 
366
- dogs_info = ""
367
 
368
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
369
- top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
 
 
 
 
 
370
  color = color_list[i % len(color_list)]
371
  draw.rectangle(box, outline=color, width=3)
372
  draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
373
 
374
  combined_confidence = detection_confidence * top1_prob
375
- dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
376
- dogs_info += f'<h2>Dog {i+1}</h2>'
 
 
377
 
378
  if top1_prob >= 0.45:
379
  breed = topk_breeds[0]
380
- description = get_dog_description(breed)
381
- dogs_info += format_description_html(description, breed)
 
 
 
 
382
 
383
  elif combined_confidence >= 0.15:
384
- dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
385
- for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
386
  prob = float(prob.replace('%', ''))
387
- dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
388
- dogs_info += "</ul>"
389
 
390
- dogs_info += '<div class="breed-buttons">'
391
  for breed in topk_breeds[:3]:
392
  button_id = f"Dog {i+1}: More about {breed}"
393
- dogs_info += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
394
  buttons.append(button_id)
395
- dogs_info += '</div>'
396
 
397
  else:
398
- dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
399
-
400
- dogs_info += '</div>'
401
 
 
 
 
 
 
402
  html_output = f"""
403
  <style>
404
  .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); }}
 
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)
378
 
379
  combined_confidence = detection_confidence * top1_prob
380
+ dog_info = [
381
+ f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">',
382
+ f'<h2>Dog {i+1}</h2>'
383
+ ]
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>")
400
 
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
 
408
  else:
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); }}