简体   繁体   中英

How to grab images in JPEG format in higher Fps from IP Camera

Hy Everyone!

I am having a problem in grabbing images from a Panasonic IP Camera in JPEG format infact the problem is with the fps because the fps is always remains 1 or 2 not more than it but infact camera supports upto 30 the cam model is Panasonic WV-SP302E i am using the following C# code to grab the image and display it in my winforms app

public partial class Form1 : Form
{
    // indicates wether to prevent caching in case of a proxy server or not
    private bool preventCaching = false;                

    public Form1()
    {
        InitializeComponent();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        while (true)
        {
            this.pictureBox1.Image = this.GetSingleFrame(@"http://ipaddress/SnapshotJPEG?Resolution=320x240&Quality=Standard");                
        }
    }

    /// <summary>
    /// Get a single JPEG frame from the camera
    /// </summary>
    /// <param name="source">JPEG Stream source</param>
    /// <exception cref="WebException">If the IP camera is not receable or an error is occured</exception>
    /// <exception cref="Exception">If an unknown error occured</exception>
    public Bitmap GetSingleFrame(string source)
    {
        byte[] buffer = new byte[512 * 1024];   // buffer to read stream
        HttpWebRequest req = null;
        WebResponse resp = null;
        Stream stream = null;
        Random rnd = new Random((int)DateTime.Now.Ticks);

        try
        {
            int read, total = 0;

            // create request
            if (!preventCaching)
            {
                req = (HttpWebRequest)WebRequest.Create(source);
            }
            else
            {
                req = (HttpWebRequest)WebRequest.Create(source + ((source.IndexOf('?') == -1) ? '?' : '&') + "fake=" + rnd.Next().ToString());
            }
            // set login and password                
            req.Credentials = new NetworkCredential("root", "a");                

            req.Timeout = -1;

            resp = req.GetResponse();

            // get response stream
            stream = resp.GetResponseStream();

            // loop
            do
            {
                read = stream.Read(buffer, total, 1024);

                total += read;
            }
            while (read != 0);

            Bitmap bmp = (Bitmap)Bitmap.FromStream(new MemoryStream(buffer, 0, total));

            return bmp;
        }
        catch (WebException ex)
        {
            string s = ex.ToString();
            return null;
        }
        catch (Exception ex)
        {
            string s = ex.ToString();
            return null;
        }
        finally
        {
            // abort request
            if (req != null)
            {
                req.Abort();
                req = null;
            }
            // close response stream
            if (stream != null)
            {
                stream.Close();
                stream = null;
            }
            // close response
            if (resp != null)
            {
                resp.Close();
                resp = null;
            }
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.backgroundWorker1.RunWorkerAsync();
    }
}

I am even using the backgrounworker component to grab images in another thread but still 2 fps. Any idea how to increase the fps

Usually you can't query more than few jpeg images / second from an IP camera. If you want a video stream with 30fps, you need to query a "video" stream like motion jpeg, not a snapshot stream.

It has been quite some time since the question, but it is still all the same since then.

The camera offers up to 30 frames per second in streaming mode, however this does not necessarily apply to JPEG snapshot frame rate. Depending on camera model effective JPEG rate might be more or less slower as compared to full speed streaming.

There is little you can actually do about this (it is typical for MPEG-4/H.264 camera to send JPEGs at lower rate), your options are:

  • acquire images from camera through receiving a video feed (which may be a standard protocol like RTSP, or a proprietary protocol through SDK or sort of ActiveX control from camera vendor)
  • replace the camera with a more appropriate model which gets you more JPEG snapshots per second

Looks like you have quite a bit of set up to get that stream going. Taking the allocation out of there so you're not constantly allocating and freeing will help.

Reading multiple frames once you've got the stream going will probably help (ie yielding bitmaps from within your data acquisition loop). FYI, you shouldn't be calling GUI operations from a non-gui thread. Use ReportProgress to send the data back.

Are you certain it's the capture that's taking the time as opposed to displaynig it? Have you tried removing the drawing code to test?

Make sure you're lighting the scene adequately. The simple and somewhat inaccurate explanation is that digital cameras in auto exposure mode will wait until they have captured enough light, which in a dark scene (like a dark room at night) with an inefficient sensor will take a while. Try the camera in a lighter room, or outside in the daylight, and see if your frame rate improves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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