DawnC commited on
Commit
279a74b
1 Parent(s): c9e5868

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -5
app.py CHANGED
@@ -172,16 +172,19 @@ def predict(image):
172
  try:
173
  # 使用 YOLO 偵測狗
174
  results = model_yolo(image)
175
- dogs = results.xyxy[0] # 提取偵測到的狗的邊界框
176
-
177
- if len(dogs) == 0:
 
 
178
  return "No dog detected in the image.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
179
 
180
  explanations = []
181
  visible_buttons = []
182
 
183
- for i, box in enumerate(dogs):
184
- x1, y1, x2, y2 = map(int, box[:4])
 
185
  cropped_image = image.crop((x1, y1, x2, y2)) # 裁剪狗區域
186
  image_tensor = preprocess_image(cropped_image)
187
 
@@ -222,6 +225,7 @@ def predict(image):
222
  return f"An error occurred: {e}", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
223
 
224
 
 
225
  def format_description(description, breed):
226
  if isinstance(description, dict):
227
  formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items()])
 
172
  try:
173
  # 使用 YOLO 偵測狗
174
  results = model_yolo(image)
175
+
176
+ # 檢查 YOLO 輸出
177
+ boxes = results[0].boxes # 修改這裡,使用 results[0].boxes 來提取邊界框
178
+
179
+ if len(boxes) == 0:
180
  return "No dog detected in the image.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
181
 
182
  explanations = []
183
  visible_buttons = []
184
 
185
+ for i, box in enumerate(boxes):
186
+ # 提取每隻狗的區域
187
+ x1, y1, x2, y2 = map(int, box.xyxy[0]) # 使用 box.xyxy 來提取邊界框座標
188
  cropped_image = image.crop((x1, y1, x2, y2)) # 裁剪狗區域
189
  image_tensor = preprocess_image(cropped_image)
190
 
 
225
  return f"An error occurred: {e}", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
226
 
227
 
228
+
229
  def format_description(description, breed):
230
  if isinstance(description, dict):
231
  formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items()])