我试图使用VideoWriter
将检索到的帧保存为视频文件,但它似乎不起作用。我已经检查了while循环并打印了输出正在工作。我不知道为什么视频的输出只有O。
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()
发布于 2022-03-15 04:16:12
看来你没有正确地问这个问题。它与TensorFlow或对象检测无关。不幸的是,我从评论中得到了这一点,而不是从问题中得到的。
至于问题,除了决议外,一切都没问题。确保帧宽为1920,高度为1080。否则,当然视频大小将为0 kb。如果您将1920x1080作为输出分辨率传递给cv2.VideoWriter
,那么您得到的帧大小也应该匹配。因此,您应该调整检索到的帧的大小:
width = 1920
height = 1080
output_size = (width, height)
out.write(cv2.resize(output_file, output_size ))
或者使用这些来获得帧宽和高度:
width = vidcap.get(cv2.CAP_PROP_FRAME_WIDTH )
height = vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT )
fps = vidcap.get(cv2.CAP_PROP_FPS)
cv2.VideoWriter(output_path, fourcc, fps, (width, height))
在编写视频时,视频大小不能更改!
https://stackoverflow.com/questions/71465911
复制相似问题