[英]AttributeError: module 'cv2.cv2' has no attribute 'release'
I come from RDBMS background and just starting on python.我来自 RDBMS 背景,刚开始使用 python。 below is a simple code i written to invoke my web cam via python
下面是我编写的一个简单代码,用于通过 python 调用我的网络摄像头
import cv2
vid = cv2.VideoCapture(0)
while vid == True:
print("Connected....");
if cv2.waitKey(0) : break
cv2.release();
but i am getting error但我收到错误
AttributeError: module 'cv2.cv2' has no attribute 'release'
while executing it.在执行时。 I am running this code using python3.5 and on linux 14.04 platform.
我正在使用 python3.5 和 linux 14.04 平台运行此代码。 I can see cv2 package installed via help("modules") list and it gets imported as well without error .
我可以看到通过 help("modules") 列表安装的 cv2 包,并且它也被导入而没有错误。 however i dont see it in the interpreter list of pycharm.
但是我在 pycharm 的解释器列表中没有看到它。 please help.
请帮忙。
cv2.release()
does not exist. cv2.release()
不存在。 I think what you are trying to do is vid.release()
我认为你想要做的是
vid.release()
cv2
is the opencv module and vid
is the VideoCapture
object. cv2
是 opencv 模块, vid
是VideoCapture
对象。 which is the one that you have to do the release.这是您必须发布的那个。
The code you have has several mistakes.你的代码有几个错误。 Before I only addressed the one you asked, but lets go through all of them.
在我只解决你问的那个之前,让我们来看看所有这些。
First one, the indentation is wrong, I guess maybe it is from copying the code.第一个,缩进不对,我猜可能是复制代码造成的。
Second one第二个
while vid == True:
This is not the correct way to do it.这不是正确的方法。 You can use the
vid.isOpened()
function to see if it opened/connected to the webcam.您可以使用
vid.isOpened()
函数查看它是否已打开/连接到网络摄像头。
Third one, you do not need to use ;
第三个,你不需要使用
;
after the instructions.在指示之后。
Fourth one, this one is not an error, but something that is not necessary第四个,这个不是错误,而是没有必要的东西
if cv2.waitKey(0) : break
the if is not necessary, waitKey will return the key pressed as an ascii character, if you use a number other than 0 it will return 0 if no key was pressed.如果没有必要,waitKey 会将按下的键返回为 ascii 字符,如果您使用 0 以外的数字,如果没有按下任何键,它将返回 0。 But with 0 it will wait for a key to be pressed "blocking" the current thread (in case you have more than one).
但是使用 0 它将等待按下一个键“阻塞”当前线程(如果你有多个)。 However, it will not wait unless that you have a
imshow
window opened.但是,除非您打开了
imshow
窗口,否则它不会等待。
Now, the complete code with those changes that I wrote and that checks if the script can connect to a camera will be现在,包含我编写的那些更改并检查脚本是否可以连接到相机的完整代码将是
import cv2
vid = cv2.VideoCapture(0)
if vid.isOpened():
print ("Connected....")
else:
print ("Not Connected....")
vid.release()
In a similar fashion you can display the video until a key is pressed:以类似的方式,您可以显示视频,直到按下某个键:
import cv2
vid = cv2.VideoCapture(0)
if vid.isOpened():
print ("Connected....")
while True:
ret, frame = vid.read()
if ret:
cv2.imshow("image", frame)
else:
print ("Error aqcuiring the frame")
break
if cv2.waitKey(10) & 0xFF:
break
else:
print ("Not Connected....")
vid.release()
cv2.destroyAllWindows()
If something is not clear, feel free to ask :)如果有什么不清楚的,请随时提问:)
import cv2 import numpy导入 cv2 导入 numpy
img = cv2.imread("lena.jpg", 1) cv2.imshow("image", img) cv2.waitKeyEx(0) cv2.destroyAllWindows() img = cv2.imread("lena.jpg", 1) cv2.imshow("image", img) cv2.waitKeyEx(0) cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.