I don't know opencv, so there may be a better answer - but you can always implement the context manager yourself by defining the __enter__
and __exit__
hooks:
class MyVideoCapture(cv.VideoCapture):
def __enter__(self):
return self
def __exit__(self, *args):
self.release()
The usage would then look like:
with MyVideoCapture(0) as c:
while(True):
_, f = c.read()
cv.imshow('e2', f)
if cv.waitKey(5) == 27:
cv.waitKey()
break
and the resource will be released after you hit the break
statement.
Based on your comment, it looks like opencv
is doing something funky here. You can also create a custom class to wrap the VideoCapture instance. In today's world, I'd probably use contextlib
@contextlib.contextmanager
def video_capture_wrapper(*args, **kwargs):
try:
vid_stream = VideoCapture(*args, **kwargs)
yield vid_stream
finally:
vid_stream.release()
and the usage here would be:
with video_capture_wrapper(0) as vid_stream:
while(True):
_, f = vid_stream.read()
cv.imshow('e2', f)
if cv.waitKey(5) == 27:
cv.waitKey()
break