• YOLOv5 onnx \tensorrt 推理


    一、yolov5 pt模型转onnx

    code:
    https://github.com/ultralytics/yolov5

    python export.py --weights yolov5s.pt --include onnx
    
    • 1

    二、onnx 推理

    import os
    import cv2
    import numpy as np
    import onnxruntime
    import time
    
    CLASSES = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
               'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
               'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
               'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
               'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
               'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
               'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
               'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
               'hair drier', 'toothbrush']  # coco80类别
    
    
    class YOLOV5():
        def __init__(self, onnxpath):
            self.onnx_session = onnxruntime.InferenceSession(onnxpath)
            self.input_name = self.get_input_name()
            self.output_name = self.get_output_name()
    
        # -------------------------------------------------------
        #   获取输入输出的名字
        # -------------------------------------------------------
        def get_input_name(self):
            input_name = []
            for node in self.onnx_session.get_inputs():
                input_name.append(node.name)
            return input_name
    
        def get_output_name(self):
            output_name = []
            for node in self.onnx_session.get_outputs():
                output_name.append(node.name)
            return output_name
    
        # -------------------------------------------------------
        #   输入图像
        # -------------------------------------------------------
        def get_input_feed(self, img_tensor):
            input_feed = {}
            for name in self.input_name:
                input_feed[name] = img_tensor
            return input_feed
    
        # -------------------------------------------------------
        #   1.cv2读取图像并resize
        #	2.图像转BGR2RGB和HWC2CHW
        #	3.图像归一化
        #	4.图像增加维度
        #	5.onnx_session 推理
        # -------------------------------------------------------
        def inference(self, img_path):
            img = cv2.imread(img_path)
            or_img = cv2.resize(img, (640, 640))
            img = or_img[:, :, ::-1].transpose(2, 0, 1)  # BGR2RGB和HWC2CHW
            img = img.astype(dtype=np.float32)
            img /= 255.0
            img = np.expand_dims(img, axis=0)
            input_feed = self.get_input_feed(img)
            pred = self.onnx_session.run(None, input_feed)[0]
            return pred, or_img
    
    
    # dets:  array [x,6] 6个值分别为x1,y1,x2,y2,score,class
    # thresh: 阈值
    def nms(dets, thresh):
        x1 = dets[:, 0]
        y1 = dets[:, 1]
        x2 = dets[:, 2]
        y2 = dets[:, 3]
        # -------------------------------------------------------
        #   计算框的面积
        #	置信度从大到小排序
        # -------------------------------------------------------
        areas = (y2 - y1 + 1) * (x2 - x1 + 1)
        scores = dets[:, 4]
        keep = []
        index = scores.argsort()[::-1]
    
        while index.size > 0:
            i = index[0]
            keep.append(i)
            # -------------------------------------------------------
            #   计算相交面积
            #	1.相交
            #	2.不相交
            # -------------------------------------------------------
            x11 = np.maximum(x1[i], x1[index[1:]])
            y11 = np.maximum(y1[i], y1[index[1:]])
            x22 = np.minimum(x2[i], x2[index[1:]])
            y22 = np.minimum(y2[i], y2[index[1:]])
    
            w = np.maximum(0, x22 - x11 + 1)
            h = np.maximum(0, y22 - y11 + 1)
    
            overlaps = w * h
            # -------------------------------------------------------
            #   计算该框与其它框的IOU,去除掉重复的框,即IOU值大的框
            #	IOU小于thresh的框保留下来
            # -------------------------------------------------------
            ious = overlaps / (areas[i] + areas[index[1:]] - overlaps)
            idx = np.where(ious <= thresh)[0]
            index = index[idx + 1]
        return keep
    
    
    def xywh2xyxy(x):
        # [x, y, w, h] to [x1, y1, x2, y2]
        y = np.copy(x)
        y[:, 0] = x[:, 0] - x[:, 2] / 2
        y[:, 1] = x[:, 1] - x[:, 3] / 2
        y[:, 2] = x[:, 0] + x[:, 2] / 2
        y[:, 3] = x[:, 1] + x[:, 3] / 2
        return y
    
    
    def filter_box(org_box, conf_thres, iou_thres):  # 过滤掉无用的框
        # -------------------------------------------------------
        #   删除为1的维度
        #	删除置信度小于conf_thres的BOX
        # -------------------------------------------------------
        org_box = np.squeeze(org_box)
        conf = org_box[..., 4] > conf_thres
        box = org_box[conf == True]
        # -------------------------------------------------------
        #	通过argmax获取置信度最大的类别
        # -------------------------------------------------------
        cls_cinf = box[..., 5:]
        cls = []
        for i in range(len(cls_cinf)):
            cls.append(int(np.argmax(cls_cinf[i])))
        all_cls = list(set(cls))
        # -------------------------------------------------------
        # 分别对每个类别进行过滤
        #	1.将第6列元素替换为类别下标
        #	2.xywh2xyxy 坐标转换
        #	3.经过非极大抑制后输出的BOX下标
        #	4.利用下标取出非极大抑制后的BOX
        # -------------------------------------------------------
        output = []
    
    
        for i in range(len(all_cls)):
            curr_cls = all_cls[i]
            curr_cls_box = []
            curr_out_box = []
            for j in range(len(cls)):
                if cls[j] == curr_cls:
                    box[j][5] = curr_cls
                    curr_cls_box.append(box[j][:6])
            curr_cls_box = np.array(curr_cls_box)
            # curr_cls_box_old = np.copy(curr_cls_box)
            curr_cls_box = xywh2xyxy(curr_cls_box)
            curr_out_box = nms(curr_cls_box, iou_thres)
            for k in curr_out_box:
                output.append(curr_cls_box[k])
        output = np.array(output)
        return output
    
    
    def draw(image, box_data):
        # -------------------------------------------------------
        #	取整,方便画框
        # -------------------------------------------------------
        boxes = box_data[..., :4].astype(np.int32)
        scores = box_data[..., 4]
        classes = box_data[..., 5].astype(np.int32)
    
        for box, score, cl in zip(boxes, scores, classes):
            top, left, right, bottom = box
            print('class: {}, score: {}'.format(CLASSES[cl], score))
            print('box coordinate left,top,right,down: [{}, {}, {}, {}]'.format(top, left, right, bottom))
    
            cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0), 2)
            cv2.putText(image, '{0} {1:.2f}'.format(CLASSES[cl], score),
                        (top, left),
                        cv2.FONT_HERSHEY_SIMPLEX,
                        0.6, (0, 0, 255), 2)
    
    
    if __name__ == "__main__":
        onnx_path = 'yolov5s.onnx'
        model = YOLOV5(onnx_path)
        output, or_img = model.inference('bus.jpg')
    
        print(output.shape)  # (1, 25200, 85)
        print(or_img.shape)  # (640, 640, 3)
        outbox = filter_box(output, 0.5, 0.5)
        draw(or_img, outbox)
        cv2.imwrite('res.jpg', or_img)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194

    在这里插入图片描述

    三、yolov5-7.0 tensorrt推理

    https://zhuanlan.zhihu.com/p/655640909

    """
    An example that uses TensorRT's Python api to make inferences video.
    """
    import ctypes
    import os
    import shutil
    import random
    import sys
    import threading
    import time
    import cv2
    import numpy as np
    import pycuda.autoinit
    import pycuda.driver as cuda
    import tensorrt as trt
    
    CONF_THRESH = 0.5
    IOU_THRESHOLD = 0.4
    LEN_ALL_RESULT = 38001
    LEN_ONE_RESULT = 38
    
    
    def get_img_path_batches(batch_size, img_dir):
        ret = []
        batch = []
        for root, dirs, files in os.walk(img_dir):
            for name in files:
                if len(batch) == batch_size:
                    ret.append(batch)
                    batch = []
                batch.append(os.path.join(root, name))
        if len(batch) > 0:
            ret.append(batch)
        return ret
    
    
    def plot_one_box(x, img, color=None, label=None, line_thickness=None):
        """
        description: Plots one bounding box on image img,
                     this function comes from YoLov5 project.
        param: 
            x:      a box likes [x1,y1,x2,y2]
            img:    a opencv image object
            color:  color to draw rectangle, such as (0,255,0)
            label:  str
            line_thickness: int
        return:
            no return
    
        """
        tl = (
                line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1
        )  # line/font thickness
        color = color or [random.randint(0, 255) for _ in range(3)]
        c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
        cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
        if label:
            tf = max(tl - 1, 1)  # font thickness
            t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
            c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
            cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA)  # filled
            cv2.putText(
                img,
                label,
                (c1[0], c1[1] - 2),
                0,
                tl / 3,
                [225, 255, 255],
                thickness=tf,
                lineType=cv2.LINE_AA,
            )
    
    
    class YoLov5TRT(object):
        """
        description: A YOLOv5 class that warps TensorRT ops, preprocess and postprocess ops.
        """
    
        def __init__(self, engine_file_path):
            # Create a Context on this device,
            self.ctx = cuda.Device(0).make_context()
            stream = cuda.Stream()
            TRT_LOGGER = trt.Logger(trt.Logger.INFO)
            runtime = trt.Runtime(TRT_LOGGER)
    
            # Deserialize the engine from file
            with open(engine_file_path, "rb") as f:
                engine = runtime.deserialize_cuda_engine(f.read())
            context = engine.create_execution_context()
    
            host_inputs = []
            cuda_inputs = []
            host_outputs = []
            cuda_outputs = []
            bindings = []
    
            for binding in engine:
                print('bingding:', binding, engine.get_binding_shape(binding))
                size = trt.volume(engine.get_binding_shape(binding)) * engine.max_batch_size
                dtype = trt.nptype(engine.get_binding_dtype(binding))
                # Allocate host and device buffers
                host_mem = cuda.pagelocked_empty(size, dtype)
                cuda_mem = cuda.mem_alloc(host_mem.nbytes)
                # Append the device buffer to device bindings.
                bindings.append(int(cuda_mem))
                # Append to the appropriate list.
                if engine.binding_is_input(binding):
                    self.input_w = engine.get_binding_shape(binding)[-1]
                    self.input_h = engine.get_binding_shape(binding)[-2]
                    host_inputs.append(host_mem)
                    cuda_inputs.append(cuda_mem)
                else:
                    host_outputs.append(host_mem)
                    cuda_outputs.append(cuda_mem)
    
            # Store
            self.stream = stream
            self.context = context
            self.engine = engine
            self.host_inputs = host_inputs
            self.cuda_inputs = cuda_inputs
            self.host_outputs = host_outputs
            self.cuda_outputs = cuda_outputs
            self.bindings = bindings
            self.batch_size = engine.max_batch_size
    
        def infer(self, raw_image_generator):
            threading.Thread.__init__(self)
            # Make self the active context, pushing it on top of the context stack.
            self.ctx.push()
            # Restore
            stream = self.stream
            context = self.context
            engine = self.engine
            host_inputs = self.host_inputs
            cuda_inputs = self.cuda_inputs
            host_outputs = self.host_outputs
            cuda_outputs = self.cuda_outputs
            bindings = self.bindings
            # Do image preprocess
            batch_image_raw = []
            batch_origin_h = []
            batch_origin_w = []
            batch_input_image = np.empty(shape=[self.batch_size, 3, self.input_h, self.input_w])
            for i, image_raw in enumerate(raw_image_generator):
                input_image, image_raw, origin_h, origin_w = self.preprocess_image(image_raw)
                batch_image_raw.append(image_raw)
                batch_origin_h.append(origin_h)
                batch_origin_w.append(origin_w)
                np.copyto(batch_input_image[i], input_image)
            batch_input_image = np.ascontiguousarray(batch_input_image)
    
            # Copy input image to host buffer
            np.copyto(host_inputs[0], batch_input_image.ravel())
            start = time.time()
            # Transfer input data  to the GPU.
            cuda.memcpy_htod_async(cuda_inputs[0], host_inputs[0], stream)
            # Run inference.
            context.execute_async(batch_size=self.batch_size, bindings=bindings, stream_handle=stream.handle)
            # Transfer predictions back from the GPU.
            cuda.memcpy_dtoh_async(host_outputs[0], cuda_outputs[0], stream)
            # Synchronize the stream
            stream.synchronize()
            end = time.time()
            # Remove any context from the top of the context stack, deactivating it.
            self.ctx.pop()
            # Here we use the first row of output in that batch_size = 1
            output = host_outputs[0]
            # Do postprocess
            for i in range(self.batch_size):
                result_boxes, result_scores, result_classid = self.post_process(
                    output[i * LEN_ALL_RESULT: (i + 1) * LEN_ALL_RESULT], batch_origin_h[i], batch_origin_w[i]
                )
                # Draw rectangles and labels on the original image
                for j in range(len(result_boxes)):
                    box = result_boxes[j]
                    plot_one_box(
                        box,
                        batch_image_raw[i],
                        label="{}:{:.2f}".format(
                            categories[int(result_classid[j])], result_scores[j],
                        ),
                    )
            return batch_image_raw, end - start
    
        def destroy(self):
            # Remove any context from the top of the context stack, deactivating it.
            self.ctx.pop()
    
        def get_raw_image(self, image):
            """
            description: Read an image from image path
            """
            # for img_path in image_path_batch:
            #     yield cv2.imread(img_path)
            yield image
    
        def get_raw_image_zeros(self, image_path_batch=None):
            """
            description: Ready data for warmup
            """
            for _ in range(self.batch_size):
                yield np.zeros([self.input_h, self.input_w, 3], dtype=np.uint8)
    
        def preprocess_image(self, raw_bgr_image):
            """
            description: Convert BGR image to RGB,
                         resize and pad it to target size, normalize to [0,1],
                         transform to NCHW format.
            param:
                input_image_path: str, image path
            return:
                image:  the processed image
                image_raw: the original image
                h: original height
                w: original width
            """
            image_raw = raw_bgr_image
            h, w, c = image_raw.shape
            image = cv2.cvtColor(image_raw, cv2.COLOR_BGR2RGB)
            # Calculate widht and height and paddings
            r_w = self.input_w / w
            r_h = self.input_h / h
            if r_h > r_w:
                tw = self.input_w
                th = int(r_w * h)
                tx1 = tx2 = 0
                ty1 = int((self.input_h - th) / 2)
                ty2 = self.input_h - th - ty1
            else:
                tw = int(r_h * w)
                th = self.input_h
                tx1 = int((self.input_w - tw) / 2)
                tx2 = self.input_w - tw - tx1
                ty1 = ty2 = 0
            # Resize the image with long side while maintaining ratio
            image = cv2.resize(image, (tw, th))
            # Pad the short side with (128,128,128)
            image = cv2.copyMakeBorder(
                image, ty1, ty2, tx1, tx2, cv2.BORDER_CONSTANT, None, (128, 128, 128)
            )
            image = image.astype(np.float32)
            # Normalize to [0,1]
            image /= 255.0
            # HWC to CHW format:
            image = np.transpose(image, [2, 0, 1])
            # CHW to NCHW format
            image = np.expand_dims(image, axis=0)
            # Convert the image to row-major order, also known as "C order":
            image = np.ascontiguousarray(image)
            return image, image_raw, h, w
    
        def xywh2xyxy(self, origin_h, origin_w, x):
            """
            description:    Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
            param:
                origin_h:   height of original image
                origin_w:   width of original image
                x:          A boxes numpy, each row is a box [center_x, center_y, w, h]
            return:
                y:          A boxes numpy, each row is a box [x1, y1, x2, y2]
            """
            y = np.zeros_like(x)
            r_w = self.input_w / origin_w
            r_h = self.input_h / origin_h
            if r_h > r_w:
                y[:, 0] = x[:, 0] - x[:, 2] / 2
                y[:, 2] = x[:, 0] + x[:, 2] / 2
                y[:, 1] = x[:, 1] - x[:, 3] / 2 - (self.input_h - r_w * origin_h) / 2
                y[:, 3] = x[:, 1] + x[:, 3] / 2 - (self.input_h - r_w * origin_h) / 2
                y /= r_w
            else:
                y[:, 0] = x[:, 0] - x[:, 2] / 2 - (self.input_w - r_h * origin_w) / 2
                y[:, 2] = x[:, 0] + x[:, 2] / 2 - (self.input_w - r_h * origin_w) / 2
                y[:, 1] = x[:, 1] - x[:, 3] / 2
                y[:, 3] = x[:, 1] + x[:, 3] / 2
                y /= r_h
    
            return y
    
        def post_process(self, output, origin_h, origin_w):
            """
            description: postprocess the prediction
            param:
                output:     A numpy likes [num_boxes,cx,cy,w,h,conf,cls_id, cx,cy,w,h,conf,cls_id, ...] 
                origin_h:   height of original image
                origin_w:   width of original image
            return:
                result_boxes: finally boxes, a boxes numpy, each row is a box [x1, y1, x2, y2]
                result_scores: finally scores, a numpy, each element is the score correspoing to box
                result_classid: finally classid, a numpy, each element is the classid correspoing to box
            """
            # Get the num of boxes detected
            num = int(output[0])
            # Reshape to a two dimentional ndarray
            pred = np.reshape(output[1:], (-1, LEN_ONE_RESULT))[:num, :]
            pred = pred[:, :6]
            # Do nms
            boxes = self.non_max_suppression(pred, origin_h, origin_w, conf_thres=CONF_THRESH, nms_thres=IOU_THRESHOLD)
            result_boxes = boxes[:, :4] if len(boxes) else np.array([])
            result_scores = boxes[:, 4] if len(boxes) else np.array([])
            result_classid = boxes[:, 5] if len(boxes) else np.array([])
            return result_boxes, result_scores, result_classid
    
        def bbox_iou(self, box1, box2, x1y1x2y2=True):
            """
            description: compute the IoU of two bounding boxes
            param:
                box1: A box coordinate (can be (x1, y1, x2, y2) or (x, y, w, h))
                box2: A box coordinate (can be (x1, y1, x2, y2) or (x, y, w, h))            
                x1y1x2y2: select the coordinate format
            return:
                iou: computed iou
            """
            if not x1y1x2y2:
                # Transform from center and width to exact coordinates
                b1_x1, b1_x2 = box1[:, 0] - box1[:, 2] / 2, box1[:, 0] + box1[:, 2] / 2
                b1_y1, b1_y2 = box1[:, 1] - box1[:, 3] / 2, box1[:, 1] + box1[:, 3] / 2
                b2_x1, b2_x2 = box2[:, 0] - box2[:, 2] / 2, box2[:, 0] + box2[:, 2] / 2
                b2_y1, b2_y2 = box2[:, 1] - box2[:, 3] / 2, box2[:, 1] + box2[:, 3] / 2
            else:
                # Get the coordinates of bounding boxes
                b1_x1, b1_y1, b1_x2, b1_y2 = box1[:, 0], box1[:, 1], box1[:, 2], box1[:, 3]
                b2_x1, b2_y1, b2_x2, b2_y2 = box2[:, 0], box2[:, 1], box2[:, 2], box2[:, 3]
    
            # Get the coordinates of the intersection rectangle
            inter_rect_x1 = np.maximum(b1_x1, b2_x1)
            inter_rect_y1 = np.maximum(b1_y1, b2_y1)
            inter_rect_x2 = np.minimum(b1_x2, b2_x2)
            inter_rect_y2 = np.minimum(b1_y2, b2_y2)
            # Intersection area
            inter_area = np.clip(inter_rect_x2 - inter_rect_x1 + 1, 0, None) * \
                         np.clip(inter_rect_y2 - inter_rect_y1 + 1, 0, None)
            # Union Area
            b1_area = (b1_x2 - b1_x1 + 1) * (b1_y2 - b1_y1 + 1)
            b2_area = (b2_x2 - b2_x1 + 1) * (b2_y2 - b2_y1 + 1)
    
            iou = inter_area / (b1_area + b2_area - inter_area + 1e-16)
    
            return iou
    
        def non_max_suppression(self, prediction, origin_h, origin_w, conf_thres=0.5, nms_thres=0.4):
            """
            description: Removes detections with lower object confidence score than 'conf_thres' and performs
            Non-Maximum Suppression to further filter detections.
            param:
                prediction: detections, (x1, y1, x2, y2, conf, cls_id)
                origin_h: original image height
                origin_w: original image width
                conf_thres: a confidence threshold to filter detections
                nms_thres: a iou threshold to filter detections
            return:
                boxes: output after nms with the shape (x1, y1, x2, y2, conf, cls_id)
            """
            # Get the boxes that score > CONF_THRESH
            boxes = prediction[prediction[:, 4] >= conf_thres]
            # Trandform bbox from [center_x, center_y, w, h] to [x1, y1, x2, y2]
            boxes[:, :4] = self.xywh2xyxy(origin_h, origin_w, boxes[:, :4])
            # clip the coordinates
            boxes[:, 0] = np.clip(boxes[:, 0], 0, origin_w - 1)
            boxes[:, 2] = np.clip(boxes[:, 2], 0, origin_w - 1)
            boxes[:, 1] = np.clip(boxes[:, 1], 0, origin_h - 1)
            boxes[:, 3] = np.clip(boxes[:, 3], 0, origin_h - 1)
            # Object confidence
            confs = boxes[:, 4]
            # Sort by the confs
            boxes = boxes[np.argsort(-confs)]
            # Perform non-maximum suppression
            keep_boxes = []
            while boxes.shape[0]:
                large_overlap = self.bbox_iou(np.expand_dims(boxes[0, :4], 0), boxes[:, :4]) > nms_thres
                label_match = boxes[0, -1] == boxes[:, -1]
                # Indices of boxes with lower confidence scores, large IOUs and matching labels
                invalid = large_overlap & label_match
                keep_boxes += [boxes[0]]
                boxes = boxes[~invalid]
            boxes = np.stack(keep_boxes, 0) if len(keep_boxes) else np.array([])
            return boxes
    
    
    class inferThread(threading.Thread):
        def __init__(self, yolov5_wrapper, image):
            threading.Thread.__init__(self)
            self.yolov5_wrapper = yolov5_wrapper
            self.image = image
    
        def run(self):
            batch_image_raw, use_time = self.yolov5_wrapper.infer(self.yolov5_wrapper.get_raw_image(self.image))
    
            # print(batch_image_raw[0].shape)
            # print((counter / (time.time() - start_time)))
            # cv2.putText(batch_image_raw[0], "FPS {0}".format(float('%.1f' % (counter / (time.time() - start_time)))),
            #             cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255),
            #             2)
    
            cv2.imshow('img', batch_image_raw[0])
    
            # for i, img_path in enumerate(self.image):
            #     parent, filename = os.path.split(img_path)
            #     save_name = os.path.join('output', filename)
            #     # Save image
            #     cv2.imwrite(save_name, batch_image_raw[i])
    
            cv2.waitKey(0)
    
    
    class warmUpThread(threading.Thread):
        def __init__(self, yolov5_wrapper):
            threading.Thread.__init__(self)
            self.yolov5_wrapper = yolov5_wrapper
    
        def run(self):
            batch_image_raw, use_time = self.yolov5_wrapper.infer(self.yolov5_wrapper.get_raw_image_zeros())
            print('warm_up->{}, time->{:.2f}ms'.format(batch_image_raw[0].shape, use_time * 1000))
    
    
    if __name__ == "__main__":
        # load custom plugin and engine
        PLUGIN_LIBRARY = "build/libmyplugins.so"
        engine_file_path = "build/yolov5s.engine"
    
        if len(sys.argv) > 1:
            engine_file_path = sys.argv[1]
        if len(sys.argv) > 2:
            PLUGIN_LIBRARY = sys.argv[2]
    
        ctypes.CDLL(PLUGIN_LIBRARY)
    
        # load coco labels
    
        categories = ["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat",
                      "traffic light",
                      "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
                      "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase",
                      "frisbee",
                      "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard",
                      "surfboard",
                      "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
                      "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
                      "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard",
                      "cell phone",
                      "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
                      "teddy bear",
                      "hair drier", "toothbrush"]
    
        if os.path.exists('output/'):
            shutil.rmtree('output/')
        os.makedirs('output/')
        # a YoLov5TRT instance
        yolov5_wrapper = YoLov5TRT(engine_file_path)
        try:
            print('batch size is', yolov5_wrapper.batch_size)
    
            # image_dir = "images/"
            # image_path_batches = get_img_path_batches(yolov5_wrapper.batch_size, image_dir)
            # print(yolov5_wrapper.batch_size)  # 1
            # print(image_path_batches)
    
            for i in range(10):
                # create a new thread to do warm_up
                thread1 = warmUpThread(yolov5_wrapper)
                thread1.start()
                thread1.join()
            # for batch in image_path_batches:
            #     print('........', batch)  # ........ ['images/zidane.jpg']
            # create a new thread to do inference
    
            cap = cv2.VideoCapture('1.mp4')
            # start_time = time.time()
            # counter = 0
            while True:
                ret, frame = cap.read()
    
                # counter += 1
                if not ret:
                    break
                else:
    
                    # thread1 = inferThread(yolov5_wrapper, frame)
                    # thread1.start()
                    # thread1.join()
    
                    batch_image_raw, use_time = yolov5_wrapper.infer(yolov5_wrapper.get_raw_image(frame))
    
                    # print(batch_image_raw[0].shape)
                    # print(use_time)
    
                    print('input one frame cost time->{:.2f}ms, saving into output/'.format(use_time * 1000))
    
                    cv2.imshow('img', batch_image_raw[0])
    
                    # counter = 0
                    # start_time = time.time()
                    cv2.waitKey(1)
        finally:
            # destroy the instance
            yolov5_wrapper.destroy()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
  • 相关阅读:
    Linux下的网络设备驱动
    UE4c++ ConvertActorsToStaticMesh & ConvertProceduralMeshToStaticMesh
    C++结构体定义 & 创建 & 赋值 & 结构体数组
    二叉树根节点到叶子节点的所有路径和
    stable-diffusion-webui之api
    如何写好一篇技术型文档?
    Linux 运维随笔
    考研信息管理系统
    MFC Windows 程序设计[337]之历史下拉列表框(附源码)
    mediapipe 实现姿态分析——举手检测
  • 原文地址:https://blog.csdn.net/qq_27481087/article/details/134055291