Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -339,14 +339,12 @@ async def predict(image):
|
|
339 |
image = Image.fromarray(image)
|
340 |
|
341 |
dogs = await detect_multiple_dogs(image)
|
342 |
-
# 更新為更容易區分的顏色組合
|
343 |
color_list = ['#FF3B30', '#34C759', '#007AFF', '#FF9500', '#5856D6', '#FF2D55', '#5AC8FA', '#FFCC00']
|
344 |
annotated_image = image.copy()
|
345 |
draw = ImageDraw.Draw(annotated_image)
|
346 |
|
347 |
-
# 改用更大的字體,提升可讀性
|
348 |
try:
|
349 |
-
font = ImageFont.truetype("arial.ttf", 24)
|
350 |
except:
|
351 |
font = ImageFont.load_default()
|
352 |
|
@@ -355,16 +353,13 @@ async def predict(image):
|
|
355 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
356 |
color = color_list[i % len(color_list)]
|
357 |
|
358 |
-
#
|
359 |
draw.rectangle(box, outline=color, width=4)
|
360 |
-
|
361 |
-
# 優化標籤背景
|
362 |
label = f"Dog {i+1}"
|
363 |
label_bbox = draw.textbbox((0, 0), label, font=font)
|
364 |
label_width = label_bbox[2] - label_bbox[0]
|
365 |
label_height = label_bbox[3] - label_bbox[1]
|
366 |
|
367 |
-
# 添加標籤背景
|
368 |
label_x = box[0] + 5
|
369 |
label_y = box[1] + 5
|
370 |
draw.rectangle(
|
@@ -378,47 +373,52 @@ async def predict(image):
|
|
378 |
top1_prob, topk_breeds, relative_probs = await predict_single_dog(cropped_image)
|
379 |
combined_confidence = detection_confidence * top1_prob
|
380 |
|
381 |
-
#
|
382 |
-
dogs_info += f''
|
383 |
-
|
|
|
|
|
384 |
<div class="dog-info-header" style="background-color: {color}20;">
|
385 |
<span class="dog-label" style="color: {color};">Dog {i+1}</span>
|
386 |
</div>
|
387 |
-
|
388 |
-
|
389 |
-
if combined_confidence < 0.15:
|
390 |
-
dogs_info += '''
|
391 |
-
<div class="warning-message">
|
392 |
-
<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>
|
393 |
</div>
|
394 |
'''
|
395 |
elif top1_prob >= 0.45:
|
396 |
breed = topk_breeds[0]
|
397 |
description = get_dog_description(breed)
|
398 |
dogs_info += f'''
|
|
|
|
|
|
|
399 |
<div class="breed-info">
|
400 |
-
<div class="confidence-score" style="color: {color};">
|
401 |
-
{breed} (Confidence: {relative_probs[0]})
|
402 |
-
</div>
|
403 |
{format_description_html(description, breed)}
|
404 |
</div>
|
405 |
'''
|
406 |
else:
|
407 |
-
dogs_info +=
|
408 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
409 |
description = get_dog_description(breed)
|
410 |
dogs_info += f'''
|
411 |
<div class="breed-section">
|
412 |
-
<div class="confidence-score" style="color: {color};">
|
413 |
-
|
|
|
414 |
</div>
|
415 |
-
{format_description_html(description, breed)}
|
416 |
</div>
|
417 |
'''
|
|
|
418 |
|
419 |
dogs_info += '</div>'
|
420 |
|
421 |
-
# 更新 CSS 樣式
|
422 |
html_output = f"""
|
423 |
<style>
|
424 |
.dog-info-card {{
|
@@ -451,8 +451,14 @@ async def predict(image):
|
|
451 |
padding: 20px;
|
452 |
}}
|
453 |
|
|
|
|
|
|
|
|
|
|
|
|
|
454 |
.breed-section {{
|
455 |
-
margin:
|
456 |
padding: 16px;
|
457 |
background-color: #f8f8f8;
|
458 |
border-radius: 6px;
|
@@ -464,10 +470,23 @@ async def predict(image):
|
|
464 |
margin-bottom: 12px;
|
465 |
}}
|
466 |
|
|
|
|
|
|
|
|
|
467 |
.warning-message {{
|
468 |
-
padding: 16px 20px;
|
469 |
color: #ff3b30;
|
470 |
font-weight: bold;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
471 |
}}
|
472 |
</style>
|
473 |
{dogs_info}
|
|
|
339 |
image = Image.fromarray(image)
|
340 |
|
341 |
dogs = await detect_multiple_dogs(image)
|
|
|
342 |
color_list = ['#FF3B30', '#34C759', '#007AFF', '#FF9500', '#5856D6', '#FF2D55', '#5AC8FA', '#FFCC00']
|
343 |
annotated_image = image.copy()
|
344 |
draw = ImageDraw.Draw(annotated_image)
|
345 |
|
|
|
346 |
try:
|
347 |
+
font = ImageFont.truetype("arial.ttf", 24)
|
348 |
except:
|
349 |
font = ImageFont.load_default()
|
350 |
|
|
|
353 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
354 |
color = color_list[i % len(color_list)]
|
355 |
|
356 |
+
# 優化圖片上的標記
|
357 |
draw.rectangle(box, outline=color, width=4)
|
|
|
|
|
358 |
label = f"Dog {i+1}"
|
359 |
label_bbox = draw.textbbox((0, 0), label, font=font)
|
360 |
label_width = label_bbox[2] - label_bbox[0]
|
361 |
label_height = label_bbox[3] - label_bbox[1]
|
362 |
|
|
|
363 |
label_x = box[0] + 5
|
364 |
label_y = box[1] + 5
|
365 |
draw.rectangle(
|
|
|
373 |
top1_prob, topk_breeds, relative_probs = await predict_single_dog(cropped_image)
|
374 |
combined_confidence = detection_confidence * top1_prob
|
375 |
|
376 |
+
# 開始資訊卡片
|
377 |
+
dogs_info += f'<div class="dog-info-card" style="border-left: 6px solid {color};">'
|
378 |
+
|
379 |
+
if combined_confidence < 0.15:
|
380 |
+
dogs_info += f'''
|
381 |
<div class="dog-info-header" style="background-color: {color}20;">
|
382 |
<span class="dog-label" style="color: {color};">Dog {i+1}</span>
|
383 |
</div>
|
384 |
+
<div class="breed-info">
|
385 |
+
<p class="warning-message">The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>
|
|
|
|
|
|
|
|
|
386 |
</div>
|
387 |
'''
|
388 |
elif top1_prob >= 0.45:
|
389 |
breed = topk_breeds[0]
|
390 |
description = get_dog_description(breed)
|
391 |
dogs_info += f'''
|
392 |
+
<div class="dog-info-header" style="background-color: {color}20;">
|
393 |
+
<span class="dog-label" style="color: {color};">{breed}</span>
|
394 |
+
</div>
|
395 |
<div class="breed-info">
|
|
|
|
|
|
|
396 |
{format_description_html(description, breed)}
|
397 |
</div>
|
398 |
'''
|
399 |
else:
|
400 |
+
dogs_info += f'''
|
401 |
+
<div class="dog-info-header" style="background-color: {color}20;">
|
402 |
+
<span class="dog-label" style="color: {color};">Dog {i+1}</span>
|
403 |
+
</div>
|
404 |
+
<div class="breed-info">
|
405 |
+
<h3 class="breeds-title">Top 3 possible breeds:</h3>
|
406 |
+
'''
|
407 |
+
|
408 |
+
for j, (breed, prob) in enumerate(zip(topk_breeds, relative_probs)):
|
409 |
description = get_dog_description(breed)
|
410 |
dogs_info += f'''
|
411 |
<div class="breed-section">
|
412 |
+
<div class="confidence-score" style="color: {color};">{breed} (Confidence: {prob})</div>
|
413 |
+
<div class="breed-info-content">
|
414 |
+
{format_description_html(description, breed)}
|
415 |
</div>
|
|
|
416 |
</div>
|
417 |
'''
|
418 |
+
dogs_info += '</div>'
|
419 |
|
420 |
dogs_info += '</div>'
|
421 |
|
|
|
422 |
html_output = f"""
|
423 |
<style>
|
424 |
.dog-info-card {{
|
|
|
451 |
padding: 20px;
|
452 |
}}
|
453 |
|
454 |
+
.breeds-title {{
|
455 |
+
margin: 0 0 16px 0;
|
456 |
+
font-size: 18px;
|
457 |
+
color: #333;
|
458 |
+
}}
|
459 |
+
|
460 |
.breed-section {{
|
461 |
+
margin: 16px 0;
|
462 |
padding: 16px;
|
463 |
background-color: #f8f8f8;
|
464 |
border-radius: 6px;
|
|
|
470 |
margin-bottom: 12px;
|
471 |
}}
|
472 |
|
473 |
+
.breed-info-content {{
|
474 |
+
margin-top: 8px;
|
475 |
+
}}
|
476 |
+
|
477 |
.warning-message {{
|
|
|
478 |
color: #ff3b30;
|
479 |
font-weight: bold;
|
480 |
+
margin: 0;
|
481 |
+
}}
|
482 |
+
|
483 |
+
ul {{
|
484 |
+
padding-left: 0;
|
485 |
+
margin: 0;
|
486 |
+
}}
|
487 |
+
|
488 |
+
li {{
|
489 |
+
margin-bottom: 8px;
|
490 |
}}
|
491 |
</style>
|
492 |
{dogs_info}
|