YOLOv8.py 1.09 KB
Newer Older
1
from tqdm import tqdm
2
3
4
5
from ultralytics import YOLO


class YOLOv8MFDModel(object):
6
    def __init__(self, weight, device="cpu"):
7
8
9
10
        self.mfd_model = YOLO(weight)
        self.device = device

    def predict(self, image):
11
        mfd_res = self.mfd_model.predict(
12
            image, imgsz=1888, conf=0.25, iou=0.45, verbose=False, device=self.device
13
        )[0]
14
15
        return mfd_res

16
17
    def batch_predict(self, images: list, batch_size: int) -> list:
        images_mfd_res = []
18
        # for index in range(0, len(images), batch_size):
19
        for index in tqdm(range(0, len(images), batch_size), desc="MFD Predict"):
20
21
22
23
24
25
26
            mfd_res = [
                image_res.cpu()
                for image_res in self.mfd_model.predict(
                    images[index : index + batch_size],
                    imgsz=1888,
                    conf=0.25,
                    iou=0.45,
27
                    verbose=False,
28
29
30
                    device=self.device,
                )
            ]
31
32
33
            for image_res in mfd_res:
                images_mfd_res.append(image_res)
        return images_mfd_res