DawnC commited on
Commit
8755519
·
verified ·
1 Parent(s): 0bae045

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -89
app.py CHANGED
@@ -247,6 +247,116 @@ 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
@@ -263,47 +373,7 @@ async def predict(image):
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 = "" # 初始化 dogs_info
306
- buttons_html = "" # 初始化 buttons_html
307
 
308
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
309
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
@@ -316,38 +386,28 @@ async def predict(image):
316
  dogs_info += f'<h2>Dog {i+1}</h2>'
317
 
318
  if top1_prob >= 0.45:
319
- # 當信心 >= 0.45 顯示單一品種
320
  breed = topk_breeds[0]
321
  description = get_dog_description(breed)
322
  dogs_info += format_description_html(description, breed)
323
 
324
  elif combined_confidence >= 0.15:
325
- # 顯示 top3 品種
326
  dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
327
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
328
- prob = float(prob.replace('%', '')) # 去掉百分比符號並轉為浮點數
329
  dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
330
  dogs_info += "</ul>"
331
 
332
- # 單獨生成按鈕邏輯
333
- temp_buttons = '<div class="breed-buttons">' # 按鈕生成區塊
334
  for breed in topk_breeds[:3]:
335
  button_id = f"Dog {i+1}: More about {breed}"
336
- temp_buttons += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
337
  buttons.append(button_id)
338
- temp_buttons += '</div>'
339
-
340
- buttons_html += temp_buttons # 按鈕 HTML 放置到按鈕區塊中
341
 
342
  else:
343
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
344
 
345
- dogs_info += '</div>' # 結束該狗的描述區塊
346
-
347
- # 避免將按鈕與 dogs_info 同步顯示
348
- if buttons_html:
349
- dogs_info += buttons_html # 確保按鈕獨立並附加到最後輸出
350
-
351
 
352
  html_output = f"""
353
  <style>
@@ -357,7 +417,6 @@ async def predict(image):
357
  .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
358
  </style>
359
  {dogs_info}
360
-
361
  """
362
 
363
  if buttons:
@@ -373,25 +432,17 @@ async def predict(image):
373
  }
374
  </script>
375
  """
376
- initial_state = {
377
- "dogs_info": dogs_info,
378
- "buttons": buttons,
379
- "show_back": True,
380
- "image": annotated_image,
381
- "is_multi_dog": len(dogs) > 1,
382
- "html_output": html_output # 儲存完整的 HTML 輸出
383
- }
384
- return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
385
- else:
386
- initial_state = {
387
- "dogs_info": dogs_info,
388
- "buttons": [],
389
- "show_back": False,
390
- "image": annotated_image,
391
- "is_multi_dog": len(dogs) > 1,
392
- "html_output": html_output # 儲存完整的 HTML 輸出
393
- }
394
- return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
395
 
396
  except Exception as e:
397
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
@@ -440,22 +491,10 @@ def format_description_html(description, breed):
440
  return html
441
 
442
 
443
- # def go_back(state):
444
- # buttons = state.get("buttons", [])
445
- # return (
446
- # state["html_output"],
447
- # state["image"],
448
- # gr.update(visible=True, choices=buttons),
449
- # gr.update(visible=False),
450
- # state
451
- # )
452
-
453
  def go_back(state):
454
  buttons = state.get("buttons", [])
455
- # 使用原始的 HTML 輸出,不添加任何額外信息
456
- html_output = state["html_output"]
457
  return (
458
- html_output,
459
  state["image"],
460
  gr.update(visible=True, choices=buttons),
461
  gr.update(visible=False),
 
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
 
373
  draw = ImageDraw.Draw(annotated_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)
 
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
+ prob = float(prob.replace('%', ''))
397
  dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
398
  dogs_info += "</ul>"
399
 
400
+ dogs_info += '<div class="breed-buttons">'
 
401
  for breed in topk_breeds[:3]:
402
  button_id = f"Dog {i+1}: More about {breed}"
403
+ dogs_info += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
404
  buttons.append(button_id)
405
+ dogs_info += '</div>'
 
 
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
  <style>
 
417
  .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
418
  </style>
419
  {dogs_info}
 
420
  """
421
 
422
  if buttons:
 
432
  }
433
  </script>
434
  """
435
+
436
+ initial_state = {
437
+ "dogs_info": dogs_info,
438
+ "buttons": buttons,
439
+ "show_back": bool(buttons),
440
+ "image": annotated_image,
441
+ "is_multi_dog": len(dogs) > 1,
442
+ "html_output": html_output
443
+ }
444
+
445
+ return html_output, annotated_image, gr.update(visible=bool(buttons), choices=buttons), initial_state
 
 
 
 
 
 
 
 
446
 
447
  except Exception as e:
448
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
 
491
  return html
492
 
493
 
 
 
 
 
 
 
 
 
 
 
494
  def go_back(state):
495
  buttons = state.get("buttons", [])
 
 
496
  return (
497
+ state["html_output"],
498
  state["image"],
499
  gr.update(visible=True, choices=buttons),
500
  gr.update(visible=False),