我来自RDBMS背景,刚刚开始使用python。下面是我编写的一个简单代码,用于通过python
调用我的网络摄像头import cv2
vid = cv2.VideoCapture(0)
while vid == True:
print("Connected....");
if cv2.waitKey(0) : break
cv2.release();
但我收到错误
AttributeError: module 'cv2.cv2' has no attribute 'release'
执行时。我使用python3.5和linux 14.04平台运行此代码。我可以通过help(" modules")列表看到安装的cv2包,它也会被导入而不会出错。但是我没有在pycharm的解释列表中看到它。请帮忙。
答案 0 :(得分:1)
vid.release()
不存在。我认为你要做的是cv2
vid
是opencv模块,VideoCapture
是while vid == True:
对象。这是你必须要发布的那个。
你的代码有几个错误。之前我只解决了你问过的问题,但是让我们仔细阅读它们。
首先,缩进是错误的,我想也许是复制代码。
第二个
vid.isOpened()
这不是正确的方法。您可以使用;
功能查看它是否已打开/连接到网络摄像头。
第三,您不需要在说明后使用if cv2.waitKey(0) : break
。
第四,这个不是错误,而是一些不必要的东西
imshow
如果没有必要,waitKey将返回按下ascii字符的键,如果使用0以外的数字,如果没有按键则返回0。但是对于0,它将等待一个键被按下"阻塞"当前线程(如果您有多个)。但是,除非您打开了import cv2
vid = cv2.VideoCapture(0)
if vid.isOpened():
print ("Connected....")
else:
print ("Not Connected....")
vid.release()
窗口,否则它不会等待。
现在,我编写的那些更改的完整代码将检查脚本是否可以连接到摄像头
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()
以类似的方式,您可以显示视频,直到按下某个键:
def main():
conn = connect("172.31.10.8")
cmd = "sz -Xbe 1.pcap"
stdin, stdout,stderr = conn.exec_command(cmd)
subproc = subprocess.Popen("rz.exe -Xbe",stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
subproc.communicate(stdout.read())
如果不清楚,请随意询问:)
答案 1 :(得分:0)
导入cv2 导入numpy
img = cv2.imread(“ lena.jpg”,1) cv2.imshow(“ image”,img) cv2.waitKeyEx(0) cv2.destroyAllWindows()