Python表示TrackerMedianFlow_create()不再是cv2的属性。
我看过这里,但是不一样:OpenCV, How to pass parameters into cv2.TrackerMedianFlow_create function? 我曾在几台不和谐的服务器上询问过,但没有成功。 我已经使用ctrl + c直接从我的教科书中复制了此代码,因此应该准确。
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()
引起问题的行是:
tracker = cv2.TrackerMedianFlow_create()
直到代码开始运行。
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'
我希望它能正常运行。
答案 0 :(得分:1)
TrackerMedianFlow
是module within the opencv-contrib package,并且不是官方OpenCV发行版的标准配置。您将需要安装opencv-contrib软件包才能访问TrackerMedianFlow_create()
对于documentation,您应该卸载不包含其他模块的软件包,并继续使用所需的其他模块重新安装opencv。
pip uninstall opencv-python
pip install opencv-contrib-python
答案 1 :(得分:1)
对于 opencv 4.5.1 用户
opencv-contrib-python
import cv2
cv2.legacy_TrackerMedianFlow()