赞
踩
通过python 语言编程设计跟踪算法,实现目标行人的选择,跟踪。促进学生理解并掌握相关跟踪算法,培养学生的编程能力。
PC、windows10系统,Visual Studio Code编辑器,opencv视觉库,numpy库, matplolib库等。
(一)实验内容
编程实现目标行人的跟踪。
1. 读取视频或打开摄像头进行实时录像。
- tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'MOSSE', 'CSRT']
-
- tracker_type = tracker_types[6]
-
- if tracker_type == 'BOOSTING':
-
- tracker = cv2.legacy.TrackerBoosting_create()
-
- if tracker_type == 'MIL':
-
- tracker = cv2.TrackerMIL_create()
-
- if tracker_type == 'KCF':
-
- tracker = cv2.TrackerKCF_create()
-
- if tracker_type == 'TLD':
-
- tracker = cv2.legacy.TrackerTLD_create()
-
- if tracker_type == 'MEDIANFLOW':
-
- tracker = cv2.legacy.TrackerMedianFlow_create()
-
- if tracker_type == "CSRT":
-
- tracker = cv2.legacy.TrackerCSRT_create()
-
- if tracker_type == "MOSSE":
-
- tracker = cv2.legacy.TrackerMOSSE_create()
-
- video = cv2.VideoCapture("D:/XiaoStudy/JiSuanJiShiJue/jisuanjishijue/shiyan3/vtest.mp4")
-
- if not video.isOpened():
-
- print("Could not open video")
-
- sys.exit()
-
- ok, frame = video.read()
-
- if not ok:
-
- print('Cannot read video file')
-
- sys.exit()

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)
源代码(记得该视频路径):
- import cv2
- import sys
- import argparse
-
- if __name__ == '__main__' :
-
- # Set up tracker.
- # Instead of MIL, you can also use
-
- tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'MOSSE', 'CSRT']
- tracker_type = tracker_types[6]
-
- ####OpenCv的contrib modules在4.5.1之后不再支持 部分跟踪方法
- # #迁移到cv2.legacy.Multitracker_create。加上“.legacy”即可
- if tracker_type == 'BOOSTING':
- tracker = cv2.legacy.TrackerBoosting_create()
- if tracker_type == 'MIL':
- tracker = cv2.TrackerMIL_create()
- if tracker_type == 'KCF':
- tracker = cv2.TrackerKCF_create()
- if tracker_type == 'TLD':
- tracker = cv2.legacy.TrackerTLD_create()
- if tracker_type == 'MEDIANFLOW':
- tracker = cv2.legacy.TrackerMedianFlow_create()
- if tracker_type == "CSRT":
- tracker = cv2.legacy.TrackerCSRT_create()
- if tracker_type == "MOSSE":
- tracker = cv2.legacy.TrackerMOSSE_create()
- # Read video 获取视频
- video = cv2.VideoCapture("D:/XiaoStudy/JiSuanJiShiJue/jisuanjishijue/shiyan3/vtest.mp4")
-
- # Exit if video not opened.
- if not video.isOpened():
- print("Could not open video")
- sys.exit()
-
- # Read first frame.
- ok, frame = video.read()
- if not ok:
- print('Cannot read video file')
- sys.exit()
-
- # Define an initial bounding box 定义初始化的边界框
- bbox = (287, 23, 86, 320)
-
- # Uncomment the line below to select a different bounding box 取消下面一行的注释,选择一个不同的边界框架
- bbox = cv2.selectROI(frame, False)
-
- # Initialize tracker with first frame and bounding box 用第一帧和包围框初始化跟踪边界框架
- ok = tracker.init(frame, bbox)
-
- while True:
- # Read a new frame
- ok, frame = video.read()
- if not ok:
- break
-
- # Start timer 开始计时
- timer = cv2.getTickCount()
-
- # Update tracker 更新跟踪信号
- ok, bbox = tracker.update(frame)
-
- # Calculate Frames per second (FPS) 计算每秒帧数(FPS)
- fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);
-
- # Draw bounding box 绘制边界框
- if ok:
- # Tracking success 跟踪成功
- 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 :
- # Tracking failure 跟踪失败
- cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)
-
- # Display tracker type on frame 在帧上显示跟踪器类型
- cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
-
- # Display FPS on frame 帧显示FPS
- cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
-
-
- # Display result 显示结果
- cv2.imshow("Tracking", frame)
-
- # Exit if ESC pressed
- k = cv2.waitKey(1) & 0xff
- if k == 27 : break

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。