DawnC commited on
Commit
43f7280
1 Parent(s): 4c94f33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -51
app.py CHANGED
@@ -308,73 +308,59 @@ async def predict(image):
308
  if isinstance(image, np.ndarray):
309
  image = Image.fromarray(image)
310
 
311
- # 快速檢查是否有多個狗
312
  dogs = await detect_multiple_dogs(image)
313
 
314
- if len(dogs) <= 1: # 單狗或無狗情況
315
- top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
316
- breed = topk_breeds[0]
317
- description = get_dog_description(breed)
318
- formatted_description = format_description(description, breed)
319
-
320
- if top1_prob >= 0.5:
321
- return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
322
- elif 0.2 <= top1_prob < 0.5:
323
- explanation = f"""
324
- **Detected with moderate confidence. Here are the top 3 possible breeds:**
325
-
326
- 1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
327
- 2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
328
- 3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
329
 
330
- Click on a button below to view more information about each breed.
331
- """
332
- return explanation, image, gr.update(visible=True, value=f"More about {topk_breeds[0]}"), gr.update(visible=True, value=f"More about {topk_breeds[1]}"), gr.update(visible=True, value=f"More about {topk_breeds[2]}")
333
- else:
334
- return "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
335
-
336
- # 多狗情況
337
  explanations = []
338
- visible_buttons = []
339
  annotated_image = image.copy()
340
  draw = ImageDraw.Draw(annotated_image)
341
- font = ImageFont.load_default()
342
 
343
  for i, (cropped_image, _, box) in enumerate(dogs, 1):
344
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
345
 
346
- draw.rectangle(box, outline="red", width=3)
347
- draw.text((box[0], box[1]), f"Dog {i}", fill="red", font=font)
 
 
 
 
348
 
349
  if top1_prob >= 0.5:
350
  breed = topk_breeds[0]
351
  description = get_dog_description(breed)
352
- explanations.append(format_description(description, breed, is_multi_dog=True, dog_number=i))
353
- elif 0.2 <= top1_prob < 0.5:
354
  explanation = f"""
355
  **Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:**
356
 
357
  1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
358
  2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
359
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
 
 
 
 
360
  """
361
- explanations.append(explanation)
362
- visible_buttons.extend([f"More about Dog {i}: {topk_breeds[0]}", f"More about Dog {i}: {topk_breeds[1]}", f"More about Dog {i}: {topk_breeds[2]}"])
363
- else:
364
- explanations.append(f"**Dog {i}**: The image is unclear or the breed is not in the dataset.")
365
 
366
- final_explanation = "\n\n".join(explanations)
367
- return final_explanation, annotated_image, gr.update(visible=len(visible_buttons) >= 1, value=visible_buttons[0] if visible_buttons else ""), gr.update(visible=True, value=visible_buttons[1] if len(visible_buttons) >= 2 else ""), gr.update(visible=True, value=visible_buttons[2] if len(visible_buttons) >= 3 else "")
 
 
 
 
 
 
 
 
 
368
 
369
  except Exception as e:
370
  return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
371
 
372
- def show_details(breed):
373
- breed_name = breed.split("More about ")[-1].split(": ")[-1] # Handle both single and multi-dog cases
374
- description = get_dog_description(breed_name)
375
- return format_description(description, breed_name)
376
-
377
- # Gradio interface setup
378
  with gr.Blocks(css="""
379
  .container { max-width: 900px; margin: auto; padding: 20px; }
380
  .gr-box { border-radius: 15px; }
@@ -391,17 +377,8 @@ with gr.Blocks(css="""
391
  output_image = gr.Image(label="Annotated Image")
392
 
393
  output = gr.Markdown(label="Prediction Results")
394
-
395
- with gr.Row():
396
- btn1 = gr.Button("View More 1", visible=False)
397
- btn2 = gr.Button("View More 2", visible=False)
398
- btn3 = gr.Button("View More 3", visible=False)
399
 
400
- input_image.change(predict, inputs=input_image, outputs=[output, output_image, btn1, btn2, btn3])
401
-
402
- btn1.click(show_details, inputs=btn1, outputs=output)
403
- btn2.click(show_details, inputs=btn2, outputs=output)
404
- btn3.click(show_details, inputs=btn3, outputs=output)
405
 
406
  gr.Examples(
407
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
 
308
  if isinstance(image, np.ndarray):
309
  image = Image.fromarray(image)
310
 
 
311
  dogs = await detect_multiple_dogs(image)
312
 
313
+ if len(dogs) == 0:
314
+ return "No dogs detected. Please upload a clear image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
315
 
 
 
 
 
 
 
 
316
  explanations = []
 
317
  annotated_image = image.copy()
318
  draw = ImageDraw.Draw(annotated_image)
319
+ font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20)
320
 
321
  for i, (cropped_image, _, box) in enumerate(dogs, 1):
322
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
323
 
324
+ # 繪製更醒目的邊框和標籤
325
+ draw.rectangle(box, outline="red", width=5)
326
+ label = f"Dog {i}"
327
+ label_size = draw.textsize(label, font=font)
328
+ draw.rectangle([box[0], box[1], box[0] + label_size[0], box[1] + label_size[1]], fill="red")
329
+ draw.text((box[0], box[1]), label, fill="white", font=font)
330
 
331
  if top1_prob >= 0.5:
332
  breed = topk_breeds[0]
333
  description = get_dog_description(breed)
334
+ explanation = f"**Dog {i}: {breed}**\n\n{format_description(description, breed)}"
335
+ else:
336
  explanation = f"""
337
  **Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:**
338
 
339
  1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
340
  2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
341
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
342
+
343
+ [More about {topk_breeds[0]}](#breed_{i}_1)
344
+ [More about {topk_breeds[1]}](#breed_{i}_2)
345
+ [More about {topk_breeds[2]}](#breed_{i}_3)
346
  """
 
 
 
 
347
 
348
+ explanations.append(explanation)
349
+
350
+ final_explanation = "\n\n---\n\n".join(explanations)
351
+
352
+ # 添加隱藏的詳細信息部分
353
+ for i, (_, _, _) in enumerate(dogs, 1):
354
+ for j, breed in enumerate(topk_breeds, 1):
355
+ description = get_dog_description(breed)
356
+ final_explanation += f"\n\n<div id='breed_{i}_{j}' style='display:none;'>\n\n{format_description(description, breed)}\n\n</div>"
357
+
358
+ return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
359
 
360
  except Exception as e:
361
  return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
362
 
363
+ # Gradio 界面設置
 
 
 
 
 
364
  with gr.Blocks(css="""
365
  .container { max-width: 900px; margin: auto; padding: 20px; }
366
  .gr-box { border-radius: 15px; }
 
377
  output_image = gr.Image(label="Annotated Image")
378
 
379
  output = gr.Markdown(label="Prediction Results")
 
 
 
 
 
380
 
381
+ input_image.change(predict, inputs=input_image, outputs=[output, output_image])
 
 
 
 
382
 
383
  gr.Examples(
384
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],