Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Live stream to OpenCV via IP camera?

Okay so ultimately I am trying to write an accurate multiple person counting program using OpenCV and Visual Studio 2010. I am quite an OpenCV and C++ newbie so please be quite specific in the responses if possible.

I have recently acquired a Dahua IP camera which I have successfully set up, by set up I mean that I can access the live stream over the internet, and can also access it with OpenCV's video capture class, PROVIDED THAT I USE HTTP PROTOCOL with this URL:

"http://admin:[email protected]:80/cgi-bin/snapshot.cgi?chn=0&u=admin&p=admin&.mjpg"

My problem is this: I can access the stream from my Source.cpp. The code is below:

#include <iostream>
#include <opencv\highgui.h>
#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;

int main(int argc, char* argv[])
    while (1)
    VideoCapture cap("http://admin:[email protected]:80/cgi-bin/snapshot.cgi?chn=0&u=admin&p=admin&.mjpg"); // open the video camera using http protocol with the URL specified 
    // //Just the camera's IP address, I have changed my computers IP to coincide with this.

    if (!cap.isOpened())  // if not success, exit program
        cout << "Cannot open the video cam" << endl;
        return -1;
   cap.set(CV_CAP_PROP_FPS,25); // Feed is already 25fps, I tried setting it to a lower value but nothing      changes.
   double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
   double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

   cout << "Frame size_1 : " << dWidth << " x " << dHeight << endl; //displays dimensions in console window, displays: "Frame size_1 : 1280 x 720" continuously

   cap.set(CV_CAP_PROP_FRAME_WIDTH, 640); //My attempts to change the frame size, pretty futile
   cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

   dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); 
   dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); ////get the width of frames of the video, change dwidth and dheight to these dimension, I expected them to be set to 640 and 480 respectively but it doesn't seem that way. 
   cout << "Frame size_2 : " << dWidth << " x " << dHeight << endl; //Still spitting out: "Frame size_2: 1280 x 720" continuously.

    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

        Mat frame;

        bool bSuccess =; // read a new frame from video

         if (!bSuccess) //if not success, break loop
             cout << "Cannot read a frame from video stream" << endl;

        imshow("MyVideo", frame); //shows the frame in "MyVideo" window

        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
            cout << "esc key is pressed by user" << endl;

    return 0;

So, with the above code I am able to open the stream from my IP camera. My first problem is that I cannot manipulate the feed (set the frame dimensions etc).

My BIGGER problem is that I cannot open the stream in my actual counting program in order to do image processing on it, literally nothing happens. After a bit of googling I've found that I should rather use the rtsp streaming protocol because it enables me to grab frames from the same source instead of redefining my capture class with each iteration in the while loop. I don't really understand how to do this.

I want to be able to stream live from my camera, and do real time image processing on the feed (Motion tracking, colour identification and people counting)

If anyone has any help it would be GREATLY appreciated, my project is due in 2 weeks.