opencv错误集锦之AttributeError: module 'cv2.cv2' has no attribute 'cv'

 

初学opencv,遇到很多坑,而且有些函数因为版本不同,前人的轮子我跑不起来,总结一下,分享给大家:

 

问题一:

total_frames = cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)

AttributeError: module 'cv2.cv2' has no attribute 'cv'

opencv错误集锦之AttributeError: module 'cv2.cv2' has no attribute 'cv'_第1张图片

 

解决:

# 原语句:
# total_frames = cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
# 更改为:
total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)

 

 

fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)

AttributeError: module 'cv2.cv2' has no attribute 'cv'

opencv错误集锦之AttributeError: module 'cv2.cv2' has no attribute 'cv'_第2张图片

解决:

# 原语句
# fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)
# 更改为
fps = cap.get(cv2.CAP_PROP_FPS)

 

 

 

彩蛋:

if int((cv2.__version__).split('.')[0]) < 3:
    # self.fps = cap.get(cv2.XXX)
    self.fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)
else:
    # self.fps = cap.get(cv2.YYY)
    self.fps = cap.get(cv2.CAP_PROP_FPS)

解决兼容性问题!

 

 

 

你可能感兴趣的:(opencv)