OpenCV-VideoWriter FPS

时间:2018-08-01 05:10:27

标签: opencv video-processing cv2

我正在构建一个脚本,以通过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

1 个答案:

答案 0 :(得分:1)

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