DawnC commited on
Commit
d9da78c
·
verified ·
1 Parent(s): 47f531d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -30
app.py CHANGED
@@ -243,26 +243,74 @@ 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)
@@ -276,36 +324,39 @@ async def predict(image):
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:
 
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
 
312
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
313
+ buttons_html = "" # 每次迴圈重置按鈕 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)
 
324
  breed = topk_breeds[0]
325
  description = get_dog_description(breed)
326
  dogs_info += format_description_html(description, breed)
327
+
328
+ # 在品種描述後加入按鈕
329
+ button_id = f"Dog {i+1}: More about {breed}"
330
+ buttons_html += f'<button style="display:block; margin-top:10px;" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
331
+ dogs_info += buttons_html
332
 
333
  elif combined_confidence >= 0.15:
334
  dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
335
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
336
  prob = float(prob.replace('%', ''))
337
  dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
 
338
 
339
+ # 為每個品種插入按鈕
340
  button_id = f"Dog {i+1}: More about {breed}"
341
+ buttons_html += f'<button style="display:block; margin-top:10px;" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
342
+
343
+ dogs_info += "</ul>"
344
+ # 在每個品種描述後插入按鈕
345
+ dogs_info += buttons_html # 現在這段是直接放在每個品種的描述之後
346
 
347
  else:
348
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
349
 
350
+ dogs_info += '</div>' # 結束當前狗的資訊區塊
351
+
352
+ # 不再生成統一的按鈕集,而是每個品種自行管理按鈕
 
 
353
  html_output = f"""
354
+ <style>
355
+ .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); }}
356
+ </style>
357
+ {dogs_info}
 
 
 
358
  """
359
+
360
 
361
 
362
  if buttons: