YOLOv8.py 1008 B

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