what.models.detection.yolo.yolov4

 1import cv2
 2import numpy as np
 3
 4from keras.models import load_model
 5import tensorflow.keras.backend as K
 6
 7from what.models.detection.utils.time_utils import Timer
 8
 9from .utils.yolo_utils import yolo_process_output, yolov4_anchors
10
11def mish(x):
12    return x * K.tanh(K.softplus(x))
13
14class YOLOV4:
15    def __init__(self, class_names, model_path):
16        self.model = load_model(model_path, custom_objects = {
17            'mish': mish
18        })
19        self.class_names = class_names
20        self.timer = Timer()
21
22    def predict(self, image, top_k=-1, prob_threshold=None):
23        input_cv_image = cv2.resize(image, (416, 416))
24        input_cv_image = np.array(input_cv_image).astype(np.float32) / 255.0
25
26        # Yolo inference
27        self.timer.start()
28        outs = self.model.predict(np.array([input_cv_image]))
29        print("FPS: ", int(1.0 / self.timer.end()))
30
31        boxes, class_ids, confidences = yolo_process_output(outs, yolov4_anchors, len(self.class_names))
32
33        return input_cv_image, boxes, class_ids, confidences
def mish(x):
12def mish(x):
13    return x * K.tanh(K.softplus(x))
class YOLOV4:
15class YOLOV4:
16    def __init__(self, class_names, model_path):
17        self.model = load_model(model_path, custom_objects = {
18            'mish': mish
19        })
20        self.class_names = class_names
21        self.timer = Timer()
22
23    def predict(self, image, top_k=-1, prob_threshold=None):
24        input_cv_image = cv2.resize(image, (416, 416))
25        input_cv_image = np.array(input_cv_image).astype(np.float32) / 255.0
26
27        # Yolo inference
28        self.timer.start()
29        outs = self.model.predict(np.array([input_cv_image]))
30        print("FPS: ", int(1.0 / self.timer.end()))
31
32        boxes, class_ids, confidences = yolo_process_output(outs, yolov4_anchors, len(self.class_names))
33
34        return input_cv_image, boxes, class_ids, confidences
YOLOV4(class_names, model_path)
16    def __init__(self, class_names, model_path):
17        self.model = load_model(model_path, custom_objects = {
18            'mish': mish
19        })
20        self.class_names = class_names
21        self.timer = Timer()
def predict(self, image, top_k=-1, prob_threshold=None):
23    def predict(self, image, top_k=-1, prob_threshold=None):
24        input_cv_image = cv2.resize(image, (416, 416))
25        input_cv_image = np.array(input_cv_image).astype(np.float32) / 255.0
26
27        # Yolo inference
28        self.timer.start()
29        outs = self.model.predict(np.array([input_cv_image]))
30        print("FPS: ", int(1.0 / self.timer.end()))
31
32        boxes, class_ids, confidences = yolo_process_output(outs, yolov4_anchors, len(self.class_names))
33
34        return input_cv_image, boxes, class_ids, confidences