DawnC commited on
Commit
9edca24
1 Parent(s): 8f303b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -76
app.py CHANGED
@@ -296,81 +296,80 @@ async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.5):
296
  return dogs
297
 
298
 
299
-
300
- # async def predict(image):
301
- # if image is None:
302
- # return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
303
-
304
- # try:
305
- # if isinstance(image, np.ndarray):
306
- # image = Image.fromarray(image)
307
-
308
- # # 嘗試檢測多隻狗
309
- # dogs = await detect_multiple_dogs(image)
310
- # if len(dogs) == 0:
311
- # # 單狗情境
312
- # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
313
- # if top1_prob < 0.2:
314
- # 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)
315
-
316
- # breed = topk_breeds[0]
317
- # description = get_dog_description(breed)
318
-
319
- # if top1_prob >= 0.5:
320
- # formatted_description = format_description(description, breed)
321
- # return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
322
- # else:
323
- # explanation = (
324
- # f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
325
- # f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
326
- # f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
327
- # f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
328
- # "Click on a button to view more information about the breed."
329
- # )
330
- # 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]}")
331
-
332
- # # 多狗情境
333
- # color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
334
- # explanations = []
335
- # annotated_image = image.copy()
336
- # draw = ImageDraw.Draw(annotated_image)
337
- # font = ImageFont.load_default()
338
-
339
- # for i, (cropped_image, _, box) in enumerate(dogs):
340
- # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
341
- # color = color_list[i % len(color_list)]
342
- # draw.rectangle(box, outline=color, width=3)
343
- # draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
344
-
345
- # breed = topk_breeds[0]
346
- # if top1_prob >= 0.5:
347
- # description = get_dog_description(breed)
348
- # formatted_description = format_description(description, breed)
349
- # explanations.append(f"Dog {i+1}: {formatted_description}")
350
- # elif top1_prob >= 0.2:
351
- # explanations.append(f"Dog {i+1}: Top 3 possible breeds:\n"
352
- # f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
353
- # f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
354
- # f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)")
355
- # else:
356
- # explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
357
-
358
- # final_explanation = "\n\n".join(explanations)
359
- # return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
360
-
361
- # except Exception as e:
362
- # return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
363
-
364
- # def show_details(choice):
365
- # if not choice:
366
- # return "Please select a breed to view details."
367
-
368
- # try:
369
- # breed = choice.split("More about ")[-1]
370
- # description = get_dog_description(breed)
371
- # return format_description(description, breed)
372
- # except Exception as e:
373
- # return f"An error occurred while showing details: {e}"
374
 
375
 
376
  # with gr.Blocks() as iface:
@@ -409,7 +408,6 @@ async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.5):
409
  # iface.launch()
410
 
411
 
412
-
413
  # 介面部分
414
  with gr.Blocks() as iface:
415
  gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
 
296
  return dogs
297
 
298
 
299
+ async def predict(image):
300
+ if image is None:
301
+ return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
302
+
303
+ try:
304
+ if isinstance(image, np.ndarray):
305
+ image = Image.fromarray(image)
306
+
307
+ # 嘗試檢測多隻狗
308
+ dogs = await detect_multiple_dogs(image)
309
+ if len(dogs) == 0:
310
+ # 單狗情境
311
+ top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
312
+ if top1_prob < 0.2:
313
+ 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)
314
+
315
+ breed = topk_breeds[0]
316
+ description = get_dog_description(breed)
317
+
318
+ if top1_prob >= 0.5:
319
+ formatted_description = format_description(description, breed)
320
+ return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
321
+ else:
322
+ explanation = (
323
+ f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
324
+ f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
325
+ f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
326
+ f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
327
+ "Click on a button to view more information about the breed."
328
+ )
329
+ 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]}")
330
+
331
+ # 多狗情境
332
+ color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
333
+ explanations = []
334
+ annotated_image = image.copy()
335
+ draw = ImageDraw.Draw(annotated_image)
336
+ font = ImageFont.load_default()
337
+
338
+ for i, (cropped_image, _, box) in enumerate(dogs):
339
+ top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
340
+ color = color_list[i % len(color_list)]
341
+ draw.rectangle(box, outline=color, width=3)
342
+ draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
343
+
344
+ breed = topk_breeds[0]
345
+ if top1_prob >= 0.5:
346
+ description = get_dog_description(breed)
347
+ formatted_description = format_description(description, breed)
348
+ explanations.append(f"Dog {i+1}: {formatted_description}")
349
+ elif top1_prob >= 0.2:
350
+ explanations.append(f"Dog {i+1}: Top 3 possible breeds:\n"
351
+ f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
352
+ f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
353
+ f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)")
354
+ else:
355
+ explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
356
+
357
+ final_explanation = "\n\n".join(explanations)
358
+ return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
359
+
360
+ except Exception as e:
361
+ return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
362
+
363
+ def show_details(choice):
364
+ if not choice:
365
+ return "Please select a breed to view details."
366
+
367
+ try:
368
+ breed = choice.split("More about ")[-1]
369
+ description = get_dog_description(breed)
370
+ return format_description(description, breed)
371
+ except Exception as e:
372
+ return f"An error occurred while showing details: {e}"
 
373
 
374
 
375
  # with gr.Blocks() as iface:
 
408
  # iface.launch()
409
 
410
 
 
411
  # 介面部分
412
  with gr.Blocks() as iface:
413
  gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")