Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -243,110 +243,6 @@ async def process_single_dog(image):
|
|
243 |
return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
|
244 |
|
245 |
|
246 |
-
# async def predict(image):
|
247 |
-
# if image is None:
|
248 |
-
# return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
249 |
-
|
250 |
-
# try:
|
251 |
-
# if isinstance(image, np.ndarray):
|
252 |
-
# image = Image.fromarray(image)
|
253 |
-
|
254 |
-
# dogs = await detect_multiple_dogs(image)
|
255 |
-
|
256 |
-
# color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
257 |
-
# buttons = []
|
258 |
-
# annotated_image = image.copy()
|
259 |
-
# draw = ImageDraw.Draw(annotated_image)
|
260 |
-
# font = ImageFont.load_default()
|
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 |
-
# buttons_html = ""
|
298 |
-
|
299 |
-
# html_output = f"""
|
300 |
-
# <style>
|
301 |
-
# .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); }}
|
302 |
-
# .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
|
303 |
-
# .breed-buttons {{ margin-top: 10px; }}
|
304 |
-
# .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
305 |
-
# </style>
|
306 |
-
# {dogs_info}
|
307 |
-
# """
|
308 |
-
|
309 |
-
|
310 |
-
# if buttons:
|
311 |
-
# html_output += """
|
312 |
-
# <script>
|
313 |
-
# function handle_button_click(button_id) {
|
314 |
-
# const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
|
315 |
-
# if (radio) {
|
316 |
-
# radio.click();
|
317 |
-
# } else {
|
318 |
-
# console.error("Radio button not found:", button_id);
|
319 |
-
# }
|
320 |
-
# }
|
321 |
-
# </script>
|
322 |
-
# """
|
323 |
-
# initial_state = {
|
324 |
-
# "dogs_info": dogs_info,
|
325 |
-
# "buttons": buttons,
|
326 |
-
# "show_back": True,
|
327 |
-
# "image": annotated_image,
|
328 |
-
# "is_multi_dog": len(dogs) > 1,
|
329 |
-
# "html_output": html_output
|
330 |
-
# }
|
331 |
-
# return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
332 |
-
# else:
|
333 |
-
# initial_state = {
|
334 |
-
# "dogs_info": dogs_info,
|
335 |
-
# "buttons": [],
|
336 |
-
# "show_back": False,
|
337 |
-
# "image": annotated_image,
|
338 |
-
# "is_multi_dog": len(dogs) > 1,
|
339 |
-
# "html_output": html_output
|
340 |
-
# }
|
341 |
-
# return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
342 |
-
|
343 |
-
|
344 |
-
# except Exception as e:
|
345 |
-
# error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
346 |
-
# print(error_msg)
|
347 |
-
# return error_msg, None, gr.update(visible=False, choices=[]), None
|
348 |
-
|
349 |
-
|
350 |
async def predict(image):
|
351 |
if image is None:
|
352 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
@@ -363,49 +259,43 @@ async def predict(image):
|
|
363 |
draw = ImageDraw.Draw(annotated_image)
|
364 |
font = ImageFont.load_default()
|
365 |
|
366 |
-
dogs_info =
|
367 |
-
|
368 |
-
|
369 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
|
|
370 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
371 |
color = color_list[i % len(color_list)]
|
372 |
draw.rectangle(box, outline=color, width=3)
|
373 |
draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
374 |
|
375 |
combined_confidence = detection_confidence * top1_prob
|
376 |
-
|
377 |
-
|
378 |
-
f'<h2>Dog {i+1}</h2>'
|
379 |
-
]
|
380 |
|
381 |
if top1_prob >= 0.45:
|
382 |
breed = topk_breeds[0]
|
383 |
description = get_dog_description(breed)
|
384 |
-
|
385 |
|
386 |
elif combined_confidence >= 0.15:
|
387 |
-
|
388 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
389 |
prob = float(prob.replace('%', ''))
|
390 |
-
|
391 |
-
|
392 |
|
393 |
-
# 將按鈕直接添加到每個狗狗的資訊中
|
394 |
-
dog_info.append('<div class="breed-buttons">')
|
395 |
for breed in topk_breeds[:3]:
|
396 |
button_id = f"Dog {i+1}: More about {breed}"
|
397 |
-
|
398 |
-
|
399 |
-
dog_info.append('</div>')
|
400 |
|
401 |
else:
|
402 |
-
|
403 |
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
html_output = f"""
|
410 |
<style>
|
411 |
.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); }}
|
@@ -413,10 +303,11 @@ async def predict(image):
|
|
413 |
.breed-buttons {{ margin-top: 10px; }}
|
414 |
.breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
415 |
</style>
|
416 |
-
{
|
417 |
"""
|
418 |
|
419 |
-
|
|
|
420 |
html_output += """
|
421 |
<script>
|
422 |
function handle_button_click(button_id) {
|
@@ -430,42 +321,31 @@ async def predict(image):
|
|
430 |
</script>
|
431 |
"""
|
432 |
initial_state = {
|
433 |
-
"dogs_info":
|
434 |
-
"buttons":
|
435 |
"show_back": True,
|
436 |
"image": annotated_image,
|
437 |
"is_multi_dog": len(dogs) > 1,
|
438 |
-
"html_output": html_output
|
439 |
}
|
440 |
-
return html_output, annotated_image, gr.update(visible=True, choices=
|
441 |
-
|
442 |
-
|
443 |
-
"dogs_info": ''.join(dogs_info),
|
444 |
-
"buttons": [],
|
445 |
-
"show_back": False,
|
446 |
-
"image": annotated_image,
|
447 |
-
"is_multi_dog": len(dogs) > 1,
|
448 |
-
"html_output": html_output
|
449 |
-
}
|
450 |
-
return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
451 |
-
|
452 |
-
else:
|
453 |
-
initial_state = {
|
454 |
"dogs_info": dogs_info,
|
455 |
"buttons": [],
|
456 |
"show_back": False,
|
457 |
"image": annotated_image,
|
458 |
"is_multi_dog": len(dogs) > 1,
|
459 |
-
"html_output": html_output
|
460 |
-
|
461 |
-
|
|
|
462 |
|
463 |
except Exception as e:
|
464 |
error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
465 |
print(error_msg)
|
466 |
return error_msg, None, gr.update(visible=False, choices=[]), None
|
467 |
|
468 |
-
|
469 |
|
470 |
def show_details_html(choice, previous_output, initial_state):
|
471 |
if not choice:
|
|
|
243 |
return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
|
244 |
|
245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
246 |
async def predict(image):
|
247 |
if image is None:
|
248 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
|
|
259 |
draw = ImageDraw.Draw(annotated_image)
|
260 |
font = ImageFont.load_default()
|
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 |
+
buttons_html = ""
|
298 |
+
|
|
|
299 |
html_output = f"""
|
300 |
<style>
|
301 |
.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 |
.breed-buttons {{ margin-top: 10px; }}
|
304 |
.breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
305 |
</style>
|
306 |
+
{dogs_info}
|
307 |
"""
|
308 |
|
309 |
+
|
310 |
+
if buttons:
|
311 |
html_output += """
|
312 |
<script>
|
313 |
function handle_button_click(button_id) {
|
|
|
321 |
</script>
|
322 |
"""
|
323 |
initial_state = {
|
324 |
+
"dogs_info": dogs_info,
|
325 |
+
"buttons": buttons,
|
326 |
"show_back": True,
|
327 |
"image": annotated_image,
|
328 |
"is_multi_dog": len(dogs) > 1,
|
329 |
+
"html_output": html_output
|
330 |
}
|
331 |
+
return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
332 |
+
else:
|
333 |
+
initial_state = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
334 |
"dogs_info": dogs_info,
|
335 |
"buttons": [],
|
336 |
"show_back": False,
|
337 |
"image": annotated_image,
|
338 |
"is_multi_dog": len(dogs) > 1,
|
339 |
+
"html_output": html_output
|
340 |
+
}
|
341 |
+
return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
342 |
+
|
343 |
|
344 |
except Exception as e:
|
345 |
error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
346 |
print(error_msg)
|
347 |
return error_msg, None, gr.update(visible=False, choices=[]), None
|
348 |
|
|
|
349 |
|
350 |
def show_details_html(choice, previous_output, initial_state):
|
351 |
if not choice:
|