我尝试运行此代码,但出现属性错误。任何帮助将不胜感激。
import cv2
import sys
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
if __name__ == '__main__':
# Set up tracker.
# Instead of MIL, you can also use
tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'CSRT', 'MOSSE']
tracker_type = tracker_types[5]
if int(minor_ver) < 3:
tracker = cv2.cv2.Tracker_create(tracker_type)
else:
if tracker_type == 'BOOSTING':
tracker = cv2.TrackerBoosting_create()
if tracker_type == 'MIL':
tracker = cv2.TrackerMIL_create()
if tracker_type == 'KCF':
tracker = cv2.TrackerKCF_create()
if tracker_type == 'TLD':
tracker = cv2.TrackerTLD_create()
if tracker_type == 'MEDIANFLOW':
tracker = cv2.TrackerMedianFlow_create()
if tracker_type == 'CSRT':
tracker = cv2.TrackerCSRT_create()
if tracker_type == 'MOSSE':
tracker = cv2.TrackerMOSSE_create()
# 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)
while True:
# Read a new frame
ok, frame = video.read()
if not ok:
break
# Start timer
timer = cv2.getTickCount()
# Update tracker
ok, bbox = tracker.update(frame)
# Calculate Frames per second (FPS)
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);
# Draw bounding box
if ok:
# Tracking success
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)
else :
# Tracking failure
cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)
# Display tracker type on frame
cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
# Display FPS on frame
cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
# Display result
cv2.imshow("Tracking", frame)
# Exit if ESC pressed
k = cv2.waitKey(1) & 0xff
if k == 27 : break
输出:
回溯(最近通话最近): 文件“ C:\ Users \ Jatin \ OpenCV-Object-Tracking \ index.py”,第15行,在 跟踪程序= cv2.cv2.Tracker_create(tracker_type) AttributeError:模块'cv2.cv2'没有属性'Tracker_create'
答案 0 :(得分:2)
这取决于您所安装的OpenCV软件包和版本。
OpenCV软件包:我相信您需要 OpenCV 3.4 + 来运行这些模型。一些跟踪器型号在3.2和3.3中可用。脚本中的所有跟踪器在3.4中都可用
opencv-python
:此存储库包含OpenCV库的主要模块。
opencv-contrib-python
:opencv-contrib-python存储库包含主要模块和contrib模块
pip安装3.4版或更高版本的contrib。
答案 1 :(得分:2)
如果您使用的是OpenCV 4或更高版本,则上面的代码可能不起作用。
if int(minor_ver) < 3:
对于OpenCV 4的换行符您只需为此实验安装opencv contrib
pip install -r requirements.txt
dlib==19.19.0
imutils==0.5.3
numpy==1.18.4
opencv-contrib-python==4.2.0.34
scipy==1.4.1
import cv2
import sys
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
print(cv2.__version__)
if __name__ == '__main__':
# Set up tracker.
# Instead of MIL, you can also use
tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'CSRT', 'MOSSE']
tracker_type = tracker_types[6]
if int(major_ver) < 4 and int(minor_ver) < 3:
tracker = cv2.cv2.Tracker_create(tracker_type)
else:
if tracker_type == 'BOOSTING':
tracker = cv2.TrackerBoosting_create()
if tracker_type == 'MIL':
tracker = cv2.TrackerMIL_create()
if tracker_type == 'KCF':
tracker = cv2.TrackerKCF_create()
if tracker_type == 'TLD':
tracker = cv2.TrackerTLD_create()
if tracker_type == 'MEDIANFLOW':
tracker = cv2.TrackerMedianFlow_create()
if tracker_type == 'CSRT':
tracker = cv2.TrackerCSRT_create()
if tracker_type == 'MOSSE':
tracker = cv2.TrackerMOSSE_create()
# 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
# Start timer
timer = cv2.getTickCount()
# Update tracker
ok, bbox = tracker.update(frame)
# Calculate Frames per second (FPS)
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);
# Draw bounding box
if ok:
# Tracking success
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)
else :
# Tracking failure
cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)
# Display tracker type on frame
cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
# Display FPS on frame
cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
# Display result
cv2.imshow("Tracking", frame)
# Exit if ESC pressed
k = cv2.waitKey(1) & 0xff
if k == 27 : break
答案 2 :(得分:1)
我的Python版本是3.7,它似乎并没有忽略下面的代码。
if int(minor_ver) < 3:
tracker = cv2.cv2.Tracker_create(tracker_type)
我只是删除了它,模块开始运行。
一个无关但希望有所帮助的建议。在上面的代码结尾,我添加了一个`destroyAllWindows()命令,该命令可以清理查看器:
if k == 27:
cv2.destroyAllWindows()
break
答案 3 :(得分:1)
我收到以下错误: tracker = cv2.TrackerMOSSE.create()
甚至以为我已经安装了: opencv-contrib-python
我可以改用这个来修复错误: tracker = cv2.legacy_TrackerMOSSE.create()
答案 4 :(得分:0)
至少对于:
您需要更换:
cv2.legacy.TrackerMOSSE_create()
与...
cv2.legacy.TrackerMOSSE_create()```