Ip / network camera access.
How to access any type of IP camera using OpenCv? What is the configuration I have to do to run a camera hosted on network IP. What will be the code for that?
asked 2012-11-01 05:24:00 -0600
This post is a wiki. Anyone with karma >50 is welcome to improve it.
How to access any type of IP camera using OpenCv? What is the configuration I have to do to run a camera hosted on network IP. What will be the code for that?
answered 2012-11-02 17:53:37 -0600
This post is a wiki. Anyone with karma >50 is welcome to improve it.
At first you must know the URL for snap (MJPEG) e.g. cvCaptureFromFile(http://ip_addr/snap.jpg); in C. The tail "/snap.jpg" is in Bosch Dinion Ip cameras. In my case it method only works (Linux, ffmpeg + h264 codecs installed), some cameras have problem with encoders and cooperative with installed codecs in system e.g. Bosch support as i saw only windows.
In one of my programs work this function, it's mixed C & C++ operators, in other way it won't work...
Mat GetImage(const char *CameraIP){
IplImage* IplImage;
CvCapture* capture;
capture = cvCaptureFromFile(CameraIP);
assert(capture);
IplImage = cvQueryFrame( capture );
assert(IplImage);
Mat OutImage(IplImage);
return( OutImage );
}
For live stream: 1)C: cvCaptureFromCAM(address); // open stream cvQueryFrame(CvCapture* capture); // get frame
2)C++ vcap.open(videoStreamAddress); // open stream vcap.read(image); // get frame
If you work on linux, and have tried a lot in other ways to resolve the problem, feel free to write about it.
I have read a lot of infos that for RTSP streaming from IP cameras are good framework POCO ([http://pocoproject.org/]).
Good luck
Awesome tip that one about the "/snap.jpg" tail. I have a Bosch and did not know about that, it will be very usefull. Now to see if the rest works...
How would I find out the URL for my camera, it's a Mega-Pixel security camera this model. Onvif protocol. It allows mjpeg and h.264. No idea how to get the url for direct streaming
@SergioBasurco I have a Mega-pixel camera too, model name GTN-IPB812VW, the URL is rtsp://IPADDRESS:554/11 (or 12 for the sub stream).
Hi,
I have one of the "malformed http header" ip cams (vilar IPC-1002), this code (in python) worked at first try, so one can switch it to other languages easily: http://stackoverflow.com/questions/21702477/how-to-parse-mjpeg-http-stream-from-ip-camera
The trick is to search for the start and stop of the jpeg image bypassing the http stuff.
Hope this help others. Pedro
I have found a work-around which is kind of hacky but it will probably work where other options have failed. It worked for me ;)
If you are able to open the stream with vlc (or other player/library) but fail to do it with opencv you can do this.
1 - Get vlc to open the http/rtsp stream from the ip camera.
2 - Set vlc to stream the source video to a file (Instructions here - http://www.ehow.com/how_11401801_stream-videos-internet-using-vlc.html).
You can probably do this step programmatically with a call to vlc executable with some parameters (Hint: you can run vlc without GUI by running cvlc).
3 - Use your preferred method of opening a file with opencv. I tested it this way (its standard video opening code):
int main(int, char**) {
cv::VideoCapture vcap;
cv::Mat image;
const std::string video = "/path/video";
if(!vcap.open(video)) {
std::cout << "Error opening video stream or file" << std::endl;
return -1;
}
for(;;) {
if(!vcap.read(image)) {
std::cout << "No frame" << std::endl;
cv::waitKey();
}
cv::imshow("Output Window", image);
if(cv::waitKey(1) >= 0) break;
}
}
Plus: this method could be improved if instead of redirecting the stream to a file, you could redirect to a device (/dev/something), and maybe opencv would be able to open it.
You can also read this stackoverflow post for more options: http://stackoverflow.com/a/7084081/1085483
You can use the following opencv c++ code for windows to get videostream from your IP WEBCAM app on android phone.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap; //
cap.open("http://192.168.226.101:8080/video?x.mjpeg");
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video cam" << endl;
return -1;
}
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 : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
namedWindow("MyNegativeVideo",CV_WINDOW_AUTOSIZE);
while (1)
{
Mat frame;
Mat contours;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
flip(frame, frame, 1);
imshow("MyVideo", frame); //show the frame in "MyVideo" window
Canny(frame, contours, 500, 1000, 5, true);
imshow("MyNegativeVideo", contours);
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;
break;
}
}
return 0;
}
Here, replace the ip address (192.168.226.101:8080) in
cap.open("http://192.168.226.101:8080/video?x.mjpeg");
with your ip address !! But make sure you have, ?x.mjpeg ,in the end (where x can be any string).
If this doesn't work then you should find out what your full ip address is? i.e. find out what is the string after the ip (In my case it was "/video" as shown in above address)
I also want to develop an app which you can get video and audio stream from IP Camera on Android. So can use this piece of code for develop my app ? I mean does it work for it if I use OpenCV lib on Android. I'm newbie on OpenCV. Any idea about how to do it ? Thanks.
Asked: 2012-11-01 05:24:00 -0600
Seen: 64,209 times
Last updated: Jul 06 '14
Using Open CV with a Network IP Camera
OpenCV code for network camera working for axis not canon
how can i retrieve and process images from an ip camera ,working with c++
OpenCV VideoCapture.open() IP Camera unstable...
How to decrease the number of processed frames from a live video camera?
Which values for window size and number of pyramids are reasonable for calcOpticalFlowPyrLK?