2017-10-05 62 views
0

Traceback (most recent call last): 
File "test.py", line 10, in <module> 
    tracker = cv2.Tracker_create("MIL") 
AttributeError: module 'cv2.cv2' has no attribute 'Tracker_create 

我得到上面的錯誤,當我嘗試運行:AttributeError的OpenCV的使用Python中

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

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

while True: 
    # Read a new frame 
    ok, frame = video.read() 
    if not ok: 
     break 

    # Update tracker 
    ok, bbox = tracker.update(frame) 

    # Draw bounding box 
    if ok: 
     p1 = (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)) 

    # Display result 
    cv2.imshow("Tracking", frame) 

    # Exit if ESC pressed 
    k = cv2.waitKey(1) & 0xff 
    if k == 27 : break 

我找到了答案在這裏:How to add "Tracker" in openCV python 2.7

但是,這讓我感到困惑更多。我在MacOSX上,我剛開始使用OpenCV,我不確定如何用正確的模塊重新編譯OpenCV。

在此先感謝,並對不起,如果我失去了明顯的東西。

+0

你安裝'opencv_contrib'模塊? 'tracker'在'tracking module'下面,它在'opencv'核心中不可用。 – thewaywewere

回答

1

所以它不是安裝的情況,但構造函數名稱已更改。

tracker = cv2.Tracker_create("MIL") 

應該是:

tracker = cv2.TrackerMIL_create() 
+0

這個答案應該被接受,希望提出這個問題的人接受答案, –