简体   繁体   English

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

[英]How to add “Tracker” in openCV python 2.7

I'm working with python 2.7 and opencv 3.1 I want to run a code for tracking objects by this: 我正在使用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)

but when I run it, I face with this error: 但是当我运行它时,我面对这个错误:

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

Here is the source code : http://www.learnopencv.com/object-tracking-using-opencv-cpp-python/ I'm searching for solutions but I can't find anything useful… what can I do to add this module to my opencv library? 以下是源代码: http//www.learnopencv.com/object-tracking-using-opencv-cpp-python/我正在寻找解决方案但我找不到任何有用的东西...我该怎么做才能添加这个模块到我的opencv库?

Just install opencv-contrib-python 只需安装opencv-contrib-python

pip install opencv-contrib-python

and it will work ! 它会工作!

I think the easiest and fastest method is to install via the .whl files. 我认为最简单,最快捷的方法是通过.whl文件安装。 @foobar gives the answer in the post @kyjanond links to, but you can obtain the .whl files from the following links. @foobar在@kyjanond链接的帖子中给出答案,但您可以从以下链接获取.whl文件。

OpenCV: https://pypi.python.org/pypi/opencv-python/3.3.0.10 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 OpenCV Contrib: https//pypi.python.org/pypi/opencv-contrib-python/3.3.0.10

I installed OpenCV 3.3.0 on Python 2.7, so I downloaded: 我在Python 2.7上安装了OpenCV 3.3.0,所以我下载了:

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

To install, I ran: 要安装,我运行:

  • python -m pip install opencv_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 python -m pip install opencv_contrib_python-3.3.0.10-cp27-cp27m-win32.whl

This worked, but in the updated version of OpenCV, the way the tracker functions are called have changed. 这很有效,但在OpenCV的更新版本中,调用跟踪器功能的方式已经改变。

The original code in the GitHub repository was: GitHub存储库中的原始代码是:


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

tracker_type = tracker_types[1]

tracker = cv2.Tracker_create(tracker_type)

I changed this to 我把它改成了


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

This approach seemed to work well for me. 这种方法对我来说似乎很有效。

It looks like you didn't compile your OpenCV with opencv_contrib modules. 看起来您没有使用opencv_contrib模块编译OpenCV。 You have to recompile it. 你必须重新编译它。 You can find a very good step-by-step tutorial how to do that in this blogpost. 您可以在博文中找到一个非常好的分步教程。

EDIT: 编辑:

If you need to compile it on Windows you can use this great tutorial by @Osama 如果你需要在Windows上编译它,你可以使用@Osama 这个伟大的教程

Hope it helps. 希望能帮助到你。

Once Installation is over. 安装结束后。 All files are installed in /usr/local/ folder. 所有文件都安装在/usr/local/文件夹中。
But to use it, your Python should be able to find OpenCV module. 但要使用它,你的Python应该能够找到OpenCV模块。

You have two options for that. 你有两个选择。

  1. Move the module to any folder in Python Path: Python path can be found out by entering import sys;print sys.path in Python terminal. 将模块移动到Python Path中的任何文件夹:通过在Python终端输入import sys;print sys.path可以找到Python路径。 It will print out many locations. 它会打印出许多地方。 Move /usr/local/lib/python2.7/site-packages/cv2.so to any of this folder. /usr/local/lib/python2.7/site-packages/cv2.so移动到此文件夹中的任何一个。 For example, su mv /usr/local/lib/python2.7/site-packages/cv2.so /usr/lib/python2.7/ → site-packages. 例如, su mv /usr/local/lib/python2.7/site-packages/cv2.so /usr/lib/python2.7/ But you will have to do this every time you install OpenCV . 但是每次安装OpenCV时都必须这样做。

  2. Add /usr/local/lib/python2.7/site-packages to the PYTHON_PATH : It is to be done only once. /usr/local/lib/python2.7/site-packages添加到PYTHON_PATH :它只需要执行一次。 Just open ~/.bashrc and add following line to it, then log out and come back. 只需打开~/.bashrc并添加以下行,然后注销并返回。 export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages . export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages Thus OpenCV installation is finished. 因此OpenCV安装完成。 Open a terminal and try import cv2. 打开终端并尝试导入cv2。

New versions of openCV use for example: TrackerKCF_create() notation. 新版本的openCV使用例如:TrackerKCF_create()表示法。

You can find new examples here 你可以在这里找到新的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM