DawnC commited on
Commit
3186b74
1 Parent(s): c617396

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -165,17 +165,20 @@ def get_akc_breeds_link():
165
  # except Exception as e:
166
  # return f"An error occurred: {e}", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
167
 
 
168
  def predict(image):
169
  if image is None:
170
  return "Please upload an image to get started.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
171
 
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
 
@@ -185,7 +188,9 @@ def predict(image):
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
 
191
  with torch.no_grad():
@@ -226,6 +231,7 @@ def predict(image):
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()])
 
165
  # except Exception as e:
166
  # return f"An error occurred: {e}", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
167
 
168
+
169
  def predict(image):
170
  if image is None:
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
 
 
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
 
196
  with torch.no_grad():
 
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()])