YOLOv8.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  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), desc="MFD Predict"):
  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