文章目录
- 1、功能描述
- 2、模型介绍
- 3、代码实现
- 4、完整代码
- 5、结果展示
- 6、优缺点分析
- 7、参考
1、功能描述
基于 Generic Object Tracking using Regression Networks 方法,实现单目标跟踪
2、模型介绍
(1)发表来自
Held D, Thrun S, Savarese S. Learning to track at 100 fps with deep regression networks[C]//Computer Vision–ECCV 2016: 14th European Conference, Amsterdam, The Netherlands, October 11–14, 2016, Proceedings, Part I 14. Springer International Publishing, 2016: 749-765.
(2)基本原理
(3)模型大小
(4)模型结构
3、代码实现
# Import modules
import cv2, sys, os
num = 0
if not (os.path.isfile('goturn.caffemodel') and os.path.isfile('goturn.prototxt')):
errorMsg = '''
Could not find GOTURN model in current directory.
Please ensure goturn.caffemodel and goturn.prototxt are in the current directory
'''
print(errorMsg)
sys.exit()
导入必要的库函数,判断 caffe 模型文件是否存在,模型下载地址见本博客最后的参考
# Create tracker
tracker = cv2.TrackerGOTURN_create()
# Read video
video = cv2.VideoCapture("tracking2.mkv")
# Exit if video not opened
if not video.isOpened():
print("Could not open video")
sys.exit()
# Read first frame
ok, frame = video.read()
# cv2.imwrite("first.jpg", frame)
# ok = False
if not ok:
print("Cannot read video file")
sys.exit()
创建跟踪器,读取视频第一帧
# Define a bounding box
# bbox = (25, 65, 35, 90) # 左上坐标宽高
bbox = (90, 85, 50, 105) # 左上坐标宽高
# 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)
用第一帧初始化跟踪器,作为待跟踪的模板,bbox 配置是目标左上角的横纵坐标,以及目标的宽和高
while True:
num += 1
# 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 = 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, (0, 0, 255), 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, "GOTURN Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);
# Display FPS on frame
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)
cv2.imwrite(f"{str(num).zfill(3)}.jpg", frame)
# Exit if ESC pressed
k = cv2.waitKey(1) & 0xff
if k == 27:
break
遍历视频中的图片,ok, bbox = tracker.update(frame)
跟踪,绘制帧率、跟踪器,保存、显示每一帧结果,ESC
键退出
4、完整代码
# Import modules
import cv2, sys, os
num = 0
if not (os.path.isfile('goturn.caffemodel') and os.path.isfile('goturn.prototxt')):
errorMsg = '''
Could not find GOTURN model in current directory.
Please ensure goturn.caffemodel and goturn.prototxt are in the current directory
'''
print(errorMsg)
sys.exit()
# Create tracker
tracker = cv2.TrackerGOTURN_create()
# Read video
video = cv2.VideoCapture("tracking2.mkv")
# Exit if video not opened
if not video.isOpened():
print("Could not open video")
sys.exit()
# Read first frame
ok, frame = video.read()
# cv2.imwrite("first.jpg", frame)
# ok = False
if not ok:
print("Cannot read video file")
sys.exit()
# Define a bounding box
# bbox = (25, 65, 35, 90) # 左上坐标宽高
bbox = (90, 85, 50, 105) # 左上坐标宽高
# 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:
num += 1
# 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 = 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, (0, 0, 255), 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, "GOTURN Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);
# Display FPS on frame
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)
cv2.imwrite(f"{str(num).zfill(3)}.jpg", frame)
# Exit if ESC pressed
k = cv2.waitKey(1) & 0xff
if k == 27:
break
5、结果展示
未设置成循环播放,可刷新网页循环播放
这个一开始就跟丢了
这个效果还差不多,不过人头一直没有跟踪上,后面也慢慢的飘了
6、优缺点分析
GOTURN是一种基于深度学习的对象跟踪算法
一、优点
-
高速度:GOTURN算法能够以非常高的速度运行,在Caffe的GPU上可以实现100fps(帧每秒)的速度,在OpenCV的CPU上也能达到20fps的速度。这使得GOTURN在处理实时视频跟踪任务时具有很高的效率。
-
离线训练:GOTURN算法利用了大量数据进行离线训练,这使得它在跟踪未见过的类别样例时也能表现出较好的效果。同时,由于不需要在线微调网络参数,只需要一次前向传播就能得到目标位置,因此跟踪速度非常快。
-
鲁棒性:GOTURN算法对视角、形变和光照变化具有一定的鲁棒性。这意味着在目标发生一定的形变、视角变化或光照变化时,算法仍然能够准确地跟踪目标。
-
泛化能力强:由于采用视频和图片训练的方式,GOTURN算法具有较强的泛化能力。它不仅能够处理视频中的目标跟踪任务,还能对静态图片中的目标进行跟踪。
二、缺点
- 对遮挡敏感:虽然GOTURN算法对视角、形变和光照变化具有鲁棒性,但它对遮挡比较敏感。当目标被其他物体遮挡时,算法的跟踪性能可能会受到影响。
- 依赖物体运动的平稳性:GOTURN算法假设物体在视频中相邻两帧的运动具有相对平稳的性质。因此,当目标发生剧烈运动或快速移动时,算法的跟踪性能可能会下降。
- 跟踪失败后恢复困难:一旦GOTURN算法在跟踪过程中失败,它可能无法及时呈现错误报告,并且难以从失败中恢复。这可能导致算法在长时间跟踪任务中表现不佳。
综上所述,GOTURN算法以其高速度和离线训练的优势在目标跟踪领域具有一定的应用价值。然而,它对遮挡的敏感性和对物体运动平稳性的依赖也限制了其在某些复杂场景下的应用。因此,在选择目标跟踪算法时,需要根据具体的应用场景和需求进行权衡和选择。
7、参考
- http://davheld.github.io/GOTURN/GOTURN.html
- 目标跟踪(2)GOTURN:基于深度学习的目标跟踪
- 模型下载地址
https://github.com/spmallick/goturn-files - https://learnopencv.com/goturn-deep-learning-based-object-tracking/
- C++ source code and grouth truth for shadow detection / removal