DawnC commited on
Commit
5c648b4
โ€ข
1 Parent(s): d59eb07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -161
app.py CHANGED
@@ -301,120 +301,9 @@ def _detect_multiple_dogs(image, conf_threshold):
301
  return dogs
302
 
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), 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), 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), 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"
330
- # f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} 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
- # return explanation, image, gr.update(visible=True, value=f"More about {topk_breeds[0]}"), gr.update(visible=True, value=f"More about {topk_breeds[1]}"), gr.update(visible=True, value=f"More about {topk_breeds[2]}")
335
-
336
- # # ๅคš็‹—ๆƒ…ๅขƒ
337
- # color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
338
- # explanations = []
339
- # annotated_image = image.copy()
340
- # draw = ImageDraw.Draw(annotated_image)
341
- # font = ImageFont.load_default()
342
-
343
- # for i, (cropped_image, _, box) in enumerate(dogs):
344
- # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
345
- # color = color_list[i % len(color_list)]
346
- # draw.rectangle(box, outline=color, width=3)
347
- # draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
348
-
349
- # breed = topk_breeds[0]
350
- # if top1_prob >= 0.5:
351
- # description = get_dog_description(breed)
352
- # formatted_description = format_description(description, breed)
353
- # explanations.append(f"Dog {i+1}: {formatted_description}")
354
- # elif top1_prob >= 0.2:
355
- # explanations.append(f"Dog {i+1}: Top 3 possible breeds:\n"
356
- # f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
357
- # f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
358
- # f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)")
359
- # else:
360
- # explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
361
-
362
- # final_explanation = "\n\n".join(explanations)
363
- # return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
364
-
365
- # except Exception as e:
366
- # return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
367
-
368
- # def show_details(choice):
369
- # if not choice:
370
- # return "Please select a breed to view details."
371
-
372
- # try:
373
- # breed = choice.split("More about ")[-1]
374
- # description = get_dog_description(breed)
375
- # return format_description(description, breed)
376
- # except Exception as e:
377
- # return f"An error occurred while showing details: {e}"
378
-
379
-
380
- # with gr.Blocks() as iface:
381
- # gr.HTML("<h1 style='text-align: center;'>๐Ÿถ Dog Breed Classifier ๐Ÿ”</h1>")
382
- # 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>")
383
-
384
- # with gr.Row():
385
- # input_image = gr.Image(label="Upload a dog image", type="pil")
386
- # output_image = gr.Image(label="Annotated Image")
387
-
388
- # output = gr.Markdown(label="Prediction Results")
389
-
390
- # with gr.Row():
391
- # btn1 = gr.Button("View More 1", visible=False)
392
- # btn2 = gr.Button("View More 2", visible=False)
393
- # btn3 = gr.Button("View More 3", visible=False)
394
-
395
- # input_image.change(
396
- # predict,
397
- # inputs=input_image,
398
- # outputs=[output, output_image, btn1, btn2, btn3]
399
- # )
400
-
401
- # btn1.click(show_details, inputs=btn1, outputs=output)
402
- # btn2.click(show_details, inputs=btn2, outputs=output)
403
- # btn3.click(show_details, inputs=btn3, outputs=output)
404
-
405
- # gr.Examples(
406
- # examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
407
- # inputs=input_image
408
- # )
409
-
410
- # 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>')
411
-
412
- # if __name__ == "__main__":
413
- # iface.launch()
414
-
415
  async def predict(image):
416
  if image is None:
417
- return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False)
418
 
419
  try:
420
  if isinstance(image, np.ndarray):
@@ -424,12 +313,29 @@ async def predict(image):
424
  dogs = await detect_multiple_dogs(image)
425
  if len(dogs) == 0:
426
  # ๅ–ฎ็‹—ๆƒ…ๅขƒ
427
- return await process_single_dog(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
 
429
  # ๅคš็‹—ๆƒ…ๅขƒ
430
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
431
  explanations = []
432
- choices = []
433
  annotated_image = image.copy()
434
  draw = ImageDraw.Draw(annotated_image)
435
  font = ImageFont.load_default()
@@ -440,63 +346,37 @@ async def predict(image):
440
  draw.rectangle(box, outline=color, width=3)
441
  draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
442
 
443
- if top1_prob < 0.2:
444
- explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset. Please upload a clearer image of the dog.")
445
- elif top1_prob >= 0.5:
446
- breed = topk_breeds[0]
447
  description = get_dog_description(breed)
448
  formatted_description = format_description(description, breed)
449
  explanations.append(f"Dog {i+1}: {formatted_description}")
 
 
 
 
 
450
  else:
451
- dog_explanation = f"Dog {i+1}: The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n"
452
- dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
453
- explanations.append(dog_explanation)
454
- choices.extend([f"Dog {i+1}: {breed}" for breed in topk_breeds[:3]])
455
 
456
  final_explanation = "\n\n".join(explanations)
457
- if choices:
458
- final_explanation += "\n\nClick on a button to view more information about the breed."
459
- return final_explanation, annotated_image, gr.update(visible=True, choices=choices), gr.update(visible=False)
460
- else:
461
- return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False)
462
 
463
  except Exception as e:
464
- return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False)
465
-
466
- async def process_single_dog(image):
467
- top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
468
- if top1_prob < 0.2:
469
- 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)
470
-
471
- breed = topk_breeds[0]
472
- description = get_dog_description(breed)
473
-
474
- if top1_prob >= 0.5:
475
- formatted_description = format_description(description, breed)
476
- return formatted_description, image, gr.update(visible=False), gr.update(visible=False)
477
- else:
478
- explanation = (
479
- f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
480
- f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
481
- f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
482
- f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
483
- "Click on a button to view more information about the breed."
484
- )
485
- choices = [f"{breed}" for breed in topk_breeds[:3]]
486
- return explanation, image, gr.update(visible=True, choices=choices), gr.update(visible=False)
487
 
488
  def show_details(choice):
489
  if not choice:
490
  return "Please select a breed to view details."
491
 
492
  try:
493
- breed = choice.split(": ")[-1] # ่™•็†ๅฏ่ƒฝ็š„ "Dog X: " ๅ‰็ถด
494
  description = get_dog_description(breed)
495
  return format_description(description, breed)
496
  except Exception as e:
497
  return f"An error occurred while showing details: {e}"
498
 
499
- # ไป‹้ข้ƒจๅˆ†
500
  with gr.Blocks() as iface:
501
  gr.HTML("<h1 style='text-align: center;'>๐Ÿถ Dog Breed Classifier ๐Ÿ”</h1>")
502
  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>")
@@ -506,20 +386,21 @@ with gr.Blocks() as iface:
506
  output_image = gr.Image(label="Annotated Image")
507
 
508
  output = gr.Markdown(label="Prediction Results")
509
- breed_choices = gr.Radio([], label="Select breed for more details", visible=False)
510
- breed_details = gr.Markdown(label="Breed Details")
 
 
 
511
 
512
  input_image.change(
513
  predict,
514
  inputs=input_image,
515
- outputs=[output, output_image, breed_choices, breed_details]
516
  )
517
 
518
- breed_choices.change(
519
- show_details,
520
- inputs=breed_choices,
521
- outputs=breed_details
522
- )
523
 
524
  gr.Examples(
525
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
@@ -529,4 +410,4 @@ with gr.Blocks() as iface:
529
  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>')
530
 
531
  if __name__ == "__main__":
532
- iface.launch()
 
301
  return dogs
302
 
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), gr.update(visible=False)
307
 
308
  try:
309
  if isinstance(image, np.ndarray):
 
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), 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), 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"
330
+ f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} 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
+ return explanation, image, gr.update(visible=True, value=f"More about {topk_breeds[0]}"), gr.update(visible=True, value=f"More about {topk_breeds[1]}"), gr.update(visible=True, value=f"More about {topk_breeds[2]}")
335
 
336
  # ๅคš็‹—ๆƒ…ๅขƒ
337
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
338
  explanations = []
 
339
  annotated_image = image.copy()
340
  draw = ImageDraw.Draw(annotated_image)
341
  font = ImageFont.load_default()
 
346
  draw.rectangle(box, outline=color, width=3)
347
  draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
348
 
349
+ breed = topk_breeds[0]
350
+ if top1_prob >= 0.5:
 
 
351
  description = get_dog_description(breed)
352
  formatted_description = format_description(description, breed)
353
  explanations.append(f"Dog {i+1}: {formatted_description}")
354
+ elif top1_prob >= 0.2:
355
+ explanations.append(f"Dog {i+1}: Top 3 possible breeds:\n"
356
+ f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
357
+ f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
358
+ f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)")
359
  else:
360
+ explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
 
 
 
361
 
362
  final_explanation = "\n\n".join(explanations)
363
+ return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
 
 
 
364
 
365
  except Exception as e:
366
+ return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
 
368
  def show_details(choice):
369
  if not choice:
370
  return "Please select a breed to view details."
371
 
372
  try:
373
+ breed = choice.split("More about ")[-1]
374
  description = get_dog_description(breed)
375
  return format_description(description, breed)
376
  except Exception as e:
377
  return f"An error occurred while showing details: {e}"
378
 
379
+
380
  with gr.Blocks() as iface:
381
  gr.HTML("<h1 style='text-align: center;'>๐Ÿถ Dog Breed Classifier ๐Ÿ”</h1>")
382
  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>")
 
386
  output_image = gr.Image(label="Annotated Image")
387
 
388
  output = gr.Markdown(label="Prediction Results")
389
+
390
+ with gr.Row():
391
+ btn1 = gr.Button("View More 1", visible=False)
392
+ btn2 = gr.Button("View More 2", visible=False)
393
+ btn3 = gr.Button("View More 3", visible=False)
394
 
395
  input_image.change(
396
  predict,
397
  inputs=input_image,
398
+ outputs=[output, output_image, btn1, btn2, btn3]
399
  )
400
 
401
+ btn1.click(show_details, inputs=btn1, outputs=output)
402
+ btn2.click(show_details, inputs=btn2, outputs=output)
403
+ btn3.click(show_details, inputs=btn3, outputs=output)
 
 
404
 
405
  gr.Examples(
406
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
 
410
  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>')
411
 
412
  if __name__ == "__main__":
413
+ iface.launch()