当前位置:   article > 正文

目标跟踪实验(计算机视觉)通过python 语言编程设计跟踪算法,实现目标行人的选择,跟踪。促进学生理解并掌握相关跟踪算法,培养学生的编程能力。_行人追踪算法

行人追踪算法

一、实验目的

通过python 语言编程设计跟踪算法,实现目标行人的选择,跟踪。促进学生理解并掌握相关跟踪算法,培养学生的编程能力。

二、实验硬、软件环境

PC、windows10系统,Visual Studio Code编辑器,opencv视觉库,numpy库, matplolib库等。

三、实验内容及步骤

(一)实验内容 

编程实现目标行人的跟踪。

  • 实验过程

1. 读取视频或打开摄像头进行实时录像。

  1. tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'MOSSE', 'CSRT']
  2.     tracker_type = tracker_types[6]
  3.     if tracker_type == 'BOOSTING':
  4.         tracker = cv2.legacy.TrackerBoosting_create()
  5.     if tracker_type == 'MIL':
  6.         tracker = cv2.TrackerMIL_create()
  7.     if tracker_type == 'KCF':
  8.         tracker = cv2.TrackerKCF_create()
  9.     if tracker_type == 'TLD':
  10.         tracker = cv2.legacy.TrackerTLD_create()
  11.     if tracker_type == 'MEDIANFLOW':
  12.         tracker = cv2.legacy.TrackerMedianFlow_create()
  13.     if tracker_type == "CSRT":
  14.         tracker = cv2.legacy.TrackerCSRT_create()
  15.     if tracker_type == "MOSSE":
  16.         tracker = cv2.legacy.TrackerMOSSE_create()
  17. video = cv2.VideoCapture("D:/XiaoStudy/JiSuanJiShiJue/jisuanjishijue/shiyan3/vtest.mp4")
  18.     if not video.isOpened():
  19.         print("Could not open video")
  20.         sys.exit()
  21.     ok, frame = video.read()
  22.     if not ok:
  23.         print('Cannot read video file')
  24.         sys.exit()

  1. 人工选择跟踪目标

bbox = (287, 23, 86, 320)

    bbox = cv2.selectROI(frame, False)

    ok = tracker.init(frame, bbox)

目标行人选择算法,即人工选择方框的方法。

3. 选择合适的跟踪算法对目标进行跟踪。

while True:

        # Read a new frame

        ok, frame = video.read()

        if not ok:

            break

        timer = cv2.getTickCount()

        ok, bbox = tracker.update(frame)

        fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);

        if ok:

            p1 = (int(bbox[0]), int(bbox[1]))

            p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))

            cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)

        else :

            cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)

        cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);

        cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);

4. 记录目标行人跟踪过程中的跟踪目标图像。

记录的图像,行人遮挡时的图像,进行对比分析

附(记得cmd安装pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-contrib-python)

源代码(记得该视频路径):

  1. import cv2
  2. import sys
  3. import argparse
  4. if __name__ == '__main__' :
  5. # Set up tracker.
  6. # Instead of MIL, you can also use
  7. tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'MOSSE', 'CSRT']
  8. tracker_type = tracker_types[6]
  9. ####OpenCv的contrib modules在4.5.1之后不再支持 部分跟踪方法
  10. # #迁移到cv2.legacy.Multitracker_create。加上“.legacy”即可
  11. if tracker_type == 'BOOSTING':
  12. tracker = cv2.legacy.TrackerBoosting_create()
  13. if tracker_type == 'MIL':
  14. tracker = cv2.TrackerMIL_create()
  15. if tracker_type == 'KCF':
  16. tracker = cv2.TrackerKCF_create()
  17. if tracker_type == 'TLD':
  18. tracker = cv2.legacy.TrackerTLD_create()
  19. if tracker_type == 'MEDIANFLOW':
  20. tracker = cv2.legacy.TrackerMedianFlow_create()
  21. if tracker_type == "CSRT":
  22. tracker = cv2.legacy.TrackerCSRT_create()
  23. if tracker_type == "MOSSE":
  24. tracker = cv2.legacy.TrackerMOSSE_create()
  25. # Read video 获取视频
  26. video = cv2.VideoCapture("D:/XiaoStudy/JiSuanJiShiJue/jisuanjishijue/shiyan3/vtest.mp4")
  27. # Exit if video not opened.
  28. if not video.isOpened():
  29. print("Could not open video")
  30. sys.exit()
  31. # Read first frame.
  32. ok, frame = video.read()
  33. if not ok:
  34. print('Cannot read video file')
  35. sys.exit()
  36. # Define an initial bounding box 定义初始化的边界框
  37. bbox = (287, 23, 86, 320)
  38. # Uncomment the line below to select a different bounding box 取消下面一行的注释,选择一个不同的边界框架
  39. bbox = cv2.selectROI(frame, False)
  40. # Initialize tracker with first frame and bounding box 用第一帧和包围框初始化跟踪边界框架
  41. ok = tracker.init(frame, bbox)
  42. while True:
  43. # Read a new frame
  44. ok, frame = video.read()
  45. if not ok:
  46. break
  47. # Start timer 开始计时
  48. timer = cv2.getTickCount()
  49. # Update tracker 更新跟踪信号
  50. ok, bbox = tracker.update(frame)
  51. # Calculate Frames per second (FPS) 计算每秒帧数(FPS)
  52. fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);
  53. # Draw bounding box 绘制边界框
  54. if ok:
  55. # Tracking success 跟踪成功
  56. p1 = (int(bbox[0]), int(bbox[1]))
  57. p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
  58. cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)
  59. else :
  60. # Tracking failure 跟踪失败
  61. cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)
  62. # Display tracker type on frame 在帧上显示跟踪器类型
  63. cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
  64. # Display FPS on frame 帧显示FPS
  65. cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
  66. # Display result 显示结果
  67. cv2.imshow("Tracking", frame)
  68. # Exit if ESC pressed
  69. k = cv2.waitKey(1) & 0xff
  70. if k == 27 : break

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/78506
推荐阅读
相关标签
  

闽ICP备14008679号