Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -300,7 +300,7 @@ async def detect_multiple_dogs(image):
|
|
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 |
|
@@ -308,7 +308,7 @@ def predict(image):
|
|
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)
|
@@ -320,7 +320,7 @@ def predict(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)
|
@@ -328,11 +328,10 @@ def predict(image):
|
|
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]})
|
@@ -351,7 +350,7 @@ Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
|
|
351 |
except Exception as e:
|
352 |
return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
353 |
|
354 |
-
def show_details(choice):
|
355 |
if not choice:
|
356 |
return "Please select a breed to view details."
|
357 |
|
@@ -362,7 +361,7 @@ def show_details(choice):
|
|
362 |
except Exception as e:
|
363 |
return f"An error occurred while showing details: {e}"
|
364 |
|
365 |
-
# Gradio 界面設置
|
366 |
with gr.Blocks(css="""
|
367 |
.container { max-width: 900px; margin: auto; padding: 20px; }
|
368 |
.gr-box { border-radius: 15px; }
|
@@ -390,8 +389,8 @@ with gr.Blocks(css="""
|
|
390 |
|
391 |
breed_buttons.select(
|
392 |
show_details,
|
393 |
-
inputs=
|
394 |
-
outputs=
|
395 |
)
|
396 |
|
397 |
gr.Examples(
|
|
|
300 |
print(f"Error in detect_multiple_dogs: {e}")
|
301 |
return []
|
302 |
|
303 |
+
async 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 |
|
|
|
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 |
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)
|
|
|
320 |
font = ImageFont.load_default()
|
321 |
|
322 |
for i, (cropped_image, _, box) in enumerate(dogs, 1):
|
323 |
+
top1_prob, topk_breeds, topk_probs_percent = await 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)
|
|
|
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, is_multi_dog=True, dog_number=i)}")
|
332 |
else:
|
333 |
explanation = f"""
|
334 |
Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
|
|
|
335 |
1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
|
336 |
2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
|
337 |
3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
|
|
|
350 |
except Exception as e:
|
351 |
return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
352 |
|
353 |
+
async def show_details(choice):
|
354 |
if not choice:
|
355 |
return "Please select a breed to view details."
|
356 |
|
|
|
361 |
except Exception as e:
|
362 |
return f"An error occurred while showing details: {e}"
|
363 |
|
364 |
+
# 修改 Gradio 界面設置
|
365 |
with gr.Blocks(css="""
|
366 |
.container { max-width: 900px; margin: auto; padding: 20px; }
|
367 |
.gr-box { border-radius: 15px; }
|
|
|
389 |
|
390 |
breed_buttons.select(
|
391 |
show_details,
|
392 |
+
inputs=breed_buttons,
|
393 |
+
outputs=breed_details
|
394 |
)
|
395 |
|
396 |
gr.Examples(
|