6

I have read many, many threads about streaming images over IP in OpenCV 2.3.1, but I still cannot get my program to work.

I downloaded IP Webcam for Android from https://market.android.com/details?id=com.pas.webcam&hl=en, and recently learned OpenCV to retrieve images from my Android phone camera.

Its built-in manual said that the image from the phone camera can be located at http://the.phone.ip.address:8080/shot.jpg. I have opened it from browser several times and it always looks fine. I also built OpenCV manually, with FFmpeg support.

So far I've tried

CvCapture* webcam = cvCaptureFromFile("http://192.168.1.220:8080/shot.jpg");

but it returns NULL and outputs

[image2 @ 0xd701e0]Could not find codec parameters (Video: mjpeg, yuv420p)

I also tried replacing http with rtsp, but it still doesn't work. I also tried to replace the url with some other image url (one direct link to random image from Google Images, and one from localhost) and it always kills with a segfault.

Here's my full source

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>

int main(int argc, char* argv[])
{ 
  CvCapture* webcam = cvCaptureFromFile("http://192.168.1.220:8080/shot.jpg");
  if(!webcam)
    {
      fprintf(stderr, "cannot open webcam\n");
      return 1;
    }

  IplImage* img = cvQueryFrame(webcam);
  if(!img)
    {
      fprintf(stderr, "cannot get image\n");
      return 1;
    }

  cvNamedWindow("test", CV_WINDOW_AUTOSIZE);
  cvShowImage("test", img);
  cvWaitKey(0);
  cvReleaseImage(&img);
  /**/ cvReleaseCapture(&webcam); /**/
  cvDestroyWindow("test");
  return 0;
}

Can OpenCV really read images over IP, or am I missing something?

Sawi
  • 161
  • 1
  • 6

3 Answers3

4

I'm not specifically familiar with openCV, but having spent a minute looking at the docs, two things jump out at me:-

First off, your're not dealing with a true video stream here: your android app just makes the current JPEG capture available and you have to continually re-sample it. Because it's an Image, not a Video, you should use cvLoadImage() instead.

Second, you're passing a URL, not a filename. You'll need a way to use HTTP to fetch the image to a local file before you try opening it with openCV.

I'd suggest you save a snapshot of the JPEG file locally from your browser, and then try getting your code working with that. Once you have it working from a local file, try adding the HTTP fetching stuff.

Roddy
  • 66,617
  • 42
  • 165
  • 277
  • And what with videostream provided with app. There are some question for example this http://stackoverflow.com/questions/712998/opencv-with-network-cameras Which explains its possible. – krzych Oct 03 '12 at 09:45
2

While it would be /awesome/ if that was supported, it doesn't appear to be. Note that OSes handle opening files differently from URLs (obviously) so it's not something that would be supported by default - you can't fopen() a URL. If OpenCV specifically did support it, it would be possible, but I have some evidence that they do not:

Here's what you can do:

  • Download the image some other way - perhaps by using a system() call to wget, perhaps by using a library to download the file into memory
  • At least one source says you may use Processing's loadImage() to load from a URL.
Dan
  • 10,531
  • 2
  • 36
  • 55
  • So why it recognizes that its mjpeg and in some particular format? I've offered boundy cause wanted to extend this question by accessing this app videofeed any possible way. But my edit to question was rejected. So OpenCV when compiled with ffmpeg should cope videofeed of this app. – krzych Oct 02 '12 at 21:51
  • That is surprising. It may just be defaults, because everything I've read suggests that OpenCV is unable to handle reading from the internet. While I doubt it would solve your problem, shouldn't you be using [VideoCapture](http://opencv.willowgarage.com/documentation/cpp/reading_and_writing_images_and_video.html#videocapture) to read a video? – Dan Oct 02 '12 at 21:56
  • Reading is sometimes not enough. I don't want to explain how it's done in OpenCV but you can see in source. I know that it should be possible, but don't know enough to make it working. Personaly I dislike answers made only by googling something and presenting results. Especially when question is like that – krzych Oct 03 '12 at 09:43
  • 1
    @krzych, the "could not find codec params" is a very misleading message. It doesn't mean that it's detected your file as mjpeg, just that the "filename" passed in ended in ".jpeg". Try changing the URL to http://somenonsenseurl.com/rubbish.jpeg" and see if you get the same message. see here: http://stackoverflow.com/questions/3735823/ffmpeg-not-finding-codec-parameters – Roddy Oct 03 '12 at 16:02
  • @krzych, so you have done research to indicate that the code in OpenCV has support for this, but you don't want to share it. Yet, you hope that someone else will do this for you. Odd. – Prof. Falken Oct 05 '12 at 14:35
0

Further searching on Roddy answer makes that I've made things work for me.

Unfortunatelly it's not nice and windows only solution. But only way to get jpg from this application is to fetch http in some way. There are many libs which can help for example libcurl, boost::asio. I've used urlmon.lib to make things work for me:

#inlcude <opencv2\opencv.hpp>
#include <UrlMon.h>
#include <tchar.h>
#pragma comment(lib,"urlmon.lib")

int main()
{
        for(;;)
        {
              HRESULT hr = URLDownloadToFile(NULL,_T("http://192.168.1.104:8080/shot.jpg"),
                                             _T("D:/test.jpg"),0,NULL);
              IplImage *test = cvLoadImage("D:/test.jpg");
              cvShowImage("test",test);
              cvWaitKey(30);
        }
}

This application can also stream mjpg as I've suggested in comments. OpenCV VideoStream seems to have option to read from it as suggested Stream live video in openCv from localHost port (http://192.168.1.1:8080) and OpenCV with Network Cameras . I've tried also this solution but get mp3 header missing. I hope that someone provide some answer with using videofeed from this application.

Community
  • 1
  • 1
krzych
  • 2,126
  • 7
  • 31
  • 50