DawnC commited on
Commit
5702af8
1 Parent(s): 5c61ec2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -251,6 +251,7 @@ def get_akc_breeds_link():
251
 
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,9 +259,12 @@ def format_description(description, breed):
258
 
259
  formatted_description = f"""
260
  **Breed**: {breed}
 
261
  {formatted_description}
 
262
  **Want to learn more about dog breeds?**
263
  [Visit the AKC dog breeds page]({get_akc_breeds_link()}) and search for {breed} to find detailed information.
 
264
  *Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page.
265
  You may need to search for the specific breed on that page.
266
  I am not responsible for the content on external sites.
@@ -270,7 +274,7 @@ Please refer to the AKC's terms of use and privacy policy.*
270
 
271
 
272
  async def predict_single_dog(image):
273
- # 直接使用模型進行預測,無需通過 YOLO
274
  return await asyncio.to_thread(_predict_single_dog, image)
275
 
276
  def _predict_single_dog(image):
@@ -286,6 +290,7 @@ def _predict_single_dog(image):
286
  return top1_prob, topk_breeds, topk_probs_percent
287
 
288
  async def detect_multiple_dogs(image):
 
289
  return await asyncio.to_thread(_detect_multiple_dogs, image)
290
 
291
  def _detect_multiple_dogs(image):
@@ -308,9 +313,11 @@ 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:
 
314
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
315
  if top1_prob < 0.2:
316
  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,23 +326,19 @@ async def predict(image):
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
 
322
- if len(dogs) == 1:
323
- top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
324
- breed = topk_breeds[0]
325
- description = get_dog_description(breed)
326
- formatted_description = format_description(description, breed)
327
- return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
328
-
329
  explanations = []
330
  visible_buttons = []
331
  annotated_image = image.copy()
332
  draw = ImageDraw.Draw(annotated_image)
333
 
 
334
  for i, (cropped_image, _, box) in enumerate(dogs):
335
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
336
 
337
- draw.rectangle(box, outline="red", width=3)
338
- draw.text((box[0], box[1]), f"Dog {i+1}", fill="red")
 
 
339
 
340
  if top1_prob >= 0.5:
341
  breed = topk_breeds[0]
@@ -349,7 +352,8 @@ Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds
349
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
350
  """
351
  explanations.append(explanation)
352
- visible_buttons.extend([f"More about {topk_breeds[0]}", f"More about {topk_breeds[1]}", f"More about {topk_breeds[2]}"])
 
353
  else:
354
  explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
355
 
 
251
 
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
+
263
  {formatted_description}
264
+
265
  **Want to learn more about dog breeds?**
266
  [Visit the AKC dog breeds page]({get_akc_breeds_link()}) and search for {breed} to find detailed information.
267
+
268
  *Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page.
269
  You may need to search for the specific breed on that page.
270
  I am not responsible for the content on external sites.
 
274
 
275
 
276
  async def predict_single_dog(image):
277
+ # 單一品種直接預測,避免 YOLO
278
  return await asyncio.to_thread(_predict_single_dog, image)
279
 
280
  def _predict_single_dog(image):
 
290
  return top1_prob, topk_breeds, topk_probs_percent
291
 
292
  async def detect_multiple_dogs(image):
293
+ # 檢測多隻狗時使用 YOLO
294
  return await asyncio.to_thread(_detect_multiple_dogs, image)
295
 
296
  def _detect_multiple_dogs(image):
 
313
  if isinstance(image, np.ndarray):
314
  image = Image.fromarray(image)
315
 
316
+ # 首先檢查圖片中是否有多隻狗
317
  dogs = await detect_multiple_dogs(image)
318
 
319
  if len(dogs) == 0:
320
+ # 單狗直接分類
321
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
322
  if top1_prob < 0.2:
323
  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)
 
326
  formatted_description = format_description(description, breed)
327
  return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
328
 
 
 
 
 
 
 
 
329
  explanations = []
330
  visible_buttons = []
331
  annotated_image = image.copy()
332
  draw = ImageDraw.Draw(annotated_image)
333
 
334
+ # 多狗情況
335
  for i, (cropped_image, _, box) in enumerate(dogs):
336
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
337
 
338
+ # 更改框的顏色以增加辨識度
339
+ color = ["red", "blue", "green", "yellow"][i % 4]
340
+ draw.rectangle(box, outline=color, width=3)
341
+ draw.text((box[0], box[1]), f"Dog {i+1}", fill=color)
342
 
343
  if top1_prob >= 0.5:
344
  breed = topk_breeds[0]
 
352
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
353
  """
354
  explanations.append(explanation)
355
+ # Top3 選項顯示在該狗的下方
356
+ visible_buttons.extend([f"More about Dog {i+1}: {topk_breeds[0]}", f"More about Dog {i+1}: {topk_breeds[1]}", f"More about Dog {i+1}: {topk_breeds[2]}"])
357
  else:
358
  explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
359