Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -261,110 +261,90 @@ async def predict(image):
|
|
261 |
|
262 |
dogs_info = ""
|
263 |
|
264 |
-
#
|
265 |
-
#
|
266 |
-
#
|
267 |
-
#
|
268 |
-
#
|
269 |
-
#
|
270 |
|
271 |
-
#
|
272 |
-
#
|
273 |
-
#
|
274 |
|
275 |
-
#
|
276 |
-
#
|
277 |
-
#
|
278 |
-
#
|
279 |
|
280 |
-
#
|
281 |
-
#
|
282 |
-
#
|
283 |
-
#
|
284 |
-
#
|
285 |
-
#
|
286 |
|
287 |
-
#
|
288 |
-
#
|
289 |
-
#
|
290 |
-
#
|
291 |
|
292 |
-
#
|
293 |
-
#
|
294 |
|
295 |
-
#
|
296 |
-
|
297 |
-
|
298 |
-
# buttons_html = ""
|
299 |
-
|
300 |
-
# html_output = f"""
|
301 |
-
# <style>
|
302 |
-
# .dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }}
|
303 |
-
# .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
|
304 |
-
# .breed-buttons {{ margin-top: 10px; }}
|
305 |
-
# .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
306 |
-
# </style>
|
307 |
-
# {dogs_info}
|
308 |
-
# """
|
309 |
|
310 |
|
311 |
|
312 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
313 |
-
buttons_html = "" #
|
314 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
315 |
color = color_list[i % len(color_list)]
|
316 |
draw.rectangle(box, outline=color, width=3)
|
317 |
draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
318 |
-
|
319 |
-
# 顯示追蹤訊息來幫助檢查數據
|
320 |
-
print(f"Detection confidence for Dog {i+1}: {detection_confidence}")
|
321 |
-
print(f"Top1 probability for Dog {i+1}: {top1_prob}")
|
322 |
-
|
323 |
combined_confidence = detection_confidence * top1_prob
|
324 |
-
print(f"Combined confidence for Dog {i+1}: {combined_confidence}") # 確認combined confidence
|
325 |
-
|
326 |
dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
327 |
dogs_info += f'<h2>Dog {i+1}</h2>'
|
328 |
-
|
329 |
-
if
|
330 |
breed = topk_breeds[0]
|
331 |
description = get_dog_description(breed)
|
332 |
dogs_info += format_description_html(description, breed)
|
333 |
|
334 |
-
elif
|
335 |
-
|
336 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
337 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
338 |
prob = float(prob.replace('%', ''))
|
339 |
dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
|
340 |
-
|
341 |
-
#
|
342 |
button_id = f"Dog {i+1}: More about {breed}"
|
343 |
-
buttons_html += f'<button
|
344 |
-
|
|
|
|
|
|
|
345 |
|
346 |
-
|
347 |
-
dogs_info += f'<div style="margin-top:10px;">{buttons_html}</div>' # 確保按鈕顯示在該品種描述下
|
348 |
-
|
349 |
-
else: # 條件3:當 conf < 0.15 時,返回 "圖像不清晰或無法辨認"
|
350 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
351 |
|
352 |
dogs_info += '</div>' # 結束當前狗的資訊區塊
|
353 |
-
|
354 |
-
# 最終生成的 HTML 輸出
|
355 |
-
html_output = f"""
|
356 |
-
<style>
|
357 |
-
.dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }}
|
358 |
-
</style>
|
359 |
-
{dogs_info}
|
360 |
-
"""
|
361 |
-
print(f"Final HTML output: {html_output}") # 檢查最終生成的 HTML
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
|
366 |
|
367 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
368 |
|
369 |
if buttons:
|
370 |
html_output += """
|
|
|
261 |
|
262 |
dogs_info = ""
|
263 |
|
264 |
+
# for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
265 |
+
# buttons_html = ""
|
266 |
+
# top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
267 |
+
# color = color_list[i % len(color_list)]
|
268 |
+
# draw.rectangle(box, outline=color,, width=3)
|
269 |
+
# draw.text((box[0] + 5, box[1] + 5) f"Dog {i+1}", fill=color, font=font)
|
270 |
|
271 |
+
# combined_confidence = detection_confidence * top1_prob
|
272 |
+
# dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
273 |
+
# dogs_info += f'<h2>Dog {i+1}</h2>'
|
274 |
|
275 |
+
# if top1_prob >= 0.45:
|
276 |
+
# breed = topk_breeds[0]
|
277 |
+
# description = get_dog_description(breed)
|
278 |
+
# dogs_info += format_description_html(description, breed)
|
279 |
|
280 |
+
# elif combined_confidence >= 0.15:
|
281 |
+
# dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
282 |
+
# for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
283 |
+
# prob = float(prob.replace('%', ''))
|
284 |
+
# dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
|
285 |
+
# dogs_info += "</ul>"
|
286 |
|
287 |
+
# for breed in topk_breeds[:3]:
|
288 |
+
# button_id = f"Dog {i+1}: More about {breed}"
|
289 |
+
# buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
290 |
+
# buttons.append(button_id)
|
291 |
|
292 |
+
# else:
|
293 |
+
# dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
294 |
|
295 |
+
# dogs_info += '</div>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
296 |
|
297 |
|
298 |
|
299 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
300 |
+
buttons_html = "" # 初始化按鈕 HTML
|
301 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
302 |
color = color_list[i % len(color_list)]
|
303 |
draw.rectangle(box, outline=color, width=3)
|
304 |
draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
305 |
+
|
|
|
|
|
|
|
|
|
306 |
combined_confidence = detection_confidence * top1_prob
|
|
|
|
|
307 |
dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
308 |
dogs_info += f'<h2>Dog {i+1}</h2>'
|
309 |
+
|
310 |
+
if top1_prob >= 0.45:
|
311 |
breed = topk_breeds[0]
|
312 |
description = get_dog_description(breed)
|
313 |
dogs_info += format_description_html(description, breed)
|
314 |
|
315 |
+
elif combined_confidence >= 0.15:
|
316 |
+
# 列出前三個可能的品種
|
317 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
318 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
319 |
prob = float(prob.replace('%', ''))
|
320 |
dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
|
321 |
+
|
322 |
+
# 在每個品種之後添加按鈕
|
323 |
button_id = f"Dog {i+1}: More about {breed}"
|
324 |
+
buttons_html += f'<button class="breed-button" style="margin-right: 5px;" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
325 |
+
buttons.append(button_id)
|
326 |
+
|
327 |
+
dogs_info += "</ul>"
|
328 |
+
dogs_info += f'<div class="breed-buttons">{buttons_html}</div>' # 把按鈕放到當前品種描述之後
|
329 |
|
330 |
+
else:
|
|
|
|
|
|
|
331 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
332 |
|
333 |
dogs_info += '</div>' # 結束當前狗的資訊區塊
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
334 |
|
335 |
|
336 |
+
|
337 |
+
buttons_html = ""
|
338 |
+
|
339 |
+
html_output = f"""
|
340 |
+
<style>
|
341 |
+
.dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }}
|
342 |
+
.dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
|
343 |
+
.breed-buttons {{ margin-top: 10px; }}
|
344 |
+
.breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
345 |
+
</style>
|
346 |
+
{dogs_info}
|
347 |
+
"""
|
348 |
|
349 |
if buttons:
|
350 |
html_output += """
|