Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -247,116 +247,6 @@ async def process_single_dog(image):
|
|
247 |
return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
|
248 |
|
249 |
|
250 |
-
# async def predict(image):
|
251 |
-
# if image is None:
|
252 |
-
# return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
253 |
-
|
254 |
-
# try:
|
255 |
-
# if isinstance(image, np.ndarray):
|
256 |
-
# image = Image.fromarray(image)
|
257 |
-
|
258 |
-
# dogs = await detect_multiple_dogs(image)
|
259 |
-
|
260 |
-
# color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
261 |
-
# buttons = []
|
262 |
-
# annotated_image = image.copy()
|
263 |
-
# draw = ImageDraw.Draw(annotated_image)
|
264 |
-
# font = ImageFont.load_default()
|
265 |
-
|
266 |
-
# dogs_info = ""
|
267 |
-
# buttons_html = ""
|
268 |
-
|
269 |
-
# for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
270 |
-
# top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
271 |
-
# color = color_list[i % len(color_list)]
|
272 |
-
# draw.rectangle(box, outline=color, width=3)
|
273 |
-
# draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
274 |
-
|
275 |
-
# combined_confidence = detection_confidence * top1_prob
|
276 |
-
# dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
277 |
-
# dogs_info += f'<h2>Dog {i+1}</h2>'
|
278 |
-
|
279 |
-
# if top1_prob >= 0.45:
|
280 |
-
# breed = topk_breeds[0]
|
281 |
-
# description = get_dog_description(breed)
|
282 |
-
# dogs_info += format_description_html(description, breed)
|
283 |
-
|
284 |
-
# elif combined_confidence >= 0.15:
|
285 |
-
# dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
286 |
-
# for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
287 |
-
# #dogs_info += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
|
288 |
-
# prob = float(prob.replace('%', '')) # new
|
289 |
-
# dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>" # new
|
290 |
-
# dogs_info += "</ul>"
|
291 |
-
# buttons_html = '<div class="breed-buttons">' #new
|
292 |
-
# for breed in topk_breeds[:3]:
|
293 |
-
# button_id = f"Dog {i+1}: More about {breed}"
|
294 |
-
# buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
295 |
-
# buttons.append(button_id)
|
296 |
-
# buttons_html += '</div>'
|
297 |
-
# else:
|
298 |
-
# dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
299 |
-
|
300 |
-
# dogs_info += '</div>'
|
301 |
-
|
302 |
-
# dogs_info += buttons_html
|
303 |
-
|
304 |
-
|
305 |
-
# # 避免將按鈕與 dogs_info 同步顯示
|
306 |
-
# if buttons_html:
|
307 |
-
# dogs_info += buttons_html # 確保按鈕獨立並附加到最後輸出
|
308 |
-
|
309 |
-
|
310 |
-
# html_output = f"""
|
311 |
-
# <style>
|
312 |
-
# .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); }}
|
313 |
-
# .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
|
314 |
-
# .breed-buttons {{ margin-top: 10px; }}
|
315 |
-
# .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
316 |
-
# </style>
|
317 |
-
# {dogs_info}
|
318 |
-
|
319 |
-
# """
|
320 |
-
|
321 |
-
# if buttons:
|
322 |
-
# html_output += """
|
323 |
-
# <script>
|
324 |
-
# function handle_button_click(button_id) {
|
325 |
-
# const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
|
326 |
-
# if (radio) {
|
327 |
-
# radio.click();
|
328 |
-
# } else {
|
329 |
-
# console.error("Radio button not found:", button_id);
|
330 |
-
# }
|
331 |
-
# }
|
332 |
-
# </script>
|
333 |
-
# """
|
334 |
-
# initial_state = {
|
335 |
-
# "dogs_info": dogs_info,
|
336 |
-
# "buttons": buttons,
|
337 |
-
# "show_back": True,
|
338 |
-
# "image": annotated_image,
|
339 |
-
# "is_multi_dog": len(dogs) > 1,
|
340 |
-
# "html_output": html_output # 儲存完整的 HTML 輸出
|
341 |
-
# }
|
342 |
-
# return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
343 |
-
# else:
|
344 |
-
# initial_state = {
|
345 |
-
# "dogs_info": dogs_info,
|
346 |
-
# "buttons": [],
|
347 |
-
# "show_back": False,
|
348 |
-
# "image": annotated_image,
|
349 |
-
# "is_multi_dog": len(dogs) > 1,
|
350 |
-
# "html_output": html_output # 儲存完整的 HTML 輸出
|
351 |
-
# }
|
352 |
-
# return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
353 |
-
|
354 |
-
# except Exception as e:
|
355 |
-
# error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
356 |
-
# print(error_msg)
|
357 |
-
# return error_msg, None, gr.update(visible=False, choices=[]), None
|
358 |
-
|
359 |
-
|
360 |
async def predict(image):
|
361 |
if image is None:
|
362 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
@@ -374,55 +264,59 @@ async def predict(image):
|
|
374 |
font = ImageFont.load_default()
|
375 |
|
376 |
dogs_info = ""
|
377 |
-
|
|
|
378 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
379 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
380 |
color = color_list[i % len(color_list)]
|
381 |
draw.rectangle(box, outline=color, width=3)
|
382 |
draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
383 |
-
|
384 |
combined_confidence = detection_confidence * top1_prob
|
385 |
dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
386 |
dogs_info += f'<h2>Dog {i+1}</h2>'
|
387 |
-
|
388 |
if top1_prob >= 0.45:
|
389 |
breed = topk_breeds[0]
|
390 |
description = get_dog_description(breed)
|
391 |
dogs_info += format_description_html(description, breed)
|
392 |
-
|
393 |
elif combined_confidence >= 0.15:
|
394 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
395 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
for breed in topk_breeds[:3]:
|
402 |
button_id = f"Dog {i+1}: More about {breed}"
|
403 |
-
|
404 |
buttons.append(button_id)
|
405 |
-
|
406 |
-
|
407 |
else:
|
408 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
409 |
-
|
410 |
dogs_info += '</div>'
|
411 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
412 |
html_output = f"""
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
{json.dumps({"dogs_info": dogs_info, "buttons": buttons}, indent=2)}
|
423 |
-
</pre>
|
424 |
-
<!-- DEBUG OUTPUT END -->
|
425 |
-
"""
|
426 |
|
427 |
if buttons:
|
428 |
html_output += """
|
@@ -437,23 +331,31 @@ async def predict(image):
|
|
437 |
}
|
438 |
</script>
|
439 |
"""
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
451 |
|
452 |
except Exception as e:
|
453 |
error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
454 |
print(error_msg)
|
455 |
return error_msg, None, gr.update(visible=False, choices=[]), None
|
456 |
-
|
457 |
|
458 |
def show_details_html(choice, previous_output, initial_state):
|
459 |
if not choice:
|
|
|
247 |
return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
|
248 |
|
249 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
async def predict(image):
|
251 |
if image is None:
|
252 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
|
|
264 |
font = ImageFont.load_default()
|
265 |
|
266 |
dogs_info = ""
|
267 |
+
buttons_html = ""
|
268 |
+
|
269 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
270 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
271 |
color = color_list[i % len(color_list)]
|
272 |
draw.rectangle(box, outline=color, width=3)
|
273 |
draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
274 |
+
|
275 |
combined_confidence = detection_confidence * top1_prob
|
276 |
dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
277 |
dogs_info += f'<h2>Dog {i+1}</h2>'
|
278 |
+
|
279 |
if top1_prob >= 0.45:
|
280 |
breed = topk_breeds[0]
|
281 |
description = get_dog_description(breed)
|
282 |
dogs_info += format_description_html(description, breed)
|
283 |
+
|
284 |
elif combined_confidence >= 0.15:
|
285 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
286 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
287 |
+
#dogs_info += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
|
288 |
+
prob = float(prob.replace('%', '')) # new
|
289 |
+
dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>" # new
|
290 |
+
dogs_info += "</ul>"
|
291 |
+
buttons_html = '<div class="breed-buttons">' #new
|
292 |
for breed in topk_breeds[:3]:
|
293 |
button_id = f"Dog {i+1}: More about {breed}"
|
294 |
+
buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
295 |
buttons.append(button_id)
|
296 |
+
buttons_html += '</div>'
|
|
|
297 |
else:
|
298 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
299 |
+
|
300 |
dogs_info += '</div>'
|
301 |
|
302 |
+
dogs_info += buttons_html
|
303 |
+
|
304 |
+
|
305 |
+
# 避免將按鈕與 dogs_info 同步顯示
|
306 |
+
if buttons_html:
|
307 |
+
dogs_info += buttons_html # 確保按鈕獨立並附加到最後輸出
|
308 |
+
|
309 |
+
|
310 |
html_output = f"""
|
311 |
+
<style>
|
312 |
+
.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); }}
|
313 |
+
.dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
|
314 |
+
.breed-buttons {{ margin-top: 10px; }}
|
315 |
+
.breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
316 |
+
</style>
|
317 |
+
{dogs_info}
|
318 |
+
|
319 |
+
"""
|
|
|
|
|
|
|
|
|
320 |
|
321 |
if buttons:
|
322 |
html_output += """
|
|
|
331 |
}
|
332 |
</script>
|
333 |
"""
|
334 |
+
initial_state = {
|
335 |
+
"dogs_info": dogs_info,
|
336 |
+
"buttons": buttons,
|
337 |
+
"show_back": True,
|
338 |
+
"image": annotated_image,
|
339 |
+
"is_multi_dog": len(dogs) > 1,
|
340 |
+
"html_output": html_output # 儲存完整的 HTML 輸出
|
341 |
+
}
|
342 |
+
return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
343 |
+
else:
|
344 |
+
initial_state = {
|
345 |
+
"dogs_info": dogs_info,
|
346 |
+
"buttons": [],
|
347 |
+
"show_back": False,
|
348 |
+
"image": annotated_image,
|
349 |
+
"is_multi_dog": len(dogs) > 1,
|
350 |
+
"html_output": html_output # 儲存完整的 HTML 輸出
|
351 |
+
}
|
352 |
+
return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
353 |
|
354 |
except Exception as e:
|
355 |
error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
356 |
print(error_msg)
|
357 |
return error_msg, None, gr.update(visible=False, choices=[]), None
|
358 |
+
|
359 |
|
360 |
def show_details_html(choice, previous_output, initial_state):
|
361 |
if not choice:
|