Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -300,68 +300,60 @@ async def detect_multiple_dogs(image):
|
|
300 |
print(f"Error in detect_multiple_dogs: {e}")
|
301 |
return []
|
302 |
|
303 |
-
|
304 |
if image is None:
|
305 |
-
return "Please upload an image to start.", None,
|
306 |
|
307 |
try:
|
308 |
if isinstance(image, np.ndarray):
|
309 |
image = Image.fromarray(image)
|
310 |
|
311 |
-
dogs =
|
312 |
|
313 |
if len(dogs) == 0:
|
314 |
-
return "No dogs detected. Please upload a clear image of a dog.", None,
|
315 |
|
316 |
explanations = []
|
317 |
buttons = []
|
318 |
-
button_details = []
|
319 |
annotated_image = image.copy()
|
320 |
draw = ImageDraw.Draw(annotated_image)
|
321 |
-
|
322 |
-
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20)
|
323 |
-
except IOError:
|
324 |
-
font = ImageFont.load_default()
|
325 |
|
326 |
for i, (cropped_image, _, box) in enumerate(dogs, 1):
|
327 |
-
top1_prob, topk_breeds, topk_probs_percent =
|
328 |
|
329 |
-
|
330 |
-
draw.
|
331 |
-
label = f"Dog {i}"
|
332 |
-
label_bbox = font.getbbox(label)
|
333 |
-
label_size = (label_bbox[2] - label_bbox[0], label_bbox[3] - label_bbox[1])
|
334 |
-
draw.rectangle([box[0], box[1], box[0] + label_size[0], box[1] + label_size[1]], fill="red")
|
335 |
-
draw.text((box[0], box[1]), label, fill="white", font=font)
|
336 |
|
337 |
if top1_prob >= 0.5:
|
338 |
breed = topk_breeds[0]
|
339 |
description = get_dog_description(breed)
|
340 |
-
|
341 |
-
explanations.append(explanation)
|
342 |
else:
|
343 |
explanation = f"""
|
344 |
-
|
345 |
|
346 |
1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
|
347 |
2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
|
348 |
3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
|
349 |
"""
|
350 |
explanations.append(explanation)
|
351 |
-
for
|
352 |
-
|
353 |
-
buttons.append(button_text)
|
354 |
-
description = get_dog_description(breed)
|
355 |
-
button_details.append(format_description(description, breed))
|
356 |
|
357 |
final_explanation = "\n\n---\n\n".join(explanations)
|
358 |
-
return final_explanation, annotated_image, buttons,
|
359 |
|
360 |
except Exception as e:
|
361 |
-
return f"An error occurred: {e}", None,
|
362 |
|
363 |
-
def
|
364 |
-
|
|
|
|
|
|
|
|
|
|
|
365 |
|
366 |
# Gradio 界面設置
|
367 |
with gr.Blocks(css="""
|
|
|
300 |
print(f"Error in detect_multiple_dogs: {e}")
|
301 |
return []
|
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)
|
306 |
|
307 |
try:
|
308 |
if isinstance(image, np.ndarray):
|
309 |
image = Image.fromarray(image)
|
310 |
|
311 |
+
dogs = detect_multiple_dogs(image)
|
312 |
|
313 |
if len(dogs) == 0:
|
314 |
+
return "No dogs detected. Please upload a clear image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
315 |
|
316 |
explanations = []
|
317 |
buttons = []
|
|
|
318 |
annotated_image = image.copy()
|
319 |
draw = ImageDraw.Draw(annotated_image)
|
320 |
+
font = ImageFont.load_default()
|
|
|
|
|
|
|
321 |
|
322 |
for i, (cropped_image, _, box) in enumerate(dogs, 1):
|
323 |
+
top1_prob, topk_breeds, topk_probs_percent = predict_single_dog(cropped_image)
|
324 |
|
325 |
+
draw.rectangle(box, outline="red", width=3)
|
326 |
+
draw.text((box[0], box[1]), f"Dog {i}", fill="red", font=font)
|
|
|
|
|
|
|
|
|
|
|
327 |
|
328 |
if top1_prob >= 0.5:
|
329 |
breed = topk_breeds[0]
|
330 |
description = get_dog_description(breed)
|
331 |
+
explanations.append(f"Dog {i}: **{breed}**\n\n{format_description(description, breed)}")
|
|
|
332 |
else:
|
333 |
explanation = f"""
|
334 |
+
Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
|
335 |
|
336 |
1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
|
337 |
2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
|
338 |
3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
|
339 |
"""
|
340 |
explanations.append(explanation)
|
341 |
+
for breed in topk_breeds:
|
342 |
+
buttons.append(f"More about Dog {i}: {breed}")
|
|
|
|
|
|
|
343 |
|
344 |
final_explanation = "\n\n---\n\n".join(explanations)
|
345 |
+
return final_explanation, annotated_image, gr.update(visible=True, choices=buttons), gr.update(visible=False), gr.update(visible=False)
|
346 |
|
347 |
except Exception as e:
|
348 |
+
return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
349 |
|
350 |
+
def show_details(choice):
|
351 |
+
try:
|
352 |
+
_, breed = choice.split(": ", 1)
|
353 |
+
description = get_dog_description(breed)
|
354 |
+
return format_description(description, breed)
|
355 |
+
except Exception as e:
|
356 |
+
return f"An error occurred while showing details: {e}"
|
357 |
|
358 |
# Gradio 界面設置
|
359 |
with gr.Blocks(css="""
|