python opencv cv2.cv.CV_CAP_PROP_FRAME_COUNT输入错误的数字 [英] python opencv cv2.cv.CV_CAP_PROP_FRAME_COUNT get wrong numbers

查看:1063
本文介绍了python opencv cv2.cv.CV_CAP_PROP_FRAME_COUNT输入错误的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import os
import cv2
path='/home/nlpr4/video-data/UCF-101/GolfSwing/v_GolfSwing_g24_c06.avi'
cap=cv2.VideoCapture(path)
video_length=int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))

success=True
count=0
while success:
    success,image=cap.read()
    if success==False:
        break
    count=count+1

print video_length,count

输出:

149 
146

为什么两个数字不同? 怎么了?

why the two numbers different? what's wrong?

推荐答案

CAP_PROP_FRAME_COUNT的get()绝对不正确!如果您检查opencv源代码.您可以找到以下内容:

The get() for CAP_PROP_FRAME_COUNT is never supposed to be accurate! If you check the opencv source code. You can find this:

int64_t CvCapture_FFMPEG::get_total_frames() const
{
    int64_t nbf = ic->streams[video_stream]->nb_frames;

    if (nbf == 0)
    {
        nbf = (int64_t)floor(get_duration_sec() * get_fps() + 0.5);
    }
    return nbf;
}

这意味着它将首先查看nb_frames的流头,您可以使用ffprobe进行检查.如果没有这样的字段,那么没有比直接解码视频更好的方法来获取帧号. opencv通过get_duration_sec() * get_fps() + 0.5进行了粗略估计,这肯定不意味着准确性.

This means it will first look into the stream header for nb_frames, which you can check with ffprobe. If there is no such field, then there is no better way to get frame number than directly decoding the video. The opencv did a rough estimation by get_duration_sec() * get_fps() + 0.5 which surely not mean for accuracy.

因此,要获得正确的帧号,您必须对整个流进行解码和读取,或者必须要求视频生成器使用nb_frames字段来生成正确的流头.

Thus, to obtain the correct frame number you have to decode and read through the entire stream, or you have to ask the video generator to generate correct stream header with nb_frames field.

这篇关于python opencv cv2.cv.CV_CAP_PROP_FRAME_COUNT输入错误的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆