DawnC commited on
Commit
fb6b017
1 Parent(s): d749ee0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -14
app.py CHANGED
@@ -289,7 +289,7 @@ async def detect_multiple_dogs(image):
289
  img = image.copy()
290
  img.thumbnail((640, 640))
291
 
292
- results = model_yolo(img, conf=0.2) # 降低閾值以檢測更多狗
293
  dogs = []
294
  for result in results:
295
  for box in result.boxes:
@@ -342,7 +342,7 @@ async def predict(image):
342
  async def process_multiple_dogs_result(dogs, image):
343
  annotated_image = image.copy()
344
  draw = ImageDraw.Draw(annotated_image)
345
- font = ImageFont.load_default()
346
 
347
  explanations = []
348
  buttons = []
@@ -352,24 +352,22 @@ async def process_multiple_dogs_result(dogs, image):
352
 
353
  optimized_box = optimize_box(box, image.size)
354
  draw.rectangle(optimized_box, outline="red", width=3)
355
- draw.text((optimized_box[0], optimized_box[1]), f"Dog {i}", fill="yellow", font=font)
356
 
357
- if top1_prob >= 0.5:
358
  breed = topk_breeds[0]
359
  description = get_dog_description(breed)
360
- explanations.append(f"Dog {i}: **{breed}**\n\n{format_description(description, breed, is_multi_dog=True, dog_number=i)}")
361
- elif top1_prob >= 0.2:
362
- explanation = f"""
363
- Dog {i}: The model couldn't confidently identify the breed. Here are the top 3 possible breeds:
364
- 1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
365
- 2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
366
- 3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
367
- """
368
  explanations.append(explanation)
369
- for breed in topk_breeds:
 
370
  buttons.append(f"More about Dog {i}: {breed}")
 
 
371
  else:
372
- explanations.append(f"Dog {i}: The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.")
373
 
374
  final_explanation = "\n\n---\n\n".join(explanations)
375
 
 
289
  img = image.copy()
290
  img.thumbnail((640, 640))
291
 
292
+ results = model_yolo(img, conf=0.1) # 降低閾值以檢測更多狗
293
  dogs = []
294
  for result in results:
295
  for box in result.boxes:
 
342
  async def process_multiple_dogs_result(dogs, image):
343
  annotated_image = image.copy()
344
  draw = ImageDraw.Draw(annotated_image)
345
+ font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20)
346
 
347
  explanations = []
348
  buttons = []
 
352
 
353
  optimized_box = optimize_box(box, image.size)
354
  draw.rectangle(optimized_box, outline="red", width=3)
355
+ draw.text((optimized_box[0], optimized_box[1]), f"Dog {i}", fill="yellow", font=font, stroke_width=2, stroke_fill="black")
356
 
357
+ if top1_prob >= 0.2:
358
  breed = topk_breeds[0]
359
  description = get_dog_description(breed)
360
+ explanation = f"Dog {i}: **{breed}**\n\n"
361
+ explanation += "\n".join([f"**{key}**: {value}" for key, value in description.items() if key != "Breed"])
362
+ explanation += f"\n\n**Want to learn more about dog breeds?** [Visit the AKC dog breeds page]({get_akc_breeds_link()}) and search for {breed} to find detailed information."
 
 
 
 
 
363
  explanations.append(explanation)
364
+
365
+ if top1_prob < 0.5:
366
  buttons.append(f"More about Dog {i}: {breed}")
367
+ buttons.append(f"More about Dog {i}: {topk_breeds[1]}")
368
+ buttons.append(f"More about Dog {i}: {topk_breeds[2]}")
369
  else:
370
+ explanations.append(f"Dog {i}: The image is unclear or the breed is not in the dataset. Please upload a clearer image of this dog.")
371
 
372
  final_explanation = "\n\n---\n\n".join(explanations)
373