Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -243,116 +243,7 @@ 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 |
-
|
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 |
-
# if buttons:
|
312 |
-
# html_output += """
|
313 |
-
# <script>
|
314 |
-
# function handle_button_click(button_id) {
|
315 |
-
# const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
|
316 |
-
# if (radio) {
|
317 |
-
# radio.click();
|
318 |
-
# } else {
|
319 |
-
# console.error("Radio button not found:", button_id);
|
320 |
-
# }
|
321 |
-
# }
|
322 |
-
# </script>
|
323 |
-
# """
|
324 |
-
# initial_state = {
|
325 |
-
# "dogs_info": dogs_info,
|
326 |
-
# "buttons": buttons,
|
327 |
-
# "show_back": True,
|
328 |
-
# "image": annotated_image,
|
329 |
-
# "is_multi_dog": len(dogs) > 1,
|
330 |
-
# "html_output": html_output
|
331 |
-
# }
|
332 |
-
# return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
333 |
-
# else:
|
334 |
-
# initial_state = {
|
335 |
-
# "dogs_info": dogs_info,
|
336 |
-
# "buttons": [],
|
337 |
-
# "show_back": False,
|
338 |
-
# "image": annotated_image,
|
339 |
-
# "is_multi_dog": len(dogs) > 1,
|
340 |
-
# "html_output": html_output
|
341 |
-
# }
|
342 |
-
# return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
343 |
-
|
344 |
-
|
345 |
-
# except Exception as e:
|
346 |
-
# error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
347 |
-
# print(error_msg)
|
348 |
-
# return error_msg, None, gr.update(visible=False, choices=[]), None
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
async def predict(image):
|
353 |
-
# 定義初始值,避免未賦值錯誤
|
354 |
-
html_output = ""
|
355 |
-
|
356 |
if image is None:
|
357 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
358 |
|
@@ -371,7 +262,7 @@ async def predict(image):
|
|
371 |
dogs_info = ""
|
372 |
|
373 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
374 |
-
buttons_html = ""
|
375 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
376 |
color = color_list[i % len(color_list)]
|
377 |
draw.rectangle(box, outline=color, width=3)
|
@@ -385,51 +276,78 @@ async def predict(image):
|
|
385 |
breed = topk_breeds[0]
|
386 |
description = get_dog_description(breed)
|
387 |
dogs_info += format_description_html(description, breed)
|
388 |
-
|
389 |
-
# 在品種描述後加入按鈕
|
390 |
-
button_id = f"Dog {i+1}: More about {breed}"
|
391 |
-
buttons_html += f'<button style="display:block; margin-top:10px;" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
392 |
-
dogs_info += buttons_html
|
393 |
|
394 |
elif combined_confidence >= 0.15:
|
395 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
396 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
397 |
prob = float(prob.replace('%', ''))
|
398 |
dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
|
|
|
399 |
|
400 |
-
|
401 |
button_id = f"Dog {i+1}: More about {breed}"
|
402 |
-
buttons_html += f'<button
|
403 |
-
|
404 |
-
dogs_info += "</ul>"
|
405 |
-
# 在品種後插入按鈕
|
406 |
-
dogs_info += buttons_html
|
407 |
|
408 |
else:
|
409 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
410 |
|
411 |
-
dogs_info += '</div>'
|
412 |
-
|
413 |
-
|
|
|
|
|
414 |
html_output = f"""
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
|
|
|
|
|
|
419 |
"""
|
|
|
420 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
421 |
|
422 |
|
423 |
-
|
424 |
-
return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
425 |
-
|
426 |
except Exception as e:
|
427 |
-
error_msg = f"An error occurred: {str(e)}"
|
428 |
print(error_msg)
|
429 |
return error_msg, None, gr.update(visible=False, choices=[]), None
|
430 |
|
431 |
|
432 |
-
|
433 |
def show_details_html(choice, previous_output, initial_state):
|
434 |
if not choice:
|
435 |
return previous_output, gr.update(visible=True), initial_state
|
|
|
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 |
|
|
|
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)
|
|
|
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 |
+
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 |
+
if buttons:
|
312 |
+
html_output += """
|
313 |
+
<script>
|
314 |
+
function handle_button_click(button_id) {
|
315 |
+
const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
|
316 |
+
if (radio) {
|
317 |
+
radio.click();
|
318 |
+
} else {
|
319 |
+
console.error("Radio button not found:", button_id);
|
320 |
+
}
|
321 |
+
}
|
322 |
+
</script>
|
323 |
+
"""
|
324 |
+
initial_state = {
|
325 |
+
"dogs_info": dogs_info,
|
326 |
+
"buttons": buttons,
|
327 |
+
"show_back": True,
|
328 |
+
"image": annotated_image,
|
329 |
+
"is_multi_dog": len(dogs) > 1,
|
330 |
+
"html_output": html_output
|
331 |
+
}
|
332 |
+
return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
333 |
+
else:
|
334 |
+
initial_state = {
|
335 |
+
"dogs_info": dogs_info,
|
336 |
+
"buttons": [],
|
337 |
+
"show_back": False,
|
338 |
+
"image": annotated_image,
|
339 |
+
"is_multi_dog": len(dogs) > 1,
|
340 |
+
"html_output": html_output
|
341 |
+
}
|
342 |
+
return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
343 |
|
344 |
|
|
|
|
|
|
|
345 |
except Exception as e:
|
346 |
+
error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
347 |
print(error_msg)
|
348 |
return error_msg, None, gr.update(visible=False, choices=[]), None
|
349 |
|
350 |
|
|
|
351 |
def show_details_html(choice, previous_output, initial_state):
|
352 |
if not choice:
|
353 |
return previous_output, gr.update(visible=True), initial_state
|