YOLOv8.py 1.2 KB

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