Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -302,7 +302,7 @@ async def detect_multiple_dogs(image):
|
|
302 |
|
303 |
async def predict(image):
|
304 |
if image is None:
|
305 |
-
return "Please upload an image to start.", None
|
306 |
|
307 |
try:
|
308 |
if isinstance(image, np.ndarray):
|
@@ -311,12 +311,15 @@ async def predict(image):
|
|
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
|
315 |
|
316 |
explanations = []
|
317 |
annotated_image = image.copy()
|
318 |
draw = ImageDraw.Draw(annotated_image)
|
319 |
-
|
|
|
|
|
|
|
320 |
|
321 |
for i, (cropped_image, _, box) in enumerate(dogs, 1):
|
322 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
@@ -324,7 +327,8 @@ async def predict(image):
|
|
324 |
# 繪製更醒目的邊框和標籤
|
325 |
draw.rectangle(box, outline="red", width=5)
|
326 |
label = f"Dog {i}"
|
327 |
-
|
|
|
328 |
draw.rectangle([box[0], box[1], box[0] + label_size[0], box[1] + label_size[1]], fill="red")
|
329 |
draw.text((box[0], box[1]), label, fill="white", font=font)
|
330 |
|
@@ -355,10 +359,10 @@ async def predict(image):
|
|
355 |
description = get_dog_description(breed)
|
356 |
final_explanation += f"\n\n<div id='breed_{i}_{j}' style='display:none;'>\n\n{format_description(description, breed)}\n\n</div>"
|
357 |
|
358 |
-
return final_explanation, annotated_image
|
359 |
|
360 |
except Exception as e:
|
361 |
-
return f"An error occurred: {e}", None
|
362 |
|
363 |
# Gradio 界面設置
|
364 |
with gr.Blocks(css="""
|
|
|
302 |
|
303 |
async def predict(image):
|
304 |
if image is None:
|
305 |
+
return "Please upload an image to start.", None
|
306 |
|
307 |
try:
|
308 |
if isinstance(image, np.ndarray):
|
|
|
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
|
315 |
|
316 |
explanations = []
|
317 |
annotated_image = image.copy()
|
318 |
draw = ImageDraw.Draw(annotated_image)
|
319 |
+
try:
|
320 |
+
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20)
|
321 |
+
except IOError:
|
322 |
+
font = ImageFont.load_default()
|
323 |
|
324 |
for i, (cropped_image, _, box) in enumerate(dogs, 1):
|
325 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
|
|
327 |
# 繪製更醒目的邊框和標籤
|
328 |
draw.rectangle(box, outline="red", width=5)
|
329 |
label = f"Dog {i}"
|
330 |
+
label_bbox = font.getbbox(label)
|
331 |
+
label_size = (label_bbox[2] - label_bbox[0], label_bbox[3] - label_bbox[1])
|
332 |
draw.rectangle([box[0], box[1], box[0] + label_size[0], box[1] + label_size[1]], fill="red")
|
333 |
draw.text((box[0], box[1]), label, fill="white", font=font)
|
334 |
|
|
|
359 |
description = get_dog_description(breed)
|
360 |
final_explanation += f"\n\n<div id='breed_{i}_{j}' style='display:none;'>\n\n{format_description(description, breed)}\n\n</div>"
|
361 |
|
362 |
+
return final_explanation, annotated_image
|
363 |
|
364 |
except Exception as e:
|
365 |
+
return f"An error occurred: {e}", None
|
366 |
|
367 |
# Gradio 界面設置
|
368 |
with gr.Blocks(css="""
|