Opencv reading IP camera, Video stream, Web camera, images and

Follow

My favourite

  • Opencv tutorial people detection
  • Head people cascade download
  • Opencv tutorial optical flow
  • Opencv Video stabilization
  • Opencv car dataset download
  • Opencv tutorial Transparent mask
  • Opencv videowriter
  • Opencv FFMPEG
  • Opencv Canny edge and hough lines
  • VideoCapture IP camera stream, Web camera, File, images and VideoWriter

    Opencv reading video files, reading video stream, Images, IP and Web cameras. I would like to cover this all in one post. Yes, video writer is also important to store your results and achievements in video. There is couple of simple trick and if you follow them, you will never have a problem with the reading and writing video, stream, files in future.
    opencv web camera



    Basic opencv web camera reading

    There is couple think you need to take care. My favorite installation on windows platform is trough NUGET package system. It is easy in few steps. I describe this many times for example VS 2017 here. Nuget set up your project without any linking settings, library path selection, global environmental variables and you can directly start coding in few seconds. Just select and install nuget and compile code below. Nothing else.  You need to take care if you have included several thinks. highgui.hpp core.hpp, imgproc.hpp, videoio, imgcodecs. All of them are not necessary to read the web camera but for example for video stream from IP camera is possible that you really need them all.

    VideoCapture web camera code

    VideoCapture cap(0); is mean open the default camera web camera. Most of the time this mean web camera on your laptop or plugged in any USB camera. The video is read in 'never ending for(;;) loop' which is break when the video from camera is not available by condition if (!cap.isOpened()). Finally the Mat img;   cap >> img;  copy image from default camera devices into your MAT container. The rest is just display. 

    #include "opencv2\highgui.hpp"
    #include "opencv2\imgproc.hpp"
    #include "opencv2\objdetect\objdetect.hpp"
    #include "opencv2\videoio\videoio.hpp"
    #include "opencv2\imgcodecs\imgcodecs.hpp"
    #include "opencv2\core\core.hpp"
    #include <vector>
    #include <stdio.h>
    #include <windows.h>
    #include <iostream>
    #include <time.h>
    
    
    using namespace cv;
    using namespace std;
    
    
    int main(int argc, const char** argv)
    {
    
    
     VideoCapture cap(0);
     for (;;)
     {
    
    
      if (!cap.isOpened()) {
    
       cout << "Video Capture Fail" << endl;
    
       break;
      }
      else {
       Mat img;
       cap >> img;
       namedWindow("Video", WINDOW_AUTOSIZE);
       imshow("Video", img);
       int key2 = waitKey(20);
      }
    
     }
    
     return 0;
    }
    
    

    Opencv video file reading

    Look at the example above for reading the camera. There is almost no difference. Just one, small and straightforward. As a parameter of cap put instead of default devices cap(0) the file name or path you want to open. There is almost always trouble with path.  In this example you just read the files that are located under your project. You can also read the file from different location or on one place by using the full path into some video folder as you can see in following examples. 

    VideoCapture cap("movie.vmw");
    VideoCapture cap("movie.mp4");
    VideoCapture cap("movie.mov");
    VideoCapture cap("movie.xxx");

    VideoCapture cap("C:/cm/movie.mov");
    VideoCapture cap("C:/cm/movie.mp4");

    Opencv Image read from file and writing 

    This is super easy task. Into the our Mat container  image load the image 6.jpg on this C:/adress/ path. There is something different what is great to have in case you are reading lots of images inside the folder. 

    Mat image;
    image = imread("C:/adress/6.jpg", CV_LOAD_IMAGE_COLOR);

    CV_LOAD_IMAGE_COLOR is defined parameter to tell reader that i want MAT with 3 colors. Basically 3 MAT array container of image size. One for blue, red and green color channel. CV_LOAD_IMAGE_GRAYSCALE is defined to tall reader that i want gray scale. Basically only one Mat of the weight(cols) and high(rows) of the image.. 

    To write results into file just use imwrite where the first string is just name of your result and image. Image is MAT containing what you want to save..

    imwrite("image.jpg", image);


    Opencv video stream verification

    I am using good practice. instead of try stream directly in opencv. I prefer to verify my stream in VLC player. It is faster than modify code and compile again of passing the camera URL as parameter. Also the VLC ask for potential user name and password if its necessary. What is annoying is that all the cameras own stream URL format.  The best approach is to find your IP camera model on http://www.ispyconnect.com and apply to verify inside the VLC. After verification put this directly to VideoCapture cap("http://IP:PORT/mjpeg/video.mjpg?counter"); 

    http://IP:PORT/mjpeg/video.mjpg?counter
    rtsp://IP:PORT/various url
    rtsp://IP:PORT/axis-cgi/mjpg/video.cgi
    http://IP:PORT/mjpg/video.mjpg
    Remember that VLC ask for password Opencv NOT.. Just add rtsp://username:password@IP:PORT
    ("rtsp://USER:PASS@xxx.xxx.xxx.xxx/axis-media/media.amp?camera=2")
    Important FFMPEG is needed in Linux. In case of Nuget packages depends but the stream sometimes needs special installation. 

    Opencv tutorial code IP camera pseudo code

    There is 3 function.. 
    First of all, the main function at the end, where are established 2 threads to read the camera stream..

    In Main
    • Thread call the stream function for both camera with different IP camera URL                       thread cam1(stream, "http://xxxxxxxR");
    • To run the function stream inside the thread with url as parametr use.                       cam1.join();
    void stream

    • Capture video from url strCamera VideoCapture cap(strCamera) 
    • Fill the frame from cap  cap >> frame;
    • Detect people in camera detect(frame, strCamera);
    void detect


    Opencv C++ IP camera code


    
    #include <iostream>
    #include <thread>
    #include "opencv2/opencv.hpp"
    #include <vector>
    using namespace std;
    using namespace cv;
    void detect(Mat img, String strCamera) {
      string cascadeName1 = "haar_cascade_for_people_detection.xml";
      CascadeClassifier detectorBody;
      bool loaded1 = detectorBody.load(cascadeName1);
      Mat original;
      img.copyTo(original);
      vector human;
      cvtColor(img, img, CV_BGR2GRAY);
      equalizeHist(img, img);
      detectorBody.detectMultiScale(img, human, 1.1, 2, 0 | 1, Size(40, 80), Size(400,480 ));
      if (human.size() > 0) 
        {
          for (int gg = 0; gg < human.size(); gg++) 
          {
          rectangle(original, human[gg].tl(), human[gg].br(), Scalar(0, 0, 255), 2, 8, 0);
          }
        }
      imshow("Detect " + strCamera, original);
      int key6 = waitKey(40);
    //End of the detect
    }
    void stream(String strCamera) {
    VideoCapture cap(strCamera);
     if (cap.isOpened()) { 
          while (true) {
            Mat frame;
            cap >> frame; 
            resize(frame, frame, Size(640, 480));  
            detect(frame, strCamera);
         }
       }
    }
    
    int main() {
        thread cam1(stream, "http://xxxxxxxR");
        thread cam2(stream, "http://xxxxxxxR");
        cam1.join();
        cam2.join();
        return 0;
    }
    

    write video into file

    On windows machine i usually works with simple wmv format. Works perfectly. Remember the golden rule of video writer in opencv. image Mat have to match same size as VideoWriter. The image is mat that i want to write as frame into the video.. Before I put them into the VideoWriter, I always resize to target size.. This causing the lots of trouble. You cannot see the video result only for that reason.  
    Size SizeOfVideo = cv::Size(1024, 740);  VideoWriter video("Result.wmv", CV_FOURCC('W', 'M', 'V', '2'), CAP_PROP_FPS,SizeOfVideo, true);
    resize(image, image, Size(800, 600)); 
     video << image; 
    OR
     video.write(image);

    2 comments:

    1. I am using Raspberry pi cam as IP camera and it shows "std::invoke no matching overloaded function found" error.

      ReplyDelete
    2. Your site is actually awe-inspiring. I've discovered numerous brand new points. The right path associated with setting up can also be intriguing. You've chosen really amazing subject. We valued this.
      USA computer vision apps

      ReplyDelete

    ad