Video

warning

The document is a continuation of the previous document, if you have landed directly on this page then, Please read from page Get started.

What is Video Component?#

Oops!, No Image to display.
  • Using video component you can capture the video from sources like

    1. Camera
    2. CCTV
    3. Video File
  • Send the captured source to

    1. AI Vision components for processing.
    2. Store video to .avi file.
    3. Display the video on Web UI.

Setup:#

Requirements for using Shunya Interfaces Video Component API's are:

  1. Availability of any one video source. (for example: Webcamera or Video file)
  2. Video component depends on these libraries to run, Hence you need to install these libraries on your device.
    • OpenCV

Install dependencies for Ubuntu 16.04 using the commands

sudo add-apt-repository ppa:lkoppel/opencv
sudo apt-get update
sudo apt install libopencv-calib3d3.2 libopencv-core3.2 libopencv-features2d3.2 libopencv-flann3.2 libopencv-highgui3.2 libopencv-imgcodecs3.2 libopencv-imgproc3.2 libopencv-ml3.2 libopencv-objdetect3.2 libopencv-photo3.2 libopencv-shape3.2 libopencv-stitching3.2 libopencv-superres3.2 libopencv-video3.2 libopencv-videoio3.2 libopencv-videostab3.2 libopencv-viz3.2 libopencv3.2-jni libopencv3.2-java libopencv-contrib3.2
sudo apt install libopencv-calib3d-dev libopencv-core-dev libopencv-features2d-dev libopencv-flann-dev libopencv-highgui-dev libopencv-imgcodecs-dev libopencv-imgproc-dev libopencv-ml-dev libopencv-objdetect-dev libopencv-photo-dev libopencv-shape-dev libopencv-stitching-dev libopencv-superres-dev libopencv-video-dev libopencv-videoio-dev libopencv-videostab-dev libopencv-viz-dev libopencv-contrib-dev libopencv-dev
  1. To install the Video component Run these commands in your terminal

    git clone https://gitlab.iotiot.in/shunya/products/shunya-interfaces/video-component.git
    cd video-component
    mkdir build && cd build
    cmake ../
    make
    sudo make install
    sudo mkdir -p /etc/shunya/
    sudo touch /etc/shunya/config.json

Using the Video Component API#

2 primary uses of Video Component API's are

  1. Capturing Video from sources
  2. Storing video to file.

Capturing Video from sources#

The steps given will help you capture Video from different sources

Step 1: Configure Video source in Shunya Interfaces.#

User needs to configure Shunya Interfaces to the Video source, by writing the configuration file in /etc/shunya/config.json.

Example configuration for Video source#

/etc/shunya/config.json should contain entry.

If you are using a webcam or embedded camera

{
"video-source": {
"cameraId": "0"
}
}
Pro-tip

Lets say your video source changed from video file to webcam, then you dont have to change the whole code, you just need to change the configuration and the video component will adjust accordingly.

Step 2: Write Code to capture from Video source.#

captureObj src;
src = newCaptureDevice("video-source"); /* Create capture object */
Mat frame; /* Variable to store frame */
while(1) {
frame = captureFrameToMem(&src); /* Capture one frame at a time in a loop*/
/* frame variable then can be passed to AI or video processing */
}

That is it done.

info

Checkout ready-to-use Capture Video example#

Ready to use example provides sample code and sample cmake, which can be used to quickly get-started using the component.

Step 3: Compile and Run code#

To compile and Run code

  1. Clone the Example
    git clone https://gitlab.iotiot.in/repo-public/examples.git
    cd examples/simple-examples/capture-video
  2. Compile the program
    mkdir build && cd build
    cmake ../
    make
  3. Make sure that you have written the configuration in "/etc/shunya/config.json"
    {
    "camera": {
    "cameraId": 0
    }
    }
  4. Run the code
    sudo ./capture-video

Storing Video to file#

The steps given will help you store video to file.

Step 1: Configure Video store in Shunya Interfaces.#

User needs to configure Shunya Interfaces to store Video, by writing the configuration file in /etc/shunya/config.json.

Example configuration for Video store#

/etc/shunya/config.json should contain entry.

You can store both images as well as video

If your storing an video file

"videoStore": {
"storeType": "video",
"storePath": "/home/shunya/Desktop",
"videoFps": 10 // FPS of the video stored
}
Pro-tip

Lets say your video store path has changed, then you dont have to change the whole code ore re-compile, you just need to change the configuration and the video component will adjust accordingly.

Step 2: Write Code to store Video to file.#

captureObj src; /* Video capture instance */
storeObj imageStore; /* Image Store instance */
storeObj videoStore; /* Video Store instance */
Mat frame;
src = newCaptureDevice("camera"); /* Create new Capture source */
imageStore = newImageStore("imageStore"); /* Create new Image store */
videoStore = newVideoStore("videoStore"); /* Create new Video store */
while(1) {
frame = captureFrameToMem(&src); /* Capture the frame from source */
storeFrameToDisk(&imageStore, frame, "file.jpg"); /* Store frame as file.jpg*/
storeStreamToDisk(&videoStore, frame, "file.avi"); /* Store frame into file.avi*/
}
/* Close objects and cleanup the memory */
closeCapture(&src);
closeStore(&imageStore);
closeStore(&videoStore);

That is it done.

info

Checkout ready-to-use Store Video example#

Ready to use example provides sample code and sample cmake, which can be used to quickly get-started using the component.

Step 3: Compile and Run code#

To compile and Run code

  1. Clone the Example
    git clone https://gitlab.iotiot.in/repo-public/examples.git
    cd examples/simple-examples/store-video
  2. Compile the program
    mkdir build && cd build
    cmake ../
    make
  3. Make sure that you have written the configuration in "/etc/shunya/config.json"
    {
    "camera": {
    "cameraId": 0
    },
    "videoStore": {
    "storeType": "video",
    "storePath": "/home/shunya/Desktop",
    "videoFps": 10
    },
    "imageStore": {
    "storeType": "image",
    "storePath": "/home/shunya/Desktop"
    }
    }
  4. Run the code
    sudo ./store-video

Facing errors with the component?#