DawnC commited on
Commit
dfaaf93
·
verified ·
1 Parent(s): 8b1c376

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +221 -56
app.py CHANGED
@@ -243,6 +243,210 @@ 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
@@ -261,41 +465,6 @@ 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
-
299
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
300
  buttons_html = ""
301
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
@@ -320,7 +489,8 @@ async def predict(image):
320
  # 每個品種名稱後面立即生成按鈕
321
  button_id = f"Dog {i+1}: More about {breed}"
322
  dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)"
323
- dogs_info += f'<button style="background-color: #4CAF50; color: white; border: none; padding: 5px 10px; border-radius: 3px; margin-left: 10px;" onclick="alert(\'{breed} clicked\')">Learn More</button></li>'
 
324
  buttons.append(button_id)
325
  dogs_info += "</ul>"
326
 
@@ -330,11 +500,6 @@ async def predict(image):
330
  dogs_info += '</div>' # 結束當前狗的資訊區塊
331
 
332
 
333
-
334
-
335
-
336
- buttons_html = ""
337
-
338
  html_output = f"""
339
  <style>
340
  .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); }}
@@ -345,19 +510,21 @@ async def predict(image):
345
  {dogs_info}
346
  """
347
 
348
- if buttons:
349
- html_output += """
350
- <script>
351
- function handle_button_click(button_id) {
352
- const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
353
- if (radio) {
354
- radio.click();
355
- } else {
356
- console.error("Radio button not found:", button_id);
357
- }
358
  }
359
- </script>
360
- """
 
 
 
361
  initial_state = {
362
  "dogs_info": dogs_info,
363
  "buttons": buttons,
@@ -478,7 +645,5 @@ with gr.Blocks() as iface:
478
 
479
  gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
480
 
481
-
482
-
483
  if __name__ == "__main__":
484
- iface.launch()
 
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
+ # if buttons:
311
+ # html_output += """
312
+ # <script>
313
+ # function handle_button_click(button_id) {
314
+ # const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
315
+ # if (radio) {
316
+ # radio.click();
317
+ # } else {
318
+ # console.error("Radio button not found:", button_id);
319
+ # }
320
+ # }
321
+ # </script>
322
+ # """
323
+ # initial_state = {
324
+ # "dogs_info": dogs_info,
325
+ # "buttons": buttons,
326
+ # "show_back": True,
327
+ # "image": annotated_image,
328
+ # "is_multi_dog": len(dogs) > 1,
329
+ # "html_output": html_output
330
+ # }
331
+ # return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
332
+ # else:
333
+ # initial_state = {
334
+ # "dogs_info": dogs_info,
335
+ # "buttons": [],
336
+ # "show_back": False,
337
+ # "image": annotated_image,
338
+ # "is_multi_dog": len(dogs) > 1,
339
+ # "html_output": html_output
340
+ # }
341
+ # return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
342
+
343
+
344
+ # except Exception as e:
345
+ # error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
346
+ # print(error_msg)
347
+ # return error_msg, None, gr.update(visible=False, choices=[]), None
348
+
349
+
350
+ # def show_details_html(choice, previous_output, initial_state):
351
+ # if not choice:
352
+ # return previous_output, gr.update(visible=True), initial_state
353
+
354
+ # try:
355
+ # breed = choice.split("More about ")[-1]
356
+ # description = get_dog_description(breed)
357
+ # formatted_description = format_description_html(description, breed)
358
+
359
+ # html_output = f"""
360
+ # <div class="dog-info">
361
+ # <h2>{breed}</h2>
362
+ # {formatted_description}
363
+ # </div>
364
+ # """
365
+
366
+ # initial_state["current_description"] = html_output
367
+ # initial_state["original_buttons"] = initial_state.get("buttons", [])
368
+
369
+ # return html_output, gr.update(visible=True), initial_state
370
+ # except Exception as e:
371
+ # error_msg = f"An error occurred while showing details: {e}"
372
+ # print(error_msg)
373
+ # return f"<p style='color: red;'>{error_msg}</p>", gr.update(visible=True), initial_state
374
+
375
+
376
+ # def format_description_html(description, breed):
377
+ # html = "<ul style='list-style-type: none; padding-left: 0;'>"
378
+ # if isinstance(description, dict):
379
+ # for key, value in description.items():
380
+ # html += f"<li style='margin-bottom: 10px;'><strong>{key}:</strong> {value}</li>"
381
+ # elif isinstance(description, str):
382
+ # html += f"<li>{description}</li>"
383
+ # else:
384
+ # html += f"<li>No description available for {breed}</li>"
385
+ # html += "</ul>"
386
+ # akc_link = get_akc_breeds_link()
387
+ # html += f'<p><a href="{akc_link}" target="_blank">Learn more about {breed} on the AKC website</a></p>'
388
+ # return html
389
+
390
+
391
+ # def go_back(state):
392
+ # buttons = state.get("buttons", [])
393
+ # return (
394
+ # state["html_output"],
395
+ # state["image"],
396
+ # gr.update(visible=True, choices=buttons),
397
+ # gr.update(visible=False),
398
+ # state
399
+ # )
400
+
401
+
402
+ # with gr.Blocks() as iface:
403
+ # gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
404
+ # gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>")
405
+
406
+ # with gr.Row():
407
+ # input_image = gr.Image(label="Upload a dog image", type="pil")
408
+ # output_image = gr.Image(label="Annotated Image")
409
+
410
+ # output = gr.HTML(label="Prediction Results")
411
+
412
+ # breed_buttons = gr.Radio(choices=[], label="More Information", visible=False)
413
+
414
+ # back_button = gr.Button("Back", visible=False)
415
+
416
+ # initial_state = gr.State()
417
+
418
+ # input_image.change(
419
+ # predict,
420
+ # inputs=input_image,
421
+ # outputs=[output, output_image, breed_buttons, initial_state]
422
+ # )
423
+
424
+ # breed_buttons.change(
425
+ # show_details_html,
426
+ # inputs=[breed_buttons, output, initial_state],
427
+ # outputs=[output, back_button, initial_state]
428
+ # )
429
+
430
+ # back_button.click(
431
+ # go_back,
432
+ # inputs=[initial_state],
433
+ # outputs=[output, output_image, breed_buttons, back_button, initial_state]
434
+ # )
435
+
436
+ # gr.Examples(
437
+ # examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
438
+ # inputs=input_image
439
+ # )
440
+
441
+ # gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
442
+
443
+
444
+
445
+ # if __name__ == "__main__":
446
+ # iface.launch()
447
+
448
+
449
+
450
  async def predict(image):
451
  if image is None:
452
  return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
 
465
 
466
  dogs_info = ""
467
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
469
  buttons_html = ""
470
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
 
489
  # 每個品種名稱後面立即生成按鈕
490
  button_id = f"Dog {i+1}: More about {breed}"
491
  dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)"
492
+ # 新增按鈕,觸發相應事件
493
+ dogs_info += f'<button style="background-color: #4CAF50; color: white; border: none; padding: 5px 10px; border-radius: 3px; margin-left: 10px;" onclick="handle_button_click(\'{button_id}\')">Learn More</button></li>'
494
  buttons.append(button_id)
495
  dogs_info += "</ul>"
496
 
 
500
  dogs_info += '</div>' # 結束當前狗的資訊區塊
501
 
502
 
 
 
 
 
 
503
  html_output = f"""
504
  <style>
505
  .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); }}
 
510
  {dogs_info}
511
  """
512
 
513
+ # 生成腳本來處理按鈕點擊事件
514
+ html_output += """
515
+ <script>
516
+ function handle_button_click(button_id) {
517
+ const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
518
+ if (radio) {
519
+ radio.click(); // 點擊按鈕後觸發事件
520
+ } else {
521
+ console.error("Radio button not found:", button_id);
 
522
  }
523
+ }
524
+ </script>
525
+ """
526
+
527
+ if buttons:
528
  initial_state = {
529
  "dogs_info": dogs_info,
530
  "buttons": buttons,
 
645
 
646
  gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
647
 
 
 
648
  if __name__ == "__main__":
649
+ iface.launch()