DawnC commited on
Commit
9ac86e7
1 Parent(s): e28b46e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -20
app.py CHANGED
@@ -40,15 +40,15 @@ class_names = ['Abyssinian (阿比西尼亞貓)', 'American Bulldog (美國鬥
40
 
41
  # 定義預測函數
42
  def classify_image(image):
43
- image = transform(image).unsqueeze(0).to(device) # 確保影像資料處理在 CPU 上
44
  with torch.no_grad():
45
  outputs = model(image)
46
- probabilities, indices = torch.topk(outputs, k=3) # 取得前3個預測
47
- probabilities = torch.nn.functional.softmax(probabilities, dim=1) # 將結果轉換為機率
48
  predictions = [(class_names[idx], prob.item()) for idx, prob in zip(indices[0], probabilities[0])]
49
- return {class_name: prob for class_name, prob in predictions} # Return raw float numbers
50
 
51
- # 設置 examples 路徑
52
  examples_path = './examples'
53
 
54
  if os.path.exists(examples_path):
@@ -56,23 +56,21 @@ if os.path.exists(examples_path):
56
  else:
57
  print(f"[ERROR] Examples folder not found at {examples_path}")
58
 
59
- # Gradio 介面
60
  examples = [[examples_path + "/" + img] for img in os.listdir(examples_path)]
61
 
62
- # 使用 Markdown 顯示 37 個品種
63
- breed_list = "### Recognizable Breeds\n" + "\n".join([f"- {breed}" for breed in class_names])
64
-
65
- demo = gr.Blocks()
66
 
67
- with demo:
68
- gr.Markdown("# Oxford Pet 🐕🐈 Recognizable Breeds")
69
- gr.Markdown(breed_list) # 用 Markdown 顯示品種列表
70
- gr.Interface(fn=classify_image,
71
- inputs=gr.Image(type="pil"), # 只需要圖片輸入
72
- outputs=[gr.Label(num_top_classes=3, label="Top 3 Predictions")],
73
- examples=examples,
74
- title='Oxford Pet 🐈🐕',
75
- description='A ResNet50-based model for classifying 37 different pet breeds.',
76
- article="[Oxford Project](https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/The%20Oxford-IIIT%20Pet%20Project)").launch()
77
 
78
  demo.launch()
 
40
 
41
  # 定義預測函數
42
  def classify_image(image):
43
+ image = transform(image).unsqueeze(0).to(device)
44
  with torch.no_grad():
45
  outputs = model(image)
46
+ probabilities, indices = torch.topk(outputs, k=3)
47
+ probabilities = torch.nn.functional.softmax(probabilities, dim=1)
48
  predictions = [(class_names[idx], prob.item()) for idx, prob in zip(indices[0], probabilities[0])]
49
+ return {class_name: f"{prob:.2f}" for class_name, prob in predictions}
50
 
51
+ # 設定 examples 路徑
52
  examples_path = './examples'
53
 
54
  if os.path.exists(examples_path):
 
56
  else:
57
  print(f"[ERROR] Examples folder not found at {examples_path}")
58
 
59
+ # 設定範例圖片
60
  examples = [[examples_path + "/" + img] for img in os.listdir(examples_path)]
61
 
62
+ # 新增下拉選單,顯示所有品種
63
+ dropdown = gr.Dropdown(choices=class_names, label="Select a breed", type="value")
 
 
64
 
65
+ # Gradio 介面
66
+ demo = gr.Interface(
67
+ fn=classify_image,
68
+ inputs=[gr.Image(type="pil")], # 移除掉 dropdown 作為輸入
69
+ outputs=[gr.Label(num_top_classes=3, label="Top 3 Predictions")],
70
+ examples=examples,
71
+ title='Oxford Pet 🐈🐕',
72
+ description='A ResNet50-based model for classifying 37 different pet breeds.',
73
+ article='[Oxford Project](https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/The%20Oxford-IIIT%20Pet%20Project)'
74
+ )
75
 
76
  demo.launch()