YOLOv8目标检测:使用ONNX模型进行推理_树莓派yolov8 onnx部署

本文详细介绍了使用YOLOv8模型进行物体检测的过程,包括图像预处理、模型推理、后处理(NMS)、GPU/CPU判断以及在图像上标注检测结果。内容涉及从模型初始化到实际应用的全流程技术细节。
摘要由CSDN通过智能技术生成

classes = {0: ‘person’, 1: ‘bicycle’, 2: ‘car’, 3: ‘motorcycle’, 4: ‘airplane’, 5: ‘bus’, 6: ‘train’, 7: ‘truck’,
8: ‘boat’, 9: ‘traffic light’, 10: ‘fire hydrant’, 11: ‘stop sign’, 12: ‘parking meter’, 13: ‘bench’,
14: ‘bird’, 15: ‘cat’, 16: ‘dog’, 17: ‘horse’, 18: ‘sheep’, 19: ‘cow’, 20: ‘elephant’, 21: ‘bear’,
22: ‘zebra’, 23: ‘giraffe’, 24: ‘backpack’, 25: ‘umbrella’, 26: ‘handbag’, 27: ‘tie’, 28: ‘suitcase’,
29: ‘frisbee’, 30: ‘skis’, 31: ‘snowboard’, 32: ‘sports ball’, 33: ‘kite’, 34: ‘baseball bat’,
35: ‘baseball glove’, 36: ‘skateboard’, 37: ‘surfboard’, 38: ‘tennis racket’, 39: ‘bottle’,
40: ‘wine glass’, 41: ‘cup’, 42: ‘fork’, 43: ‘knife’, 44: ‘spoon’, 45: ‘bowl’, 46: ‘banana’, 47: ‘apple’,
48: ‘sandwich’, 49: ‘orange’, 50: ‘broccoli’, 51: ‘carrot’, 52: ‘hot dog’, 53: ‘pizza’, 54: ‘donut’,
55: ‘cake’, 56: ‘chair’, 57: ‘couch’, 58: ‘potted plant’, 59: ‘bed’, 60: ‘dining table’, 61: ‘toilet’,
62: ‘tv’, 63: ‘laptop’, 64: ‘mouse’, 65: ‘remote’, 66: ‘keyboard’, 67: ‘cell phone’, 68: ‘microwave’,
69: ‘oven’, 70: ‘toaster’, 71: ‘sink’, 72: ‘refrigerator’, 73: ‘book’, 74: ‘clock’, 75: ‘vase’,
76: ‘scissors’, 77: ‘teddy bear’, 78: ‘hair drier’, 79: ‘toothbrush’}

随机颜色

color_palette = np.random.uniform(100, 255, size=(len(classes), 3))

判断是使用GPU或CPU

providers = [
(‘CUDAExecutionProvider’, {
‘device_id’: 0, # 可以选择GPU设备ID,如果你有多个GPU
}),
‘CPUExecutionProvider’, # 也可以设置CPU作为备选
]

def calculate_iou(box, other_boxes):
“”"
计算给定边界框与一组其他边界框之间的交并比(IoU)。

参数:

  • box: 单个边界框,格式为 [x1, y1, width, height]。
  • other_boxes: 其他边界框的数组,每个边界框的格式也为 [x1, y1, width, height]。

返回值:

  • iou: 一个数组,包含给定边界框与每个其他边界框的IoU值。
    “”"

    计算交集的左上角坐标

    x1 = np.maximum(box[0], np.array(other_boxes)[:, 0])
    y1 = np.maximum(box[1], np.array(other_boxes)[:, 1])

    计算交集的右下角坐标

    x2 = np.minimum(box[0] + box[2], np.array(other_boxes)[:, 0] + np.array(other_boxes)[:, 2])
    y2 = np.minimum(box[1] + box[3], np.array(other_boxes)[:, 1] + np.array(other_boxes)[:, 3])

    计算交集区域的面积

    intersection_area = np.maximum(0, x2 - x1) * np.maximum(0, y2 - y1)

    计算给定边界框的面积

    box_area = box[2] * box[3]

    计算其他边界框的面积

    other_boxes_area = np.array(other_boxes)[:, 2] * np.array(other_boxes)[:, 3]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值