简体   繁体   English

从视频中提取帧以使用opencv进行检测

[英]extract frame from a video for detection with opencv

i'm developing a program to detect object in video or image. 我正在开发一个程序来检测视频或图像中的对象。 It works with the image, but now i want to use it with video. 它适用于图像,但现在我想将其用于视频。 I use a specific folder to pick the image so i wanted to save frames from video in that folder before the detection. 我使用特定的文件夹来选择图像,因此我想在检测之前将视频中的帧保存在该文件夹中。 The variable video and salvataggio are alredy setted. 可变视频和Salvataggio已设置。 In the following code i navigate throw the folder to analize the video: 在以下代码中,我导航抛出该文件夹以分析视频:

DIR *dir;
dir = opendir(video.c_str());
string vidName;
struct dirent *ent;
if (dir != NULL) {
        while ((ent = readdir (dir)) != NULL) {
            vidName= ent->d_name;
            if(vidName.compare(".")!= 0 && vidName.compare("..")!= 0)
            {
            //string vidPath(neg + vidName);
                estraiframe(video, vidName, salvataggio);
            }
        }
        closedir (dir);

}
else {
    cout<<"directory "<< video << " not present"<<endl;
}
}

The function estraiframe save the frame in the output folder. estraiframe函数将帧保存在输出文件夹中。

void estraiframe(string path, string vidName, string output){

string vidPath(path + vidName);
VideoCapture cap(vidPath);
if( !cap.isOpened()){
        cout << "Cannot open the video file" << endl;
        return;
}

double count = cap.get(CV_CAP_PROP_FRAME_COUNT);
double rate = cap.get(CV_CAP_PROP_FPS);
int counter = 0;
for (int i=1; i< count; i+=rate*5)
{


cap.set(CV_CAP_PROP_POS_FRAMES,i);

Mat frame;
cap.read(frame);

counter++;
string nomeframe = to_string(counter) + "-frame_from"+vidName+".jpg";
string percorso (output+nomeframe);
cout << percorso;
imwrite(percorso,frame);
}
}

Apparently it works but after the last frame it gave me the following error: 显然可以,但是在最后一帧之后,它给了我以下错误:

Assertion stream_index < ogg->nstreams failed at libavformat/oggdec.c:898 I locked for it but i didin't find where is the error 断言stream_index <ogg-> nstreams在libavformat / oggdec.c中失败:898我为此锁定,但是我没有发现错误在哪里

Your video contains various stream indexes, eg 3 audios, and 1 video stream would result in 4 stream indexes. 您的视频包含各种流索引,例如3个音频,而1个视频流将产生4个流索引。 I recommend that you check the amount of streams your video contains and print ogg->nstreams to check if this corresponds. 我建议您检查视频中包含的流的数量,并打印ogg-> nstreams以检查是否对应。 Looking at the source code in oggdec.c at line 898 在898行查看oggdec.c中的源代码

static int ogg_read_seek(AVFormatContext *s, int stream_index,
                         int64_t timestamp, int flags)
{
    struct ogg *ogg       = s->priv_data;
    struct ogg_stream *os = ogg->streams + stream_index;
    int ret;

    av_assert0(stream_index < ogg->nstreams); /* Line 898 */

You're clearly going out of bounds here. 您显然在这里越界。

Apparently I manage to solve it, I modified the function that extract the frame. 显然我设法解决了问题,我修改了提取框架的功能。

void estraiframe(string path, string vidName, string output){
string vidPath(path + vidName);
VideoCapture cap(vidPath);
if( !cap.isOpened()){
        cout << "Cannot open the video file" << endl;
        return;
}

double rate = cap.get(CV_CAP_PROP_FPS);
int counter = 0;
Mat frame;
int i=1;

while(1)
{
     cap.read ( frame);
     if( frame.empty()) break;
     counter++;

     if (counter == rate*5*i){
     i++;
     string nomeframe = to_string(counter) + "-frame_from"+vidName+".jpg";
     string percorso (output+nomeframe);
     imwrite(percorso, frame);
     }

     char key = waitKey(10);
     if ( key == 27) break;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM