我喜欢使用with语句访问文件和数据库连接,因为如果出现错误或文件关闭,它会自动断开连接。
f = open('file.txt', 'r')
for i in f():
print(i)
f.close()
与
with open('file.txt', 'r') as f:
for i in f:
print(i)
以下是否有从相机缓冲区读取的等效改写?:
c = cv.VideoCapture(0)
while(1):
_,f = c.read()
cv.imshow('e2',f)
if cv.waitKey(5)==27:
cv.waitKey()
break
c.release()
我试过了:
c = cv.VideoCapture(0)
while(1):
with c.read() as _,f:
cv.imshow('e2',f)
if cv.waitKey(5)==27:
cv.waitKey()
break
---没有运气。看起来拆卸/释放是一种不同的功能。这个成语可以在这里吗?
答案 0 :(得分:4)
另一种方式:
from contextlib import contextmanager
@contextmanager
def VideoCapture(*args, **kwargs):
cap = cv2.VideoCapture(*args, **kwargs)
try:
yield cap
finally:
cap.release()
答案 1 :(得分:3)
我不知道opencv,所以可能有更好的答案 - 但您可以通过定义__enter__
和__exit__
挂钩来自己实现context manager:
class MyVideoCapture(cv.VideoCapture):
def __enter__(self):
return self
def __exit__(self, *args):
self.release()
用法如下:
with MyVideoCapture(0) as c:
while(1):
_,f = c.read()
cv.imshow('e2',f)
if cv.waitKey(5)==27:
cv.waitKey()
break
并且在您点击break
语句后将释放资源。
根据您的评论,opencv
看起来像是在做一些时髦的事情。您还可以创建一个自定义类来包装VideoCapture实例:
class VideoCaptureWrapper(object):
def __init__(self, *args, **kwargs):
self.vid_steam = VideoCapture(*args, **kwargs):
def __enter__(self):
return self
def __exit__(self, *args):
self.vid_stream.release()
这里的用法是:
with VideoCaptureWrapper(0) as c:
while(1):
_,f = c.vid_stream.read()
cv.imshow('e2',f)
if cv.waitKey(5)==27:
cv.waitKey()
break