简体   繁体   中英

Python says that "TrackerMedianFlow_create()" is no longer an attribute of cv2, Has the Library been updated?

Python says that TrackerMedianFlow_create() is no longer an attribute of cv2.

I've looked here but it's not the same: OpenCV, How to pass parameters into cv2.TrackerMedianFlow_create function? I've asked on several discord servers without success. I've copied this code directly from my textbook with ctrl + c so it should be exact.

import cv2
import numpy as np

cap = cv2.VideoCapture("../data/traffic.mp4")
_, frame = cap.read()

bbox = cv2.selectROI(frame, False, True)

cv2.destroyAllWindows()

tracker = cv2.TrackerMedianFlow_create()
status_tracker = tracker.init(frame, bbox)
fps = 0

while True:
    status_cap, frame = cap.read()
    if not status_cap:
        break

    if status_tracker:
        timer = cv2.getTickCount()
        status_tracker, bbox = tracker.update(frame)

    if status_tracker:
        x, y, w, h = [int(i) for i in bbox]
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 15)
        fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);
        cv2.putText(frame, "FPS: %.0f" % fps, (0, 80), cv2.FONT_HERSHEY_SIMPLEX, 3.5, (0, 0, 0), 8);
    else:
        cv2.putText(frame, "Tracking failure detected", (0, 80), cv2.FONT_HERSHEY_SIMPLEX, 3.5, (0,0,255), 8)

    cv2.imshow("MedianFlow tracker", frame)

    k = cv2.waitKey(1)

    if k == 27: 
        break

cv2.destroyAllWindows()

My line that causes the problem is:

tracker = cv2.TrackerMedianFlow_create()

Up until there the code runs.

Traceback (most recent call last):
  File "D:/Documents/E-Books/Comp Vision/opencv3computervisionwithpythoncookbook_ebook/OpenCV3ComputerVisionwithPythonCookbook_Code/Chapter04/myPart5.py", line 11, in <module>
    tracker = cv2.TrackerMedianFlow_create()
AttributeError: module 'cv2.cv2' has no attribute 'TrackerMedianFlow_create'

I expected it to work without an error.

for opencv 4.5.1 user

opencv-contrib-python

import cv2
cv2.legacy_TrackerMedianFlow()

TrackerMedianFlow is a module within the opencv-contrib package , and does not come standard with the official OpenCV distribution. You will need to install the opencv-contrib package to access TrackerMedianFlow_create()

Per the documentation , you should uninstall the package without the additional modules and proceed to reinstall opencv with the additional modules you need.

pip uninstall opencv-python
pip install opencv-contrib-python

It has been installed opencv-contrib-python-4.5.4.60 version,Again AttributeError: module 'cv2' has no attribute 'TrackerMedianFlow_create',what is the reason

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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