如何添加" Tracker"在openCV python 2.7中

时间:2017-02-22 09:20:45

标签: python opencv tracker

我正在使用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库中?

5 个答案:

答案 0 :(得分:17)

只需安装opencv-contrib-python

即可
pip install opencv-contrib-python

它会起作用!

答案 1 :(得分:2)

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

OpenCV:https://pypi.python.org/pypi/opencv-python/3.3.0.10

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

我在Python 2.7上安装了OpenCV 3.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 install opencv_python-3.3.0.10-cp27-cp27m-win32.whl
  • python -m pip install opencv_contrib_python-3.3.0.10-cp27-cp27m-win32.whl

这很有效,但在OpenCV的更新版本中,调用跟踪器功能的方式已经改变。

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()

这种方法似乎对我有用。

答案 2 :(得分:1)

看起来您没有使用opencv_contrib模块编译OpenCV。你必须重新编译它。您可以在this博文中找到一个非常好的分步教程。

修改

如果您需要在Windows上编译它,可以使用@Osama

this精彩教程

希望它有所帮助。

答案 3 :(得分:1)

安装结束后。所有文件都安装在 /usr/local/文件夹。
但要使用它,你的Python应该能够找到 OpenCV模块。

你有两种选择。

  1. 将模块移动到Python Path中的任何文件夹:通过在Python终端输入import sys;print sys.path可以找到Python路径。它会打印出许多地方。将/usr/local/lib/python2.7/site-packages/cv2.so移动到此文件夹中的任何一个。例如,su mv /usr/local/lib/python2.7/site-packages/cv2.so /usr/lib/python2.7/→site-packages。但是每次安装OpenCV时都必须这样做。

  2. /usr/local/lib/python2.7/site-packages添加到PYTHON_PATH:只需执行一次。只需打开~/.bashrc并添加以下行,然后退出并返回。 export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages。因此OpenCV安装完成。打开终端试试吧 import cv2。

答案 4 :(得分:0)

新版本的openCV使用例如:TrackerKCF_create()表示法。

您可以找到新的示例here