如何在openCV python 2.7中添加“Tracker”

2024-06-09 21:04:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python 2.7和opencv 3.1 我想运行一个跟踪对象的代码:

import cv2
import sys

if __name__ == '__main__' :

    # Set up tracker.
    # Instead of MIL, you can also use
    # BOOSTING, KCF, TLD, MEDIANFLOW or GOTURN

    tracker = cv2.Tracker_create("MIL")

    # Read video
    video = cv2.VideoCapture("videos/chaplin.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)

但当我运行它时,我会面临这个错误:

AttributeError: 'module' object has no attribute 'Tracker_create'

这是源代码:http://www.learnopencv.com/object-tracking-using-opencv-cpp-python/ 我在寻找解决办法,但我找不到任何有用的… 如何将此模块添加到我的opencv库中?


Tags: importboxifvideosysnotokcv2
3条回答

我认为最简单和最快的方法是通过.whl文件安装。@foobar在post@kyjanond链接中给出了答案,但是您可以从以下链接获取.whl文件。

打开简历:https://pypi.python.org/pypi/opencv-python/3.3.0.10

OpenCV贡献:https://pypi.python.org/pypi/opencv-contrib-python/3.3.0.10

我在Python2.7上安装了OpenCV3.3.0,所以我下载了:

  • opencv_python-3.3.0.10-cp27-cp27m-win32.whl
  • opencv_contrib_python-3.3.0.10-cp27-cp27m-win32.whl

要安装,我运行了:

  • python-m pip安装opencv_python-3.3.0.10-cp27-cp27m-win32.whl
  • python-m pip安装opencv_contrib_python-3.3.0.10-cp27-cp27m-win32.whl

这是可行的,但是在OpenCV的更新版本中,tracker函数的调用方式已经改变。

GitHub存储库中的原始代码是:


tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN']

tracker_type = tracker_types[1]

tracker = cv2.Tracker_create(tracker_type)

我把这个改成了


tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN']

tracker_type = tracker_types[1]

if tracker_type == tracker_types[0]:
    tracker = cv2.TrackerBoosting_create()
elif tracker_type == tracker_types[1]:
    tracker = cv2.TrackerMIL_create()
elif tracker_type == tracker_types[2]:
    tracker = cv2.TrackerKCF_create()
elif tracker_type == tracker_types[3]:
    tracker = cv2.TrackerTLD_create()
elif tracker_type == tracker_types[4]:
    tracker = cv2.TrackerMedianFlow_create()
elif tracker_type == tracker_types[5]:
    tracker = cv2.TrackerGOTURN_create()

这种方法似乎对我很有效。

只需安装opencv contrib python

pip install opencv-contrib-python

它会起作用的!

看起来你没有用OpenCV_contrib模块编译你的OpenCV。你必须重新编译它。您可以在thisblogpost中找到一个非常好的分步指导。

编辑:

如果需要在Windows上编译,可以使用@Osama的this优秀教程

希望有帮助。

相关问题 更多 >