DawnC commited on
Commit
0aa2a11
·
1 Parent(s): bc22f79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -1
app.py CHANGED
@@ -405,6 +405,103 @@ async def process_single_dog(image):
405
  # 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>')
406
 
407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408
  async def predict(image):
409
  if image is None:
410
  return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
@@ -442,7 +539,7 @@ async def predict(image):
442
  if top1_prob >= 0.45:
443
  breed = topk_breeds[0]
444
  description = get_dog_description(breed)
445
- html_output += f"<p><strong>{breed}</strong> ({top1_prob:.2%} confidence)</p>"
446
  html_output += format_description_html(description, breed)
447
  button_id = f"Dog {i+1}: More about {breed}"
448
  html_output += f'<div class="breed-buttons"><button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button></div>'
@@ -500,6 +597,7 @@ async def predict(image):
500
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
501
  print(error_msg)
502
  return error_msg, None, gr.update(visible=False, choices=[]), None
 
503
 
504
 
505
  def show_details_html(choice, previous_output, initial_state):
 
405
  # 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>')
406
 
407
 
408
+ # async def predict(image):
409
+ # if image is None:
410
+ # return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
411
+
412
+ # try:
413
+ # if isinstance(image, np.ndarray):
414
+ # image = Image.fromarray(image)
415
+
416
+ # dogs = await detect_multiple_dogs(image)
417
+
418
+ # color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
419
+ # html_output = """
420
+ # <style>
421
+ # .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); }
422
+ # .dog-info h2 { background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }
423
+ # .breed-buttons { margin-top: 10px; }
424
+ # .breed-button { margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }
425
+ # </style>
426
+ # """
427
+ # buttons = []
428
+ # annotated_image = image.copy()
429
+ # draw = ImageDraw.Draw(annotated_image)
430
+ # font = ImageFont.load_default()
431
+
432
+ # for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
433
+ # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
434
+ # color = color_list[i % len(color_list)]
435
+ # draw.rectangle(box, outline=color, width=3)
436
+ # draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
437
+
438
+ # combined_confidence = detection_confidence * top1_prob
439
+ # html_output += f'<div class="dog-info" style="border-left: 5px solid {color};">'
440
+ # html_output += f'<h2>Dog {i+1}</h2>'
441
+
442
+ # if top1_prob >= 0.45:
443
+ # breed = topk_breeds[0]
444
+ # description = get_dog_description(breed)
445
+ # html_output += f"<p><strong>{breed}</strong> ({top1_prob:.2%} confidence)</p>"
446
+ # html_output += format_description_html(description, breed)
447
+ # button_id = f"Dog {i+1}: More about {breed}"
448
+ # html_output += f'<div class="breed-buttons"><button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button></div>'
449
+ # buttons.append(button_id)
450
+ # elif combined_confidence >= 0.15:
451
+ # html_output += f"<p>Top 3 possible breeds:</p><ul>"
452
+ # for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
453
+ # html_output += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
454
+ # html_output += "</ul>"
455
+ # html_output += '<div class="breed-buttons">'
456
+ # for breed in topk_breeds[:3]:
457
+ # button_id = f"Dog {i+1}: More about {breed}"
458
+ # html_output += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
459
+ # buttons.append(button_id)
460
+ # html_output += '</div>'
461
+ # else:
462
+ # html_output += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
463
+
464
+ # html_output += '</div>'
465
+
466
+ # if buttons:
467
+ # html_output += """
468
+ # <script>
469
+ # function handle_button_click(button_id) {
470
+ # const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
471
+ # if (radio) {
472
+ # radio.click();
473
+ # } else {
474
+ # console.error("Radio button not found:", button_id);
475
+ # }
476
+ # }
477
+ # </script>
478
+ # """
479
+ # initial_state = {
480
+ # "explanation": html_output,
481
+ # "buttons": buttons,
482
+ # "show_back": True,
483
+ # "image": annotated_image,
484
+ # "is_multi_dog": len(dogs) > 1,
485
+ # "dogs_info": html_output
486
+ # }
487
+ # return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
488
+ # else:
489
+ # initial_state = {
490
+ # "explanation": html_output,
491
+ # "buttons": [],
492
+ # "show_back": False,
493
+ # "image": annotated_image,
494
+ # "is_multi_dog": len(dogs) > 1,
495
+ # "dogs_info": html_output
496
+ # }
497
+ # return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
498
+
499
+ # except Exception as e:
500
+ # error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
501
+ # print(error_msg)
502
+ # return error_msg, None, gr.update(visible=False, choices=[]), None
503
+
504
+
505
  async def predict(image):
506
  if image is None:
507
  return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
 
539
  if top1_prob >= 0.45:
540
  breed = topk_breeds[0]
541
  description = get_dog_description(breed)
542
+ #html_output += f"<p><strong>{breed}</strong> ({top1_prob:.2%} confidence)</p>"
543
  html_output += format_description_html(description, breed)
544
  button_id = f"Dog {i+1}: More about {breed}"
545
  html_output += f'<div class="breed-buttons"><button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button></div>'
 
597
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
598
  print(error_msg)
599
  return error_msg, None, gr.update(visible=False, choices=[]), None
600
+
601
 
602
 
603
  def show_details_html(choice, previous_output, initial_state):