我正在构建一个脚本,以通过CSV文件将边框叠加到我的视频上。每个框架都有n个边界框,因此我只是遍历每个框架中的边界框并在该框架上绘制cv2.rectangle。结果,对于所有框架,我都会多次写入一个框架。

虽然我的VideoWriter构造函数采用23.97 FPS作为参数,但生成的FPS却低得多。有没有一种算法或方法可以设置适当的FPS来补偿写入视频后的FPS下降?

以下是我的代码段:

avg_fws = counter_written/float(total_frames-1)
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = video.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('data/bounding_videos/%s.avi' % video_file, fourcc, fps * avg_fws, (width,height))
counter = 1
print (counter_written)
while (video.isOpened()):
    ret, frame = video.read()
    if ret == True:
        if len(frames_dict) != 0:
            for i in frames_dict[counter].keys():
                box_dim = frames_dict[counter][i]
                x, y, w, h = box_dim
                cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 255), 3)
                out.write(frame)
        else:
            out.write(frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
    counter += 1
video.release()
out.release()
cv2.destroyAllWindows()

计数器只是让我跟踪/访问帧,而avg_fws是每秒写入的平均帧,基本上是total_num_of_frames_written / total_num_of_frames_in_video

最佳答案

代码的问题在于,您在for循环中为绘制的每个矩形编写了多个框架。您需要做的是在一个框架上一次绘制所有矩形,并仅将其写入一次。可以通过将out.write(frame)移出for循环来完成。

关于opencv - OpenCV-VideoWriter FPS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51625716/

10-12 22:02