欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > 【python】OpenCV—Tracking(10.3)——GOTURN

【python】OpenCV—Tracking(10.3)——GOTURN

2024/10/25 13:46:29 来源:https://blog.csdn.net/bryant_meng/article/details/143176012  浏览:    关键词:【python】OpenCV—Tracking(10.3)——GOTURN

在这里插入图片描述

文章目录

  • 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, osnum = 0if 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 = Falseif 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 frameok, frame = video.read()if not ok:break# Start timertimer = cv2.getTickCount()# Update trackerok, bbox = tracker.update(frame)# Calculate Frames per second (FPS)fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);# Draw bounding boxif ok:# Tracking successp1 = (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 failurecv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)# Display tracker type on framecv2.putText(frame, "GOTURN Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);# Display FPS on framecv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);# Display resultcv2.imshow("Tracking", frame)cv2.imwrite(f"{str(num).zfill(3)}.jpg", frame)# Exit if ESC pressedk = cv2.waitKey(1) & 0xffif k == 27:break

遍历视频中的图片,ok, bbox = tracker.update(frame) 跟踪,绘制帧率、跟踪器,保存、显示每一帧结果,ESC 键退出

4、完整代码

# Import modules
import cv2, sys, osnum = 0if 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 = Falseif 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 frameok, frame = video.read()if not ok:break# Start timertimer = cv2.getTickCount()# Update trackerok, bbox = tracker.update(frame)# Calculate Frames per second (FPS)fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);# Draw bounding boxif ok:# Tracking successp1 = (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 failurecv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)# Display tracker type on framecv2.putText(frame, "GOTURN Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);# Display FPS on framecv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);# Display resultcv2.imshow("Tracking", frame)cv2.imwrite(f"{str(num).zfill(3)}.jpg", frame)# Exit if ESC pressedk = cv2.waitKey(1) & 0xffif 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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com