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

Ela*_*ahe 8 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)
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,我面对这个错误:

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

以下是源代码:http://www.learnopencv.com/object-tracking-using-opencv-cpp-python/ 我正在寻找解决方案,但我找不到任何有用的东西...我该怎么做才能添加这个模块到我的opencv库?

小智 18

只需安装opencv-contrib-python

pip install opencv-contrib-python
Run Code Online (Sandbox Code Playgroud)

它会工作!