一段时间后,网络摄像机停止流 [英] IP Camera stop streaming after some time

查看:958
本文介绍了一段时间后,网络摄像机停止流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,我想使用 IP摄像机显示视频流,并通过拍摄的图像其他一些主要操作的网络摄像机

I am working on one application where I want to use IP camera for displaying video streaming and and some other major operations on image captured by the IP Camera.

摄像头捕捉使用的库
。对于照相机捕获:的 Emgu.CV

下面是我正在使用C#其中的代码。

Below is the code which I am using in C#.

变量声明

    private Capture capture;        //takes images from camera as image frames
    private Emgu.CV.UI.ImageBox img; // Dynamic Picture Controls
    private int nCam;               // no of cameras   

代码用于处理图像

  private void ProcessFrame(object sender, EventArgs arg)
  {
    try
          {                
      // Live Streaming Display
     Image<Bgr, Byte> ImageFrame = capture.QueryFrame();

    // If Ip camera try to reinitialize the IP camera
    if(ImageFrame == null)
   {
       capture.Dispose();
       capture = new Capture(URL);                              
        ImageFrame = capture.QueryFrame();
     }                
      ImageFrame = ImageFrame.Resize(img.Width, img.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); 

     img.Image = ImageFrame;

    // Here I am doing some other operations like 
    // 1. Save Image captured from the IP Camera
    // 2. Detect faces in Image 
    // 3. Draw Face markers on Image
    // 4. Some database based on result of Face Detection
    // 4. Delete image File 
    // continue Looping for other Ip Cameras        

     }
      catch (NullReferenceException e)
       {
       }
    }

现在,问题是一段时间后的 QueryFrame()提供价值和摄像机停止流。

Now, The Problem is after some time the QueryFrame() provide null value and camera Stop streaming.

任何一个可以告诉我为什么发生这种情况?
我怎样才能解决这个问题呢?
如果需要更多的信息,请让我知道。

Can any one tell me why this is happening? How I can resolve this problem? If any more information is needed Please Let me know.

在此先感谢。

推荐答案

很抱歉的延迟,但我必须提供一个与众多公共IP摄像机工作的例子。这将需要EMGU参考与您的当前版本和目标生成目录应设置为EMGU Version\bin另外,它解压到文件夹的例子更换。

Sorry about the delay but I have provide an example that works with several public IP cameras. It will need the EMGU reference replacing with your current version and the target build directory should be set to "EMGU Version\bin" alternatively extract it to the examples folder.

http://sourceforge.net/projects/emguexample/files/Capture/CameraCapture%20Public%20IP.zip/download

而不是使用年长QueryFrame()方法,它使用了RetrieveBgrFrame()方法。它一直相当不错,我有没有空例外。然而,如果你做这样的事情更换ProcessFrame()方法。

Rather than using the older QueryFrame() method it uses the RetrieveBgrFrame() method. It has worked reasonably well and I have had no null exceptions. However if you do replace the ProcessFrame() method with something like this

您不应该试图做任何操作,如果返回的帧图像是一个可为空场,并应不会有问题,如果_capture.RetrieveBgrFrame();如果有问题,那么还有一个更大的问题则返回null。

You should not be attempting to do any operations if the frame returned is Image is a nullable field and should not have a problem if _capture.RetrieveBgrFrame(); returns null if there is a problem then there is a bigger issue.

private void ProcessFrame(object sender, EventArgs arg)
{
    //If you want to access the image data the use the following method call
    //Image<Bgr, Byte> frame = new Image<Bgr,byte>(_capture.RetrieveBgrFrame().ToBitmap());

    if (RetrieveBgrFrame.Checked)
    {
        Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();
        //because we are using an autosize picturebox we need to do a thread safe update
        if(frame!=null)
        { 
             DisplayImage(frame.ToBitmap());
             Image<Bgr, Byte> ImageFrame = frame.Resize(img.Width, img.Height,  Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); 

             // Here I am doing some other operations like 
             // 1. Save Image captured from the IP Camera
             // 2. Detect faces in Image 
             // 3. Draw Face markers on Image
             // 4. Some database based on result of Face Detection
             // 4. Delete image File 
             // continue Looping for other Ip Cameras  
        }
        //else do nothing as we have no image
    }
    else if (RetrieveGrayFrame.Checked)
    {
        Image<Gray, Byte> frame = _capture.RetrieveGrayFrame();
        //because we are using an autosize picturebox we need to do a thread safe update
        if (frame != null) DisplayImage(frame.ToBitmap());
    }
}

在一个单独的注意,您的评论继续循环其他IP摄像机可能会导致几个问题。你应该有一个新的捕获您所使用的每个摄像头摄像头的构造。你有多少摄像头使用?什么公共IP摄像机您使用的,所以我可以尝试复制的问题?究其原因,独立的构造函数是IP摄像机需要一段时间来谈判,连接不断处置原有的结构和替换它会严重破坏垃圾收集,如果计时问题引入没有结束。

On a separate note your comment 'continue Looping for other Ip Cameras' may cause several issues. You should have a new Capture constructor for each camera camera you are using. How many camera are you using? and what public ip camera are you using so I can attempt to replicate the issue? The reason for the separate constructor is that ip cameras take a while to negotiate connections with and constantly Disposing of the original construct and replacing it will play havoc with the garbage collector and introduce no end if timing issues.

干杯

克里斯

如果您的相机的超时后返回null帧,然后我会检查,看看是否有与安装或一个问题,也许你的连接很慢断开您减少滞后他人有各种导致但这不是一个代码问题。您可以使用C#单独的数据采集为位图,然后把它传递给图像类型变量。这里有一个伟大的文章:

If your camera is returning null frames after a timeout period then I would check to see if there is an issue with the setup or maybe your connection is so slow it disconnects you to reduce lag to others there are various causes but this is not a code problem. You can use c# alone to acquire the data to a bitmap and then pass this to an Image type variable. There is a great article here:

http://www.codeproject.com/Articles/15537/Camera-Vision-video-surveillance-on-C

我已经适应了这个,所以你可以使用的HttpWebRequest作为最后的检查,看是否流是活的,尽管仍有空例外情况将在这里生产的:

I've adapted this so you can use a HttpWebRequest as a final check to see if the stream is alive although there are still null exceptions that will be produced here:

using System.Net;
using System.IO;

string url;

    private void ProcessFrame(object sender, EventArgs arg)
    {
        //***If you want to access the image data the use the following method call***/
        //Image<Bgr, Byte> frame = new Image<Bgr,byte>(_capture.RetrieveBgrFrame().ToBitmap());

        if (RetrieveBgrFrame.Checked)
        {
            Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();
            //because we are using an autosize picturebox we need to do a thread safe update
            if (frame != null)
            {
                DisplayImage(frame.ToBitmap());

            }
            else
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                // get response
                WebResponse resp = req.GetResponse();
                //get stream
                Stream stream = resp.GetResponseStream();
                if (!stream.CanRead)
                {
                    //try reconnecting the camera
                    captureButtonClick(null, null); //pause
                    _capture.Dispose();//get rid
                    captureButtonClick(null, null); //reconnect
                }
            }
        }
        else if (RetrieveGrayFrame.Checked)
        {
            Image<Gray, Byte> frame = _capture.RetrieveGrayFrame();
            //because we are using an autosize picturebox we need to do a thread safe update
            if (frame != null) DisplayImage(frame.ToBitmap());
        }
    }

    private void captureButtonClick(object sender, EventArgs e)
    {
        url = Camera_Selection.SelectedItem.ToString(); //add this
        ... the rest of the code
    }



要显示多个摄像头,你可以创建一个类来处理捕获结构和processframe事件。理想情况下,你会引发一个特定目的建造的事件调用,其中将包括一个摄像头标识符作为frameready事件调用不会调用它。我必须让事情变得更容易创建形式为MDI父,打开一个对象来管理捕捉变量和帧ready事件。 Alpha版本可以在这里找到:

To display multiple webcams you would create a class to handle the Capture construct and processframe event. Ideally you would raise an purpose built event call that would include a camera identifier as the frameready event call does not call this. I have to make things easier created a form with as a MDI parent and opened an object to manage the capture variables and frame ready event. The Alpha version is available here:

http://sourceforge.net/projects/emguexample/files/Capture/CameraCapture%20Public%20IP%20Multipl%20Display%20Alpha.zip/download

这篇关于一段时间后,网络摄像机停止流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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