yolov8+deepsort实现目标跟踪

yolov8+deepsort实现目标跟踪

一、yolov8训练行人数据集

使用官方提供的项目代码和widerperon数据集
在这里插入图片描述

二、deepsort进行跟踪

deep_sort_pytorch项目文件添加到yolov8项目根目录中,添加训练权重文件,测试视频等,创建detect.py文件对行人视频进行跟踪
部分代码如下:



def process_frame(frame):
    results = model(frame)

    bbox_xyxy = []
    confidences = []
    oids = []

    for result in results:
        boxes = result.boxes.xyxy.cpu().numpy() 
        scores = result.boxes.conf.cpu().numpy()  
        classes = result.boxes.cls.cpu().numpy()  

        bbox_xyxy.extend(boxes)
        confidences.extend(scores)
        oids.extend(classes)

    names = ['person']  

    # Placeholder for the detection results
    bbox_xyxy = np.array(bbox_xyxy)
    confidences = np.array(confidences)
    oids = np.array(oids)


    xywh_bboxs = [xyxy_to_xywh(*bbox) for bbox in bbox_xyxy]
    xywh_bboxs = np.array(xywh_bboxs)

    detections = deepsort.update(xywh_bboxs, confidences, oids, frame)

    if len(detections) > 0:
        bbox_xyxy = detections[:, :4]
        identities = detections[:, -2]
        object_id = detections[:, -1]
        frame = draw_boxes(frame, bbox_xyxy, names, object_id, identities)

    return frame


def main(video_source):
    init_tracker()
    cap = cv2.VideoCapture(video_source)

    fps = int(cap.get(cv2.CAP_PROP_FPS))
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    # Define the codec and create VideoWriter object
    out = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*'XVID'), fps, (width, height))


    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break

        frame = process_frame(frame)

        out.write(frame)

        cv2.imshow('Frame', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    out.release()
    cv2.destroyAllWindows()


if __name__ == "__main__":
    model = YOLO('./runs/train/weights/best.pt') 
    video_source = 'datasets/people.mp4' 
    main(video_source)


在这里插入图片描述

参考:https://github.com/MuhammadMoinFaisal/YOLOv8-DeepSORT-Object-Tracking

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值