DawnC commited on
Commit
5c61ec2
1 Parent(s): fc2afdb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -11
app.py CHANGED
@@ -251,7 +251,6 @@ def get_akc_breeds_link():
251
 
252
 
253
  def format_description(description, breed):
254
- # 分別將不同的屬性分開來顯示,保持結果的可讀性
255
  if isinstance(description, dict):
256
  formatted_description = "\n".join([f"**{key}**: {value}" for key, value in description.items()])
257
  else:
@@ -260,10 +259,8 @@ def format_description(description, breed):
260
  formatted_description = f"""
261
  **Breed**: {breed}
262
  {formatted_description}
263
-
264
  **Want to learn more about dog breeds?**
265
  [Visit the AKC dog breeds page]({get_akc_breeds_link()}) and search for {breed} to find detailed information.
266
-
267
  *Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page.
268
  You may need to search for the specific breed on that page.
269
  I am not responsible for the content on external sites.
@@ -271,12 +268,12 @@ Please refer to the AKC's terms of use and privacy policy.*
271
  """
272
  return formatted_description
273
 
 
274
  async def predict_single_dog(image):
275
- # 使用 asyncio.to_thread 將同步操作轉換為異步
276
  return await asyncio.to_thread(_predict_single_dog, image)
277
 
278
  def _predict_single_dog(image):
279
- # 直接使用模型進行預測,無需通過 YOLO
280
  image_tensor = preprocess_image(image)
281
  with torch.no_grad():
282
  output = model(image_tensor)
@@ -289,11 +286,9 @@ def _predict_single_dog(image):
289
  return top1_prob, topk_breeds, topk_probs_percent
290
 
291
  async def detect_multiple_dogs(image):
292
- # 使用 asyncio.to_thread 將同步操作轉換為異步
293
  return await asyncio.to_thread(_detect_multiple_dogs, image)
294
 
295
  def _detect_multiple_dogs(image):
296
- # 使用 YOLO 檢測多隻狗
297
  results = model_yolo(image)
298
  dogs = []
299
  for result in results:
@@ -313,11 +308,9 @@ async def predict(image):
313
  if isinstance(image, np.ndarray):
314
  image = Image.fromarray(image)
315
 
316
- # 首先檢查圖片中是否有多隻狗
317
  dogs = await detect_multiple_dogs(image)
318
 
319
  if len(dogs) == 0:
320
- # 沒有狗或 YOLO 未檢測到狗,使用單狗直接分類
321
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
322
  if top1_prob < 0.2:
323
  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)
@@ -327,14 +320,12 @@ async def predict(image):
327
  return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
328
 
329
  if len(dogs) == 1:
330
- # 檢測到一隻狗時,直接分類不使用 YOLO 來節省時間
331
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
332
  breed = topk_breeds[0]
333
  description = get_dog_description(breed)
334
  formatted_description = format_description(description, breed)
335
  return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
336
 
337
- # 若有多隻狗,則使用 YOLO 的檢測結果來處理
338
  explanations = []
339
  visible_buttons = []
340
  annotated_image = image.copy()
@@ -382,6 +373,7 @@ async def show_details(choice):
382
  except Exception as e:
383
  return f"An error occurred while showing details: {e}"
384
 
 
385
  with gr.Blocks(css="""
386
  .container { max-width: 900px; margin: auto; padding: 20px; }
387
  .gr-box { border-radius: 15px; }
 
251
 
252
 
253
  def format_description(description, breed):
 
254
  if isinstance(description, dict):
255
  formatted_description = "\n".join([f"**{key}**: {value}" for key, value in description.items()])
256
  else:
 
259
  formatted_description = f"""
260
  **Breed**: {breed}
261
  {formatted_description}
 
262
  **Want to learn more about dog breeds?**
263
  [Visit the AKC dog breeds page]({get_akc_breeds_link()}) and search for {breed} to find detailed information.
 
264
  *Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page.
265
  You may need to search for the specific breed on that page.
266
  I am not responsible for the content on external sites.
 
268
  """
269
  return formatted_description
270
 
271
+
272
  async def predict_single_dog(image):
273
+ # 直接使用模型進行預測,無需通過 YOLO
274
  return await asyncio.to_thread(_predict_single_dog, image)
275
 
276
  def _predict_single_dog(image):
 
277
  image_tensor = preprocess_image(image)
278
  with torch.no_grad():
279
  output = model(image_tensor)
 
286
  return top1_prob, topk_breeds, topk_probs_percent
287
 
288
  async def detect_multiple_dogs(image):
 
289
  return await asyncio.to_thread(_detect_multiple_dogs, image)
290
 
291
  def _detect_multiple_dogs(image):
 
292
  results = model_yolo(image)
293
  dogs = []
294
  for result in results:
 
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
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
315
  if top1_prob < 0.2:
316
  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)
 
320
  return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
321
 
322
  if len(dogs) == 1:
 
323
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
324
  breed = topk_breeds[0]
325
  description = get_dog_description(breed)
326
  formatted_description = format_description(description, breed)
327
  return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
328
 
 
329
  explanations = []
330
  visible_buttons = []
331
  annotated_image = image.copy()
 
373
  except Exception as e:
374
  return f"An error occurred while showing details: {e}"
375
 
376
+
377
  with gr.Blocks(css="""
378
  .container { max-width: 900px; margin: auto; padding: 20px; }
379
  .gr-box { border-radius: 15px; }