gpt4 book ai didi

python - 属性错误 : module 'cv2.cv2' has no attribute 'cv' (similar topic didn't help)

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

我正在使用 python-OpenCV 4.4 版,并且仅在运行基本代码块时:

import cv2

camera_capture = cv2.VideoCapture(0)
fps = 30
size = int(camera_capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),\
int(camera_capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
video_writer = cv2.VideoWriter('MyOutputVid.avi', cv2.cv.CV_FOURCC('I','4','2','0'),
fps, size)
我得到 AttributeError: module 'cv2.cv2' has no attribute 'cv'我知道已经从 OpenCV 过渡到 OpenCV2,但是如何
进口受到影响?
我发现了类似的话题 here但删除第二个 cv2从代码对我来说没有帮助。它只是抛出了另一个错误 AttributeError: module 'cv2.cv2' has no attribute 'CV_CAP_PROP_FRAME_WIDTH'如何使用属性?

最佳答案

所以这段代码似乎有效。希望做你想做的事。适用于 Windows 10、python3 3.8.6 64 位、opencv-python 4.4.0.42。

import cv2

camera_capture = cv2.VideoCapture(0)

fps = 30
size = int(camera_capture.get(cv2.CAP_PROP_FRAME_WIDTH)),\
int(camera_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))

vidwrite = cv2.VideoWriter('testvideo.avi', cv2.VideoWriter_fourcc('I','4','2','0'), fps,
size,True)


# Write and show recording
while camera_capture.isOpened():

_, frame = camera_capture.read()
vidwrite.write(frame)

cv2.imshow("showlive", frame)
cv2.waitKey(1)

如果您仍然对 CAP_PROP_FRAME_HEIGHT 有问题,我建议您重新安装 opencv。 32 位版本的 python3 不支持 OpenCV 中的某些函数,可能也值得检查。当然,另一种方法是使用 height, width, _ = frame.shape请参阅上面代码中的框架。

关于python - 属性错误 : module 'cv2.cv2' has no attribute 'cv' (similar topic didn't help),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64081509/

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