DawnC commited on
Commit
2f1738c
·
1 Parent(s): 3f8895a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -72
app.py CHANGED
@@ -261,110 +261,90 @@ 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
-
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)
317
  draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
318
-
319
- # 顯示追蹤訊息來幫助檢查數據
320
- print(f"Detection confidence for Dog {i+1}: {detection_confidence}")
321
- print(f"Top1 probability for Dog {i+1}: {top1_prob}")
322
-
323
  combined_confidence = detection_confidence * top1_prob
324
- print(f"Combined confidence for Dog {i+1}: {combined_confidence}") # 確認combined confidence
325
-
326
  dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
327
  dogs_info += f'<h2>Dog {i+1}</h2>'
328
-
329
- if combined_confidence >= 0.45: # 條件1:當 conf >= 0.45 時,返回最可能的品種資訊
330
  breed = topk_breeds[0]
331
  description = get_dog_description(breed)
332
  dogs_info += format_description_html(description, breed)
333
 
334
- elif 0.15 < combined_confidence < 0.45: # 條件2:當 0.15 < conf < 0.45 時,返回前三個可能性並生成按鈕
335
- print(f"Entering button generation for Dog {i+1}") # 確認進入這部分的條件
336
  dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
337
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
338
  prob = float(prob.replace('%', ''))
339
  dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
340
-
341
- # 為每個品種生成按鈕並輸出確認訊息
342
  button_id = f"Dog {i+1}: More about {breed}"
343
- buttons_html += f'<button style="display:inline-block; margin-top:10px;" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
344
- print(f"Button generated for breed: {breed}") # 檢查按鈕是否生成
 
 
 
345
 
346
- # 渲染按鈕,確保CSS樣式顯示正確
347
- dogs_info += f'<div style="margin-top:10px;">{buttons_html}</div>' # 確保按鈕顯示在該品種描述下
348
-
349
- else: # 條件3:當 conf < 0.15 時,返回 "圖像不清晰或無法辨認"
350
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
351
 
352
  dogs_info += '</div>' # 結束當前狗的資訊區塊
353
-
354
- # 最終生成的 HTML 輸出
355
- html_output = f"""
356
- <style>
357
- .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); }}
358
- </style>
359
- {dogs_info}
360
- """
361
- print(f"Final HTML output: {html_output}") # 檢查最終生成的 HTML
362
-
363
-
364
-
365
 
366
 
367
-
 
 
 
 
 
 
 
 
 
 
 
368
 
369
  if buttons:
370
  html_output += """
 
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
 
299
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
300
+ buttons_html = "" # 初始化按鈕 HTML
301
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
302
  color = color_list[i % len(color_list)]
303
  draw.rectangle(box, outline=color, width=3)
304
  draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
305
+
 
 
 
 
306
  combined_confidence = detection_confidence * top1_prob
 
 
307
  dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
308
  dogs_info += f'<h2>Dog {i+1}</h2>'
309
+
310
+ if top1_prob >= 0.45:
311
  breed = topk_breeds[0]
312
  description = get_dog_description(breed)
313
  dogs_info += format_description_html(description, breed)
314
 
315
+ elif combined_confidence >= 0.15:
316
+ # 列出前三個可能的品種
317
  dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
318
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
319
  prob = float(prob.replace('%', ''))
320
  dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
321
+
322
+ # 在每個品種之後添加按鈕
323
  button_id = f"Dog {i+1}: More about {breed}"
324
+ buttons_html += f'<button class="breed-button" style="margin-right: 5px;" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
325
+ buttons.append(button_id)
326
+
327
+ dogs_info += "</ul>"
328
+ dogs_info += f'<div class="breed-buttons">{buttons_html}</div>' # 把按鈕放到當前品種描述之後
329
 
330
+ else:
 
 
 
331
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
332
 
333
  dogs_info += '</div>' # 結束當前狗的資訊區塊
 
 
 
 
 
 
 
 
 
 
 
 
334
 
335
 
336
+
337
+ buttons_html = ""
338
+
339
+ html_output = f"""
340
+ <style>
341
+ .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); }}
342
+ .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
343
+ .breed-buttons {{ margin-top: 10px; }}
344
+ .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
345
+ </style>
346
+ {dogs_info}
347
+ """
348
 
349
  if buttons:
350
  html_output += """