DawnC commited on
Commit
4368b39
1 Parent(s): bed9a70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
app.py CHANGED
@@ -251,6 +251,7 @@ def get_akc_breeds_link():
251
  # iface.launch()
252
 
253
  def format_description(description, breed):
 
254
  if isinstance(description, dict):
255
  formatted_description = "\n".join([f"**{key}**: {value}" for key, value in description.items()])
256
  else:
@@ -258,10 +259,9 @@ def format_description(description, breed):
258
 
259
  formatted_description = f"""
260
  **Breed**: {breed}
261
-
262
  {formatted_description}
263
 
264
- **Want to learn more about dog breeds?**
265
  [Visit the AKC dog breeds page]({get_akc_breeds_link()}) and search for {breed} to find detailed information.
266
 
267
  *Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page.
@@ -271,7 +271,9 @@ Please refer to the AKC's terms of use and privacy policy.*
271
  """
272
  return formatted_description
273
 
 
274
  def predict_single_dog(image):
 
275
  image_tensor = preprocess_image(image)
276
  with torch.no_grad():
277
  output = model(image_tensor)
@@ -283,18 +285,21 @@ def predict_single_dog(image):
283
  topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
284
  return top1_prob, topk_breeds, topk_probs_percent
285
 
 
286
  def detect_multiple_dogs(image):
 
287
  results = model_yolo(image)
288
  dogs = []
289
  for result in results:
290
  for box in result.boxes:
291
- if box.cls == 16: # COCO dataset class for dog is 16
292
  xyxy = box.xyxy[0].tolist()
293
  confidence = box.conf.item()
294
  cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
295
  dogs.append((cropped_image, confidence, xyxy))
296
  return dogs
297
 
 
298
  def predict(image):
299
  if image is None:
300
  return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
@@ -303,22 +308,28 @@ def predict(image):
303
  if isinstance(image, np.ndarray):
304
  image = Image.fromarray(image)
305
 
306
- # First, check if there are multiple dogs using YOLO
307
  dogs = detect_multiple_dogs(image)
308
 
309
- if len(dogs) <= 1:
310
- # Single dog or no dog detected, use direct classification
311
  top1_prob, topk_breeds, topk_probs_percent = predict_single_dog(image)
 
 
312
  breed = topk_breeds[0]
313
  description = get_dog_description(breed)
314
  formatted_description = format_description(description, breed)
315
-
316
- if top1_prob < 0.2:
317
- 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)
318
-
319
  return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
320
-
321
- # Multiple dogs detected, process each dog
 
 
 
 
 
 
 
 
322
  explanations = []
323
  visible_buttons = []
324
  annotated_image = image.copy()
@@ -337,7 +348,6 @@ def predict(image):
337
  elif 0.2 <= top1_prob < 0.5:
338
  explanation = f"""
339
  Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds:
340
-
341
  1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
342
  2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
343
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
@@ -353,6 +363,7 @@ Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds
353
  except Exception as e:
354
  return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
355
 
 
356
  def show_details(breed):
357
  breed_name = breed.split("More about ")[-1]
358
  description = get_dog_description(breed_name)
 
251
  # iface.launch()
252
 
253
  def format_description(description, breed):
254
+ # 分別將不同的屬性分開來顯示,保持結果的可讀性
255
  if isinstance(description, dict):
256
  formatted_description = "\n".join([f"**{key}**: {value}" for key, value in description.items()])
257
  else:
 
259
 
260
  formatted_description = f"""
261
  **Breed**: {breed}
 
262
  {formatted_description}
263
 
264
+ **Want to learn more about dog breeds?**
265
  [Visit the AKC dog breeds page]({get_akc_breeds_link()}) and search for {breed} to find detailed information.
266
 
267
  *Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page.
 
271
  """
272
  return formatted_description
273
 
274
+
275
  def predict_single_dog(image):
276
+ # 直接使用模型進行預測,無需通過 YOLO
277
  image_tensor = preprocess_image(image)
278
  with torch.no_grad():
279
  output = model(image_tensor)
 
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
  def detect_multiple_dogs(image):
290
+ # 使用 YOLO 檢測多隻狗
291
  results = model_yolo(image)
292
  dogs = []
293
  for result in results:
294
  for box in result.boxes:
295
+ if box.cls == 16: # COCO 資料集中狗的類別是 16
296
  xyxy = box.xyxy[0].tolist()
297
  confidence = box.conf.item()
298
  cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
299
  dogs.append((cropped_image, confidence, xyxy))
300
  return dogs
301
 
302
+
303
  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
  if isinstance(image, np.ndarray):
309
  image = Image.fromarray(image)
310
 
311
+ # 首先檢查圖片中是否有多隻狗
312
  dogs = detect_multiple_dogs(image)
313
 
314
+ if len(dogs) == 0:
315
+ # 沒有狗或 YOLO 未檢測到狗,使用單狗直接分類
316
  top1_prob, topk_breeds, topk_probs_percent = 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
  breed = topk_breeds[0]
320
  description = get_dog_description(breed)
321
  formatted_description = format_description(description, breed)
 
 
 
 
322
  return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
323
+
324
+ if len(dogs) == 1:
325
+ # 檢測到一隻狗時,直接分類不使用 YOLO 來節省時間
326
+ top1_prob, topk_breeds, topk_probs_percent = predict_single_dog(image)
327
+ breed = topk_breeds[0]
328
+ description = get_dog_description(breed)
329
+ formatted_description = format_description(description, breed)
330
+ return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
331
+
332
+ # 若有多隻狗,則使用 YOLO 的檢測結果來處理
333
  explanations = []
334
  visible_buttons = []
335
  annotated_image = image.copy()
 
348
  elif 0.2 <= top1_prob < 0.5:
349
  explanation = f"""
350
  Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds:
 
351
  1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
352
  2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
353
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
 
363
  except Exception as e:
364
  return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
365
 
366
+
367
  def show_details(breed):
368
  breed_name = breed.split("More about ")[-1]
369
  description = get_dog_description(breed_name)