gpt4 book ai didi

python-3.x - Python中OpenCV的AttributeError

转载 作者:行者123 更新时间:2023-12-02 17:39:39 27 4
gpt4 key购买 nike

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

当我尝试运行时出现上述错误:
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。

在此先感谢,如果我遗漏了一些明显的东西,我很抱歉。

最佳答案

所以这不是安装的情况,而是构造函数名称发生了变化。

tracker = cv2.Tracker_create("MIL")

应该:
tracker = cv2.TrackerMIL_create()

关于python-3.x - Python中OpenCV的AttributeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46586252/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com