我想在不保存视频的情况下,在用cv2.VideoWriter编写视频后阅读它。
例如:
video = cv2.VideoWriter('using.mp4', cv2.VideoWriter_fourcc(*'MJPG'), 10, size)现在,在编写这个cv2.VideoWriter对象之后,是否可以像video.read()一样读取它,但是因为read()是cv2.VideoCapture的一个函数,它会抛出一个错误
Exception has occurred: AttributeError
'cv2.VideoWriter' object has no attribute 'read'那么,有没有可能阅读cv2.VideoWriter
发布于 2022-02-25 07:58:28
从视频写入器读取帧的替代方法是将帧保存在列表中,而不是保存循环中的每个帧。完成后,可以将它们写在循环之外,并将保存影响作为video.read()
video = cv2.VideoWriter('using.mp4', cv2.VideoWriter_fourcc(*'MJPG'), 10, size)
for frame in frames:
writer.write(frame)
for frame in frames:
# do other stuff here详细的示例(注意,我更改了四me-您的示例不适合我)
import cv2
def cam_test(port: int = 0) -> None:
frames = []
cap = cv2.VideoCapture(port)
if not cap.isOpened(): # Check if the web cam is opened correctly
print("failed to open cam")
else:
print('cam opened on port {}'.format(port))
for i in range(10 ** 10):
success, cv_frame = cap.read()
if not success:
print('failed to capture frame on iter {}'.format(i))
break
frames.append(cv_frame)
cv2.imshow('Input', cv_frame)
k = cv2.waitKey(1)
if k == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# Now you have the frames at hand
if len(frames) > 0:
# if you want to write them
size = (frames[0].shape[1], frames[0].shape[0])
video = cv2.VideoWriter(
filename='using.mp4',
fourcc=cv2.VideoWriter_fourcc(c1='m', c2='p', c3='4', c4='v'),
fps=10,
frameSize=size
)
for frame in frames:
video.write(frame)
# and to answer your question, you wanted to do video.read() which would have gave you frame by frame
for frame in frames:
pass # each iteration is like video.read() if video.read() was possible
return
if __name__ == '__main__':
cam_test()https://stackoverflow.com/questions/71261949
复制相似问题