DawnC commited on
Commit
a46c623
1 Parent(s): a312d58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -36
app.py CHANGED
@@ -250,15 +250,16 @@ def get_akc_breeds_link():
250
  # if __name__ == "__main__":
251
  # iface.launch()
252
 
253
- def format_description(description, breed):
254
  if isinstance(description, dict):
255
- formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items()])
256
  else:
257
  formatted_description = description
258
 
 
 
259
  formatted_description = f"""
260
- **Breed**: {breed}
261
-
262
  {formatted_description}
263
 
264
  **Want to learn more about dog breeds?**
@@ -271,30 +272,6 @@ 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)
278
- logits = output[0] if isinstance(output, tuple) else output
279
- probabilities = F.softmax(logits, dim=1)
280
- topk_probs, topk_indices = torch.topk(probabilities, k=3)
281
- top1_prob = topk_probs[0][0].item()
282
- topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
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)
@@ -318,7 +295,7 @@ def predict(image):
318
  return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
319
  elif 0.2 <= top1_prob < 0.5:
320
  explanation = f"""
321
- Detected with moderate confidence. Here are the top 3 possible breeds:
322
 
323
  1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
324
  2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
@@ -336,28 +313,28 @@ Click on a button below to view more information about each breed.
336
  annotated_image = image.copy()
337
  draw = ImageDraw.Draw(annotated_image)
338
 
339
- for i, (cropped_image, _, box) in enumerate(dogs):
340
  top1_prob, topk_breeds, topk_probs_percent = predict_single_dog(cropped_image)
341
 
342
  draw.rectangle(box, outline="red", width=3)
343
- draw.text((box[0], box[1]), f"Dog {i+1}", fill="red")
344
 
345
  if top1_prob >= 0.5:
346
  breed = topk_breeds[0]
347
  description = get_dog_description(breed)
348
- explanations.append(f"Dog {i+1}:\n{format_description(description, breed)}")
349
  elif 0.2 <= top1_prob < 0.5:
350
  explanation = f"""
351
- Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds:
352
 
353
  1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
354
  2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
355
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
356
  """
357
  explanations.append(explanation)
358
- visible_buttons.extend([f"More about {topk_breeds[0]}", f"More about {topk_breeds[1]}", f"More about {topk_breeds[2]}"])
359
  else:
360
- explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
361
 
362
  final_explanation = "\n\n".join(explanations)
363
  return final_explanation, annotated_image, gr.update(visible=len(visible_buttons) >= 1, value=visible_buttons[0] if visible_buttons else ""), gr.update(visible=True, value=visible_buttons[1] if len(visible_buttons) >= 2 else ""), gr.update(visible=True, value=visible_buttons[2] if len(visible_buttons) >= 3 else "")
@@ -366,7 +343,7 @@ Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds
366
  return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
367
 
368
  def show_details(breed):
369
- breed_name = breed.split("More about ")[-1]
370
  description = get_dog_description(breed_name)
371
  return format_description(description, breed_name)
372
 
 
250
  # if __name__ == "__main__":
251
  # iface.launch()
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"])
256
  else:
257
  formatted_description = description
258
 
259
+ header = f"**Dog {dog_number}: {breed}**\n\n" if is_multi_dog else f"**Breed: {breed}**\n\n"
260
+
261
  formatted_description = f"""
262
+ {header}
 
263
  {formatted_description}
264
 
265
  **Want to learn more about dog breeds?**
 
272
  """
273
  return formatted_description
274
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  def predict(image):
276
  if image is None:
277
  return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
295
  return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
296
  elif 0.2 <= top1_prob < 0.5:
297
  explanation = f"""
298
+ **Detected with moderate confidence. Here are the top 3 possible breeds:**
299
 
300
  1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
301
  2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
 
313
  annotated_image = image.copy()
314
  draw = ImageDraw.Draw(annotated_image)
315
 
316
+ for i, (cropped_image, _, box) in enumerate(dogs, 1):
317
  top1_prob, topk_breeds, topk_probs_percent = predict_single_dog(cropped_image)
318
 
319
  draw.rectangle(box, outline="red", width=3)
320
+ draw.text((box[0], box[1]), f"Dog {i}", fill="red", font=ImageFont.truetype("arial.ttf", 20))
321
 
322
  if top1_prob >= 0.5:
323
  breed = topk_breeds[0]
324
  description = get_dog_description(breed)
325
+ explanations.append(format_description(description, breed, is_multi_dog=True, dog_number=i))
326
  elif 0.2 <= top1_prob < 0.5:
327
  explanation = f"""
328
+ **Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:**
329
 
330
  1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
331
  2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
332
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
333
  """
334
  explanations.append(explanation)
335
+ visible_buttons.extend([f"More about Dog {i}: {topk_breeds[0]}", f"More about Dog {i}: {topk_breeds[1]}", f"More about Dog {i}: {topk_breeds[2]}"])
336
  else:
337
+ explanations.append(f"**Dog {i}**: The image is unclear or the breed is not in the dataset.")
338
 
339
  final_explanation = "\n\n".join(explanations)
340
  return final_explanation, annotated_image, gr.update(visible=len(visible_buttons) >= 1, value=visible_buttons[0] if visible_buttons else ""), gr.update(visible=True, value=visible_buttons[1] if len(visible_buttons) >= 2 else ""), gr.update(visible=True, value=visible_buttons[2] if len(visible_buttons) >= 3 else "")
 
343
  return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
344
 
345
  def show_details(breed):
346
+ breed_name = breed.split("More about ")[-1].split(": ")[-1] # Handle both single and multi-dog cases
347
  description = get_dog_description(breed_name)
348
  return format_description(description, breed_name)
349