DawnC commited on
Commit
3fa059c
1 Parent(s): e43d9f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -249,6 +249,7 @@ def get_akc_breeds_link():
249
  # if __name__ == "__main__":
250
  # iface.launch()
251
 
 
252
  def format_description(description, breed, is_multi_dog=False, dog_number=None):
253
  if isinstance(description, dict):
254
  formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items() if key != "Breed"])
@@ -271,7 +272,7 @@ Please refer to the AKC's terms of use and privacy policy.*
271
  """
272
  return formatted_description
273
 
274
-
275
  async def predict_single_dog(image):
276
  image_tensor = preprocess_image(image)
277
  with torch.no_grad():
@@ -284,6 +285,7 @@ async def predict_single_dog(image):
284
  topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
285
  return top1_prob, topk_breeds, topk_probs_percent
286
 
 
287
  async def detect_multiple_dogs(image):
288
  try:
289
  results = model_yolo(image)
@@ -300,6 +302,7 @@ async def detect_multiple_dogs(image):
300
  print(f"Error in detect_multiple_dogs: {e}")
301
  return []
302
 
 
303
  async def predict(image):
304
  if image is None:
305
  return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
@@ -308,6 +311,7 @@ async def predict(image):
308
  if isinstance(image, np.ndarray):
309
  image = Image.fromarray(image)
310
 
 
311
  dogs = await detect_multiple_dogs(image)
312
 
313
  if len(dogs) == 0:
@@ -322,8 +326,9 @@ async def predict(image):
322
  for i, (cropped_image, _, box) in enumerate(dogs, 1):
323
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
324
 
 
325
  draw.rectangle(box, outline="red", width=3)
326
- draw.text((box[0], box[1]), f"Dog {i}", fill="red", font=font)
327
 
328
  if top1_prob >= 0.5:
329
  breed = topk_breeds[0]
@@ -350,6 +355,7 @@ Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
350
  except Exception as e:
351
  return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
352
 
 
353
  async def show_details(choice):
354
  if not choice:
355
  return "Please select a breed to view details."
@@ -361,7 +367,7 @@ async def show_details(choice):
361
  except Exception as e:
362
  return f"An error occurred while showing details: {e}"
363
 
364
- # 修改 Gradio 界面設置
365
  with gr.Blocks(css="""
366
  .container { max-width: 900px; margin: auto; padding: 20px; }
367
  .gr-box { border-radius: 15px; }
@@ -398,7 +404,7 @@ with gr.Blocks(css="""
398
  inputs=input_image
399
  )
400
 
401
- 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%20Breed_Classifier">Dog Breed Classifier</a>')
402
 
403
  if __name__ == "__main__":
404
  iface.launch()
 
249
  # if __name__ == "__main__":
250
  # iface.launch()
251
 
252
+ # 格式化狗的品種描述函數
253
  def format_description(description, breed, is_multi_dog=False, dog_number=None):
254
  if isinstance(description, dict):
255
  formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items() if key != "Breed"])
 
272
  """
273
  return formatted_description
274
 
275
+ # 預測單隻狗的品種
276
  async def predict_single_dog(image):
277
  image_tensor = preprocess_image(image)
278
  with torch.no_grad():
 
285
  topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
286
  return top1_prob, topk_breeds, topk_probs_percent
287
 
288
+ # 偵測多隻狗的函數
289
  async def detect_multiple_dogs(image):
290
  try:
291
  results = model_yolo(image)
 
302
  print(f"Error in detect_multiple_dogs: {e}")
303
  return []
304
 
305
+ # 主預測函數
306
  async def predict(image):
307
  if image is None:
308
  return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
311
  if isinstance(image, np.ndarray):
312
  image = Image.fromarray(image)
313
 
314
+ # YOLO 偵測多隻狗
315
  dogs = await detect_multiple_dogs(image)
316
 
317
  if len(dogs) == 0:
 
326
  for i, (cropped_image, _, box) in enumerate(dogs, 1):
327
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
328
 
329
+ # 標註框框標籤更醒目
330
  draw.rectangle(box, outline="red", width=3)
331
+ draw.text((box[0], box[1]), f"Dog {i}", fill="yellow", font=font)
332
 
333
  if top1_prob >= 0.5:
334
  breed = topk_breeds[0]
 
355
  except Exception as e:
356
  return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
357
 
358
+ # 顯示選擇的品種詳細信息
359
  async def show_details(choice):
360
  if not choice:
361
  return "Please select a breed to view details."
 
367
  except Exception as e:
368
  return f"An error occurred while showing details: {e}"
369
 
370
+ # Gradio 介面設置
371
  with gr.Blocks(css="""
372
  .container { max-width: 900px; margin: auto; padding: 20px; }
373
  .gr-box { border-radius: 15px; }
 
404
  inputs=input_image
405
  )
406
 
407
+ 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>')
408
 
409
  if __name__ == "__main__":
410
  iface.launch()