Запись / сохранение видеопотока, если обнаружение истинно

Я хочу написать видеопоток после того, как обнаружение будет найдено верным.

Я использую эту ссылку как Пример видео

Моя реализация кода выглядит так:

int main(int argc, const char** argv) {
bool detection = false;
VideoCapture cap(-1);

if (!cap.isOpened())
{
printf("ERROR: Cannot open the video file");
}
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE);

double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
cout << "Frame Size = " << dWidth << "x" << dHeight << endl;
Size frameSize (static_cast<int>(dWidth), static_cast<int>(dHeight));

VideoWriter record ("/home/hacker/MyVideo.avi", CV_FOURCC('P','I','M','1'),
30, frameSize, true);

if (!record.isOpened())
{
printf("Error: Failed to write the video");
return -1;
}

while (true)
{
Mat frame;

if (!frame.empty())
{
detectAndDisplay(frame);
}
else
{
printf(" --(!) No captured frame -- Break!"); break;
}

if (detection == true)
{
record.write(frame);
}

char c = cvWaitKey(33);
if (c == 27) { break; }
}
return 0;
}

В моем домашнем каталоге я вижу Myvideo.avi, но он пуст.

Я получил следующие ошибки в командной строке:

VIDIOC_QUERMENU: Invalid argument
VIDIOC_QUERMENU: Invalid argument
Frame size: 640x480  Output #0, avi, to '/home/hacker/MyVideo.avi":
Stream #0.0: Video: mpeg1video (hq), yvu420p, 640x480, q=2-31, 19660
kb/s, 9ok tbn, 23,98 tbc
--(!) No captured frame -- Break! Process returned 0 (0x0) execution time: 0,75 s

0

Решение

Вы должны выпустить VideoWriter (record.Release ();). Он закрывает файл.

0

Другие решения

Я пытаюсь решить это так:

Но у меня есть 2 проблемы:

Я хочу сохранить MyVideo.avi, если detecAndDisplay (frame) == true. Но он все равно его сохраняет (с пустой видеозаписью). И если это сохранить запись видео работает быстрее.

int main( int argc, const char** argv ) {

Mat frame;VideoCapture capture(-1); // open the default camera
if( !capture.isOpened() )
{
printf("Camera failed to open!\n");
return -1;
}

capture >> frame; // get first frame for size

for(;;)
{
// get a new frame from camera
capture >> frame;
//-- 3. Apply the classifier to the frame
if( !frame.empty() )
{
detectAndDisplay( frame );
}

if(detectAndDisplay(frame)==true)
{
// record video
VideoWriter record("MyVideo.avi", CV_FOURCC('D','I','V','X'), 30, frame.size(), true);

if( !record.isOpened() )
{
printf("VideoWriter failed to open!\n");
return -1;
}

// add frame to recorded
record << frame;
}if(waitKey(30) >= 0) break;
}

return 0;
}/** @function detectAndDisplay */
/** this function detect face draw a rectangle around and detect eye & mouth and draw a circle around */
bool detectAndDisplay( Mat frame ) {
...
...
return true;

}
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector