yoohaythem / OpenCV

OpenCV 视频学习笔记完全注释
GNU General Public License v3.0
3 stars 0 forks source link

项目实战-目标追踪:AttributeError: module ‘cv2‘ has no attribute ‘TrackerBoosting_create‘ #2

Open yoohaythem opened 1 year ago

yoohaythem commented 1 year ago

错误描述:在利用opencv库做目标跟踪时,报错如下: AttributeError: module 'cv2' has no attribute 'TrackerBoosting_create' AttributeError: module 'cv2' has no attribute 'TrackerTLD_create' AttributeError: module 'cv2' has no attribute 'TrackerMedianFlow_create' AttributeError: module 'cv2' has no attribute 'TrackerMOSSE_create' AttributeError: module ‘cv2.cv2‘ has no attribute ‘MultiTracker_create‘

报错原因:opencv版本包括4.5.1以上,都不支持这些函数。

解决方法:通过cv2.legacy来调用这些函数。

原始代码:

OPENCV_OBJECT_TRACKERS = { "csrt": cv2.TrackerCSRT_create, "kcf": cv2.TrackerKCF_create, "boosting": cv2.TrackerBoosting_create, "mil": cv2.TrackerMIL_create, "tld": cv2.TrackerTLD_create, "medianflow": cv2.TrackerMedianFlow_create, "mosse": cv2.TrackerMOSSE_create } trackers = cv2.MultiTracker_create() # # 实例化OpenCV's multi-object tracker

改动后代码:

OPENCV_OBJECT_TRACKERS = { "csrt": cv2.TrackerCSRT_create, "kcf": cv2.TrackerKCF_create, "boosting": cv2.legacy.TrackerBoosting_create, "mil": cv2.TrackerMIL_create, "tld": cv2.legacy.TrackerTLD_create, "medianflow": cv2.legacy.TrackerMedianFlow_create, "mosse": cv2.legacy.TrackerMOSSE_create } trackers = cv2.legacy.MultiTracker_create() # # 实例化OpenCV's multi-object tracker

yoohaythem commented 12 months ago

error: OpenCV(4.5.4) :-1 : error: (-5:Bad argument) in function 'add' Overload resolution failed:

Expected Ptrcv::legacy::Tracker for argument 'newTracker' Expected Ptrcv::legacy::Tracker for argument 'newTracker'

原因是cv2.TrackerKCF_create()也发生了相应的变动,需要改成cv2.legacy.TrackerKCF_create

所以建议讲整个字典OPENCV_OBJECT_TRACKERS 全部改为: OPENCV_OBJECT_TRACKERS = { "csrt": cv2.legacy.TrackerCSRT_create, "kcf": cv2.legacy.TrackerKCF_create, "boosting": cv2.legacy.TrackerBoosting_create, "mil": cv2.legacy.TrackerMIL_create, "tld": cv2.legacy.TrackerTLD_create, "medianflow": cv2.legacy.TrackerMedianFlow_create, "mosse": cv2.legacy.TrackerMOSSE_create }