VideoWriter 无法正确保存视频
我正在尝试使用 VideoWriter
将检索到的帧保存为视频文件,但它似乎不起作用。我已经检查了 while 循环并打印输出是否正常。不知道为什么输出的视频只有O kb。
def detect_video(self, path, output_path):
# Set output video writer with codec
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, 25.0, (1920, 1080))
# Read the video
vidcap = cv2.VideoCapture(path)
frame_read, image = vidcap.read()
count = 0
# Iterate over frames and pass each for prediction
while frame_read:
# Perform object detection and add to output file
output_file = self.detect(image)
#print(output_file)
# Write frame with predictions to video
out.write(output_file)
# Read next frame
frame_read, image = vidcap.read()
count += 1
#print(count)
# Release video file when we're ready
out.release()
I'm trying to use VideoWriter
to save the retrieved frames as video file but it seems it does not work. I have checked the while loop and print the output is working. I don't know why the output of the video is only O kb.
def detect_video(self, path, output_path):
# Set output video writer with codec
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, 25.0, (1920, 1080))
# Read the video
vidcap = cv2.VideoCapture(path)
frame_read, image = vidcap.read()
count = 0
# Iterate over frames and pass each for prediction
while frame_read:
# Perform object detection and add to output file
output_file = self.detect(image)
#print(output_file)
# Write frame with predictions to video
out.write(output_file)
# Read next frame
frame_read, image = vidcap.read()
count += 1
#print(count)
# Release video file when we're ready
out.release()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来你没有正确地提出问题。它与 TensorFlow 或对象检测无关。不幸的是,我是从评论中得到这一点的,而不是从问题中得到的。
至于问题,一切都好,但解决不了。确保帧宽度为 1920,高度为 1080。否则,视频大小当然将为 0 kb。如果您将 1920x1080 传递给
cv2.VideoWriter
作为输出分辨率,您获得的帧大小也应该匹配。因此,您应该调整大小检索到的帧:
或使用这些来获取帧的宽度和高度:
PS 视频大小在写入视频时无法更改!
It seems you didn't ask the question correctly. It has nothing to do with either TensorFlow or Object Detection. Unfortunately I got this from the comments, not from the question.
As for the problem, everything is okay but the resolution. Make sure the frame width is 1920 and the height is 1080. Otherwise, of course the video size will be 0 kb. If you've passed 1920x1080 to
cv2.VideoWriter
as output resolution, the frame size you get should also match. So, you should either resize theretrieved frame:
or use these to get the frame width and height:
P.S. Video size can not be changed while writing the video!