DawnC commited on
Commit
f88f4be
1 Parent(s): 2255c7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -21
app.py CHANGED
@@ -171,25 +171,22 @@ def predict(image):
171
  return "Please upload an image to get started.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
172
 
173
  try:
174
- # 檢查圖片是否是 numpy.ndarray,如果是則轉換為 PIL.Image
175
  if isinstance(image, np.ndarray):
176
  image = Image.fromarray(image)
177
 
178
  # 使用 YOLO 偵測狗
179
  results = model_yolo(image)
180
- boxes = results[0].boxes # 提取邊界框
181
 
182
  if len(boxes) == 0:
183
- return "No dog detected in the image.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
184
 
185
  explanations = []
186
  visible_buttons = []
187
 
188
  for i, box in enumerate(boxes):
189
- # 提取每隻狗的區域
190
- x1, y1, x2, y2 = map(int, box.xyxy[0]) # 使用 box.xyxy 來提取邊界框座標
191
-
192
- # 裁剪出狗區域,確保 image 是 PIL.Image 格式
193
  cropped_image = image.crop((x1, y1, x2, y2))
194
  image_tensor = preprocess_image(cropped_image)
195
 
@@ -204,34 +201,28 @@ def predict(image):
204
  topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
205
  topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
206
 
207
- # 根據信心分數進行判斷
208
  if top1_prob >= 0.5:
209
  breed = topk_breeds[0]
210
  description = get_dog_description(breed)
211
- explanations.append(f"Detected a dog: **{breed}** with {topk_probs_percent[0]} confidence.")
212
- elif 0.2 <= top1_prob < 0.5:
 
 
213
  explanation = (
214
- f"Detected a dog with moderate confidence. Here are the top 3 possible breeds:\n"
215
  f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
216
  f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
217
  f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n"
 
218
  )
219
- explanations.append(explanation)
220
- visible_buttons.extend([i+1 for _ in range(3)])
221
- else:
222
- explanations.append("The image is too unclear or the breed is not in the dataset. Please upload a clearer image.")
223
-
224
- # 處理不同情境的結果
225
- if len(explanations) > 0:
226
- final_explanation = "\n\n".join(explanations)
227
- return final_explanation, gr.update(visible=len(visible_buttons) >= 1), gr.update(visible=len(visible_buttons) >= 2), gr.update(visible=len(visible_buttons) >= 3)
228
 
229
  except Exception as e:
230
  return f"An error occurred: {e}", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
231
 
232
 
233
 
234
-
235
  def format_description(description, breed):
236
  if isinstance(description, dict):
237
  formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items()])
 
171
  return "Please upload an image to get started.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
172
 
173
  try:
174
+ # 確保圖片轉換為 PIL.Image 格式
175
  if isinstance(image, np.ndarray):
176
  image = Image.fromarray(image)
177
 
178
  # 使用 YOLO 偵測狗
179
  results = model_yolo(image)
180
+ boxes = results[0].boxes # 提取 YOLO 偵測結果中的邊界框
181
 
182
  if len(boxes) == 0:
183
+ return "The image is too unclear or the dog breed is not in the dataset. Please upload a clearer image of the dog.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
184
 
185
  explanations = []
186
  visible_buttons = []
187
 
188
  for i, box in enumerate(boxes):
189
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
 
 
 
190
  cropped_image = image.crop((x1, y1, x2, y2))
191
  image_tensor = preprocess_image(cropped_image)
192
 
 
201
  topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
202
  topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
203
 
204
+ # 高於 50% 信心的情況,直接返回詳細資訊,不顯示信心分數
205
  if top1_prob >= 0.5:
206
  breed = topk_breeds[0]
207
  description = get_dog_description(breed)
208
+ return format_description(description, breed), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
209
+
210
+ # 信心低於 50% 的情況,返回前三個可能的品種,並顯示按鈕
211
+ else:
212
  explanation = (
213
+ f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n"
214
  f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
215
  f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
216
  f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n"
217
+ "Click on a button to view more information about the breed."
218
  )
219
+ return explanation, 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]}")
 
 
 
 
 
 
 
 
220
 
221
  except Exception as e:
222
  return f"An error occurred: {e}", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
223
 
224
 
225
 
 
226
  def format_description(description, breed):
227
  if isinstance(description, dict):
228
  formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items()])