在树莓派上使用带有多个网络摄像头的opencv拍摄多个v4l错误以拍摄游戏中时光倒流

时间:2018-10-25 09:09:57

标签: c++ opencv raspberry-pi3 opencv3.0 v4l

我之所以发布此消息,是因为我已经在网上搜索,试图找出引起错误的原因,但没有找到任何错误。

所以我试图从4个不同角度拍摄生长中植物的延时摄影。 我的树莓派3连接了4个网络摄像头(Logitech B525)。 一个小时,所有4个摄像头应拍摄尽可能窄且准确的时间同步帧。

在平稳运行应做的事情后,有时几分钟甚至是几个小时,V4L不再找到所连接的摄像机或发布损坏的信息。 产生错误的摄像机是随机的,因此没有坏的USB端口,没有断线。 一段时间后,出现故障的相机再次开始工作,然后又有另一台相机出现故障,这种情况一直持续到所有相机都出现故障并且程序停止运行。

以下是错误输出的示例

我对保存期的编程输出:

cam 1 attached
cam 2 attached
cam 3 attached
cam 4 attached
18-10-23_10-00-00
saving file of frame 4.. '../cam4/cam4_image_18-10-23_10-00-00.jpg'
18-10-23_10-00-01
saving file of frame 3.. '../cam3/cam3_image_18-10-23_10-00-01.jpg'
18-10-23_10-00-02
saving file of frame 2..'../cam2/cam2_image_18-10-23_10-00-02.jpg'
18-10-23_10-00-02
saving file of frame 1.. '../cam1/cam1_image_18-10-23_10-00-02.jpg'
waiting for next capture!

然后发生错误:

libv4l2: error setting pixformat: Input-/Output Error
VIDEOIO ERROR: libv4l unable to ioctl S_FMT
libv4l2: error setting pixformat: Input-/Output Error
libv4l1: error setting pixformat: Input-/Output Error
libv4l2: error setting pixformat: Input-/Output Error
libv4l1: error setting pixformat: Input-/Output Error
VIDEOIO ERROR: libv4l unable to ioctl VIDIOCSPICT

cam 1 not attached
cam 2 attached
VIDEOIO ERROR: V4L: device /dev/video2: Unable to query number of channels
cam 3 not attached
libv4l2: error setting pixformat: Input-/Output Error
VIDEOIO ERROR: libv4l unable to ioctl S_FMT
libv4l2: error setting pixformat: Input-/Output Error
libv4l1: error setting pixformat: Input-/Output Error
libv4l2: error setting pixformat: Input-/Output Error
libv4l1: error setting pixformat: Input-/Output Error
VIDEOIO ERROR: libv4l unable to ioctl VIDIOCSPICT

cam 4 not attached
sh: 1: reboot: not found
libv4l2: error setting pixformat: Input-/Output Error
VIDEOIO ERROR: libv4l unable to ioctl S_FMT
libv4l2: error setting pixformat: Input-/Output Error
libv4l1: error setting pixformat: Input-/Output Error
libv4l2: error setting pixformat: Input-/Output Error
libv4l1: error setting pixformat: Input-/Output Error
VIDEOIO ERROR: libv4l unable to ioctl VIDIOCSPICT


cam 1 attached
cam 2 attached
cam 3 attached
VIDEOIO ERROR: V4L: device /dev/video3: Unable to query number of channels
cam 4 not attached

摄像机永远不会断开连接,并且始终始终存在于设备文件夹/ dev / videoX中,我想可以断定这不是电源问题。

我的代码中导致错误的部分是开关部分,它以5秒钟的延迟重复一遍又一遍。 我的猜测是USB带带宽问题,因为树莓只有一个USB 2.0总线拆分为4个端口。 我尝试在关闭和再次打开捕获之间增加更多的时间,但没有任何帮助。 我想念什么吗?

这是我的代码:

                switch(4)
                {
                    case 4:
                    {
                        VideoCapture cap4(3);
                        if(!cap4.isOpened())
                        {
                            cout<<"cam 4 not functional or attached!"<<endl;
                        }
                        else
                        {
                            //cap4.set(5, 30); //5=FPS settings
                            //cap4.set(38, 3); //38=Buffersize settings, Frames saved in Buffer
                            cap4.set(3,1280);//3=Width, Pixelcount
                            cap4.set(4,720); //4=Height, Pixelcount
                            Mat frame4;
                            cap4 >> frame4; // get a new frame from camera
                            waitKey(30); //wait for 2 Frames
                            cap4.release();
                            imshow("camera_4", frame4);                             

                            if(frame4.empty())
                            {
                                cerr<<"Something is wrong with the camera 4, could not get frame 4!"<<endl;
                            }
                            else
                            {
                                try
                                {
                                    if(save_next_file)
                                    {
                                        string name4 = "../cam4/cam4_image_"+currentDateTime()+".jpg";
                                        cout<<"saving file of frame 4.. '"<<name4<<"'"<<endl;
                                        imwrite(name4.c_str(),frame4);
                                        waitKey(30);
                                        gettimeofday(&last_save, NULL); //reset the time interval
                                    }
                                }   
                                catch(cv::Exception e)
                                {
                                    cout<<"error saving frame 4. >__<"<<endl;
                                }
                            }
                        }
                    }


                    case 3:
                    {
                        VideoCapture cap3(2);
                        if(!cap3.isOpened())
                        {
                            cout<<"cam 3 not functional or attached!"<<endl;
                        }
                        else
                        {
                            //cap3.set(5, 30); //5=FPS settings
                            //cap3.set(38, 3); //38=Buffersize settings, Frames saved in Buffer
                            cap3.set(3,1280);//3=Width, Pixelcount
                            cap3.set(4,720); //4=Height, Pixelcount
                            Mat frame3;
                            cap3 >> frame3; // get a new frame from camera
                            waitKey(30); //wait for 2 Frames
                            cap3.release();
                            imshow("camera_3", frame3);

                            if(frame3.empty())
                            {
                                cerr<<"Something is wrong with the camrea 3, could not get frame 3. >__<"<<endl;
                            }
                            else
                            {                   
                                try
                                {
                                    if(save_next_file)
                                    {
                                        string name3 = "../cam3/cam3_image_"+currentDateTime()+".jpg";
                                        cout<<"saving file of frame 3.. '"<<name3<<"'"<<endl;
                                        imwrite(name3.c_str(),frame3);
                                        waitKey(25);
                                        gettimeofday(&last_save, NULL); //reset the time interval
                                    }
                                }       
                                catch(cv::Exception e)
                                {
                                    cout<<"error saving frame 3."<<endl;
                                }
                            }
                        }
                    }


                    case 2:
                    {
                        VideoCapture cap2(1);
                        if(!cap2.isOpened())
                        {
                            cout<<"cam 2 not functional or attached!"<<endl;
                        }
                        else
                        {
                            //cap2.set(5, 30); //5=FPS settings
                            //cap2.set(38, 3); //38=Buffersize settings, Frames saved in Buffer
                            cap2.set(3,1280);//3=Width, Pixelcount
                            cap2.set(4,720); //4=Height, Pixelcount
                            Mat frame2;
                            cap2 >> frame2; // get a new frame from camera
                            waitKey(30); //wait for 2 Frames
                            cap2.release();
                            imshow("camera_2", frame2);

                            if(frame2.empty())
                            {
                                cerr << "Something is wrong with the camera 2, could not get frame 2!" << endl;
                            }
                            else
                            {
                                try
                                {
                                    if(save_next_file)
                                    {
                                        string name2 = "../cam2/cam2_image_"+currentDateTime()+".jpg";
                                        cout<<"saving file of frame 2..'"<<name2<<"'"<<endl;
                                        imwrite(name2.c_str(),frame2);
                                        waitKey(25);
                                        gettimeofday(&last_save, NULL); //reset the time interval
                                    }
                                }
                                catch(cv::Exception e)
                                {
                                    cout<<"error saving frame 2. >__<"<<endl;
                                }
                            }
                        }
                    }


                    case 1:
                    {
                        VideoCapture cap1(0);
                        if(cap1.isOpened())
                        if(!cap1.isOpened())
                        {
                            cout<<"cam 1 not functional or attached!"<<endl;
                        }
                        else
                        {
                            //cap1.set(5, 30); //5=FPS settings
                            //cap1.set(38, 3); //38=Buffersize settings, Frames saved in Buffer
                            cap1.set(3,1280);//3=Width, Pixelcount
                            cap1.set(4,720); //4=Height, Pixelcount
                            Mat frame1;
                            cap1 >> frame1; // get a new frame from camera
                            waitKey(30); //wait for 2 Frames
                            cap1.release();
                            imshow("camera_1", frame1);

                            if(frame1.empty())
                            {
                                cerr << "Something is wrong with camera 1, could not get frame 1!" << endl;
                            }
                            else
                            {
                                try
                                {
                                    if(save_next_file)
                                    {
                                        string name1 = "../cam1/cam1_image_"+currentDateTime()+".jpg";
                                        cout<<"saving file of frame 1.. '"<<name1<<"'"<<endl;
                                        imwrite(name1.c_str(),frame1);
                                        waitKey(25);
                                        gettimeofday(&last_save, NULL); //reset the time interval
                                    }
                                }
                                catch(cv::Exception e)
                                {
                                    cout<<"error saving frame 1!"<<endl;
                                }
                            }
                        }
                    }       
                }

2 个答案:

答案 0 :(得分:0)

刚刚发现了这篇文章:https://stackoverflow.com/a/53472812/3643138 尝试用文件名而不是索引打开设备!这对我来说成功了!

答案 1 :(得分:0)

所以大家晚更新。 我发现去年与Raspberry Pi有关。 USB Bandwith和Raspberry pi 3B +的功率不足。 我在计算机上运行的代码与在Raspberry上使用的代码相同,并且没有任何问题。我甚至能够在多线程处理任务的同时运行4个摄像机流。 Pi可以一次处理一个低分辨率的网络摄像头,但是一旦使用另一个摄像头,就无法同时进行流式传输甚至是单个图像捕获。