• 应用DeepSORT实现目标跟踪


    在ByteTrack被提出之前,可以说DeepSORT是最好的目标跟踪算法之一。本文,我们就来应用这个算法实现目标跟踪。

    DeepSORT的官方网址是https://github.com/nwojke/deep_sort。但在这里,我们不使用官方的代码,而使用第三方代码,其网址为https://github.com/levan92/deep_sort_realtime。

    下面我们就来应用DeepSORT。首先在虚拟环境内安装必要的软件包:

    1. conda install python=3.8
    2. pip install deep-sort-realtime

    可以看出,DeepSORT算法只是需要几个常规的软件包:numpy、scipy和opencv-python,对用户十分友好。

    使用DeepSORT也很方便,先导入DeepSORT:

    from deep_sort_realtime.deepsort_tracker import DeepSort

    实例化:

    tracker = DeepSort()

    DeepSort有一些输入参数,在这里只介绍几个常用的参数:

    max_iou_distance:IoU的门控阈值,大于该值的关联会被忽略,默认值为0.7

    max_age:当遗漏次数大于该值时轨迹会被删除,默认值为30

    n_init:在初始阶段轨迹被保留的帧数,默认值为3

    nms_max_overlap:非最大值抑制阈值,如果该值为1.0,表示不使用非最大值抑制,默认值为1.0

    max_cosine_distance:余弦距离阈值,默认值为0.2

    nn_budget:外观描述符的最大尺寸(int类型),如果为None,则不强制执行,默认值为None

    实现目标跟踪

    tracks = tracker.update_tracks(bbs, frame=frame)

    bbs为目标检测器的结果列表,每个结果是一个元组,形式为([left,top,w,h],置信值,类型),其中类型为字符串型

    frame为帧图像

    输出tracks为目标跟踪结果,使用for循环可以得到各个目标的跟踪信息:

    for track in tracks:

    下面介绍一些track的常用属性和方法:

    track_id:目标ID

    orginal_ltwh、det_conf、det_class:分别表示目标边框信息、置信值和类型,这三个值都是由tracker.update_tracks传入系统的原始目标的信息,但此时已匹配上了目标ID

    to_ltrb()和to_ltwh():得到目标边框信息,两者的形式不同

    is_confirmed():表示如果该目标ID被确认,则返回True

    下面我们就给出DeepSORT实现目标跟踪的完整程序,在这里,我们仍然使用YOLOv8作为目标检测器:

    1. import numpy as np
    2. import cv2
    3. from ultralytics import YOLO
    4. from deep_sort_realtime.deepsort_tracker import DeepSort
    5. model = YOLO('yolov8l.pt')
    6. cap = cv2.VideoCapture("D:/track/Highway Traffic.mp4")
    7. fps = cap.get(cv2.CAP_PROP_FPS)
    8. size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    9. fNUMS = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    10. fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    11. videoWriter = cv2.VideoWriter("D:/track/mytrack.mp4", fourcc, fps, size)
    12. tracker = DeepSort(max_age=5)
    13. def box_label(image, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)):
    14. p1, p2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
    15. cv2.rectangle(image, p1, p2, color, thickness=1, lineType=cv2.LINE_AA)
    16. if label:
    17. w, h = cv2.getTextSize(label, 0, fontScale=2 / 3, thickness=1)[0]
    18. outside = p1[1] - h >= 3
    19. p2 = p1[0] + w, p1[1] - h - 3 if outside else p1[1] + h + 3
    20. cv2.rectangle(image, p1, p2, color, -1, cv2.LINE_AA)
    21. cv2.putText(image,
    22. label, (p1[0], p1[1] - 2 if outside else p1[1] + h + 2),
    23. 0, 2/3, txt_color, thickness=1, lineType=cv2.LINE_AA)
    24. while cap.isOpened():
    25. success, frame = cap.read()
    26. if success:
    27. results = model(frame,conf=0.4)
    28. outputs = results[0].boxes.data.cpu().numpy()
    29. detections = []
    30. if outputs is not None:
    31. for output in outputs:
    32. x1, y1, x2, y2 = list(map(int, output[:4]))
    33. if output[5] == 2:
    34. detections.append(([x1, y1, int(x2-x1), int(y2-y1)], output[4], 'car'))
    35. elif output[5] == 5:
    36. detections.append(([x1, y1, int(x2-x1), int(y2-y1)], output[4], 'bus'))
    37. elif output[5] == 7:
    38. detections.append(([x1, y1, int(x2-x1), int(y2-y1)], output[4], 'truck'))
    39. tracks = tracker.update_tracks(detections, frame=frame)
    40. for track in tracks:
    41. if not track.is_confirmed():
    42. continue
    43. track_id = track.track_id
    44. bbox = track.to_ltrb()
    45. box_label(frame, bbox, '#'+str(int(track_id))+ track.det_class , (167, 146, 11))
    46. cv2.putText(frame, "https://blog.csdn.net/zhaocj", (25, 50),
    47. cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
    48. cv2.imshow("YOLOv8 Tracking", frame)
    49. videoWriter.write(frame)
    50. if cv2.waitKey(1) & 0xFF == ord("q"):
    51. break
    52. else:
    53. break
    54. cap.release()
    55. videoWriter.release()
    56. cv2.destroyAllWindows()

  • 相关阅读:
    工业交换机在安全方面有哪些注意事项?
    【Linux操作系统】:基本指令
    【python版CV】-直方图 & 傅里叶变换
    es部署保姆教程
    应用商店优化之关键词优化指南1
    【SA8295P 源码分析 (二)】109 - QNX 如何实现显示图片到 Screen 显示屏上
    ORB(Oriented FAST and Rotated BRIEF)
    LNMP架构下部署Discuz!社区论坛与Wordpress博客
    基于matlab求两个数最大公约数函数gcd
    CSP-J初赛复习大题整理笔记
  • 原文地址:https://blog.csdn.net/zhaocj/article/details/133686871