DawnC commited on
Commit
dd99f2c
1 Parent(s): a46c623

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py CHANGED
@@ -272,6 +272,34 @@ Please refer to the AKC's terms of use and privacy policy.*
272
  """
273
  return formatted_description
274
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  def predict(image):
276
  if image is None:
277
  return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
272
  """
273
  return formatted_description
274
 
275
+ def predict_single_dog(image):
276
+ # 直接使用模型進行預測,無需通過 YOLO
277
+ image_tensor = preprocess_image(image)
278
+ with torch.no_grad():
279
+ output = model(image_tensor)
280
+ logits = output[0] if isinstance(output, tuple) else output
281
+ probabilities = F.softmax(logits, dim=1)
282
+ topk_probs, topk_indices = torch.topk(probabilities, k=3)
283
+ top1_prob = topk_probs[0][0].item()
284
+ topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
285
+ topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
286
+ return top1_prob, topk_breeds, topk_probs_percent
287
+
288
+
289
+ def detect_multiple_dogs(image):
290
+ # 使用 YOLO 檢測多隻狗
291
+ results = model_yolo(image)
292
+ dogs = []
293
+ for result in results:
294
+ for box in result.boxes:
295
+ if box.cls == 16: # COCO 資料集中狗的類別是 16
296
+ xyxy = box.xyxy[0].tolist()
297
+ confidence = box.conf.item()
298
+ cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
299
+ dogs.append((cropped_image, confidence, xyxy))
300
+ return dogs
301
+
302
+
303
  def predict(image):
304
  if image is None:
305
  return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)