错误描述:在利用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'
报错原因:opencv版本包括4.5.1以上,都不支持这些函数;网上其它解决方法多数提出安装 ‘opencv-contrib-python’,但是个人试过无法解决该问题;
解决方法:通过cv2.legacy来调用这些函数,比如 ‘cv2.TrackerBoosting_create’ 改成 ‘cv2.legacy.TrackerBoosting_create’;其它也类似改动即可!
原始代码:
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
}
改动后代码:
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
}
欢迎指正!