DawnC commited on
Commit
93d8148
·
verified ·
1 Parent(s): b8af8ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -12
app.py CHANGED
@@ -261,41 +261,87 @@ async def predict(image):
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); }}
@@ -306,6 +352,8 @@ async def predict(image):
306
  {dogs_info}
307
  """
308
 
 
 
309
  if buttons:
310
  html_output += """
311
  <script>
 
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
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
 
311
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
312
  color = color_list[i % len(color_list)]
313
  draw.rectangle(box, outline=color, width=3)
314
  draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
315
+
316
  combined_confidence = detection_confidence * top1_prob
317
  dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
318
  dogs_info += f'<h2>Dog {i+1}</h2>'
319
+
320
  if top1_prob >= 0.45:
321
  breed = topk_breeds[0]
322
  description = get_dog_description(breed)
323
  dogs_info += format_description_html(description, breed)
324
+
325
  elif combined_confidence >= 0.15:
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
+ dogs_info += '<div class="breed-buttons">'
334
  for breed in topk_breeds[:3]:
335
  button_id = f"Dog {i+1}: More about {breed}"
336
+ dogs_info += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
337
  buttons.append(button_id)
338
+ dogs_info += '</div>' # 結束按鈕部分
339
+
340
  else:
341
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
342
+
343
+ dogs_info += '</div>' # 結束狗狗信息框
344
+
 
 
345
  html_output = f"""
346
  <style>
347
  .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); }}
 
352
  {dogs_info}
353
  """
354
 
355
+
356
+
357
  if buttons:
358
  html_output += """
359
  <script>