YOLOv8.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. from ultralytics import YOLO
  2. class YOLOv8MFDModel(object):
  3. def __init__(self, weight, device="cpu"):
  4. self.mfd_model = YOLO(weight)
  5. if not device.startswith("cpu"):
  6. self.mfd_model.half()
  7. self.device = device
  8. def predict(self, image):
  9. mfd_res = self.mfd_model.predict(
  10. image, imgsz=1888, conf=0.25, iou=0.45, verbose=False, device=self.device
  11. )[0]
  12. return mfd_res
  13. def batch_predict(self, images: list, batch_size: int) -> list:
  14. images_mfd_res = []
  15. for index in range(0, len(images), batch_size):
  16. mfd_res = [
  17. image_res.cpu()
  18. for image_res in self.mfd_model.predict(
  19. images[index : index + batch_size],
  20. imgsz=1888,
  21. conf=0.25,
  22. iou=0.45,
  23. verbose=False,
  24. device=self.device,
  25. )
  26. ]
  27. for image_res in mfd_res:
  28. images_mfd_res.append(image_res)
  29. return images_mfd_res