JoeC
JoeC

Reputation: 71

How to capture video stream via http with an IP camera using emgucv in c#

I'm having the hardest time trying to find a solution for something I think would be very simple. The Capture Constructor (String) in Emgu.CV should "Create a capture from file or a video stream."

However, I cannot capture anything with my code in C# despite my IP camera (Axis) allowing a video stream as follows: Request a Motion JPEG video stream -> http://myserver/axis-cgi/mjpg/video.cgi (By the way, according to the manufacturer, "A successful request returns a continuous flow of JPEG images. The content type is multipart/x-mixed-replace and each image ends with a boundary string .")

FYI, the camera server does require a username and password login, which I haven't been able to figure out how to include with Capture yet, either... Am I supposed to make a HTTPWebRequest first and then do Capture, or am I supposed to do something much more complicated? Not sure if login may be an issue since I didn't get a specific error on this, but suspect a webrequest may be necessay, which I don't know how to include...

Stripped down code in my form.cs:

Capture _capture = null; //Camera
string sourceURL = "http://192.168.0.90/axis-cgi/mjpg/video.cgi";
_capture = new Capture(sourceURL);
Image<Bgr, Byte> imgOriginal = new Image<Bgr, byte>(_capture.RetrieveBgrFrame().ToBitmap());

Then I try to display imgOriginal in an ImageBox. However, at the last step above, it already generates an error that says "unable to create capture..." or something like this.

Shouldn't this be very simple with emguCV or am I mistaken? If someone can help me figure out how to capture the image, I can take it from there with processing my images. Thank you in advance!

Upvotes: 0

Views: 8450

Answers (2)

rugby2312
rugby2312

Reputation: 1484

Might be too late for this post , but hopefully it'll help someone else in the future.

For MJPEG video codec use ==> http://root:[email protected]/axis-cgi/mjpg/video.cgi?x.mjpeg

For H.264 codec use ==> rtsp://root:[email protected]/axis-media/media.amp?videocodec=h264&resolution=640x480

Please note that these URIs apply only to AXIS brand IP Cameras . For other IP camera brands , I'd suggest you to check the below website , as each manufacturer has a different HTTP or RTSP URI

http://www.soleratec.com/support/rtsp/rtsp_listing

As for the implementation code, here is a headstart :

private static Capture _cameraCapture;

//Windows form button to start the video stream                
private void btn_play_Click(object sender, EventArgs e)
{            
 Run();                            
} 

private void Run()
{
 if (rdbWebcam.Checked == true) //radio button
 {
  _cameraCapture = new Capture(0); //use local webcam
 }
else
  {
  _cameraCapture = new Capture(txtrtsp.Text); //use rtsp or http uri you typed into a textbox
  }
 Application.Idle += ProcessFrame;
}   

private void  ProcessFrame(object sender, EventArgs e)
 {         
   try
       {
        Mat frame = _cameraCapture.QueryFrame();
        imageBox1.Image = frame; //imagebox to  show live video
        }
       catch (Exception ex)
        {
        MessageBox.Show(ex.Message);
         Application.Exit();
         }
}

//Windows Form FormClosing event
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{     
 if (_cameraCapture != null)
    {
     _cameraCapture.Stop();
     _cameraCapture.Dispose();
    }

}

Upvotes: 2

Shiva
Shiva

Reputation: 6887

There are a few things that you might want to try.

  1. First you can use something like fiddler(its a proxy to monitor your web traffic) to check that when application is making the request to the server what response is coming back.
  2. Second if the server requires authentication its very likely its using HTTP Basic authentication you might want to try to call the url something like

    string sourceURL = "http://username:[email protected]/axis-cgi/mjpg/video.cgi"; _capture = new Capture(sourceURL);

or else you will have to send the parameters in Authorization Header

  1. You can use the native cvInvoke function to check that if it helps.The code will be something like this.

    Capture _Capture = new Emgu.CV.CvInvoke.cvCreateFileCapture("http://username:[email protected]/axis-cgi/mjpg/video.cgi");

Please refer to this SO answer more info

Upvotes: 0

Related Questions