Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -250,22 +250,25 @@ def get_akc_breeds_link():
|
|
250 |
# if __name__ == "__main__":
|
251 |
# iface.launch()
|
252 |
|
253 |
-
|
254 |
def format_description(description, breed):
|
255 |
if isinstance(description, dict):
|
256 |
formatted_description = "\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
257 |
else:
|
258 |
formatted_description = description
|
259 |
|
260 |
-
|
261 |
-
|
|
|
|
|
262 |
|
263 |
-
|
264 |
-
|
265 |
-
"I am not responsible for the content on external sites. "
|
266 |
-
"Please refer to the AKC's terms of use and privacy policy.*")
|
267 |
-
formatted_description += disclaimer
|
268 |
|
|
|
|
|
|
|
|
|
|
|
269 |
return formatted_description
|
270 |
|
271 |
def predict_single_dog(image):
|
@@ -280,7 +283,7 @@ def predict_single_dog(image):
|
|
280 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
281 |
return top1_prob, topk_breeds, topk_probs_percent
|
282 |
|
283 |
-
def
|
284 |
results = model_yolo(image)
|
285 |
dogs = []
|
286 |
for result in results:
|
@@ -292,19 +295,6 @@ def detect_dogs(image):
|
|
292 |
dogs.append((cropped_image, confidence, xyxy))
|
293 |
return dogs
|
294 |
|
295 |
-
|
296 |
-
def predict_breed(cropped_image):
|
297 |
-
image_tensor = preprocess_image(cropped_image)
|
298 |
-
with torch.no_grad():
|
299 |
-
output = model(image_tensor)
|
300 |
-
logits = output[0] if isinstance(output, tuple) else output
|
301 |
-
probabilities = F.softmax(logits, dim=1)
|
302 |
-
topk_probs, topk_indices = torch.topk(probabilities, k=3)
|
303 |
-
top1_prob = topk_probs[0][0].item()
|
304 |
-
topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
|
305 |
-
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
306 |
-
return top1_prob, topk_breeds, topk_probs_percent
|
307 |
-
|
308 |
def predict(image):
|
309 |
if image is None:
|
310 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
@@ -313,21 +303,22 @@ def predict(image):
|
|
313 |
if isinstance(image, np.ndarray):
|
314 |
image = Image.fromarray(image)
|
315 |
|
316 |
-
# First,
|
317 |
-
|
318 |
-
|
319 |
-
if
|
320 |
-
#
|
|
|
321 |
breed = topk_breeds[0]
|
322 |
description = get_dog_description(breed)
|
323 |
formatted_description = format_description(description, breed)
|
|
|
|
|
|
|
|
|
324 |
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
325 |
|
326 |
-
#
|
327 |
-
dogs = detect_dogs(image)
|
328 |
-
if len(dogs) == 0:
|
329 |
-
return "No dogs detected or the image is unclear. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
330 |
-
|
331 |
explanations = []
|
332 |
visible_buttons = []
|
333 |
annotated_image = image.copy()
|
@@ -342,14 +333,15 @@ def predict(image):
|
|
342 |
if top1_prob >= 0.5:
|
343 |
breed = topk_breeds[0]
|
344 |
description = get_dog_description(breed)
|
345 |
-
explanations.append(f"Dog {i+1}:\n
|
346 |
elif 0.2 <= top1_prob < 0.5:
|
347 |
-
explanation =
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
|
|
353 |
explanations.append(explanation)
|
354 |
visible_buttons.extend([f"More about {topk_breeds[0]}", f"More about {topk_breeds[1]}", f"More about {topk_breeds[2]}"])
|
355 |
else:
|
@@ -361,12 +353,12 @@ def predict(image):
|
|
361 |
except Exception as e:
|
362 |
return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
363 |
|
364 |
-
|
365 |
def show_details(breed):
|
366 |
breed_name = breed.split("More about ")[-1]
|
367 |
description = get_dog_description(breed_name)
|
368 |
return format_description(description, breed_name)
|
369 |
|
|
|
370 |
with gr.Blocks(css="""
|
371 |
.container {
|
372 |
max-width: 900px;
|
|
|
250 |
# if __name__ == "__main__":
|
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:
|
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?**
|
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.
|
268 |
+
You may need to search for the specific breed on that page.
|
269 |
+
I am not responsible for the content on external sites.
|
270 |
+
Please refer to the AKC's terms of use and privacy policy.*
|
271 |
+
"""
|
272 |
return formatted_description
|
273 |
|
274 |
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:
|
|
|
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 |
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()
|
|
|
333 |
if top1_prob >= 0.5:
|
334 |
breed = topk_breeds[0]
|
335 |
description = get_dog_description(breed)
|
336 |
+
explanations.append(f"Dog {i+1}:\n{format_description(description, breed)}")
|
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]})
|
344 |
+
"""
|
345 |
explanations.append(explanation)
|
346 |
visible_buttons.extend([f"More about {topk_breeds[0]}", f"More about {topk_breeds[1]}", f"More about {topk_breeds[2]}"])
|
347 |
else:
|
|
|
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)
|
359 |
return format_description(description, breed_name)
|
360 |
|
361 |
+
|
362 |
with gr.Blocks(css="""
|
363 |
.container {
|
364 |
max-width: 900px;
|