Skip to main content

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.

List of features implemented in Shunya stack#

With Shunya stack Video component you can

  1. Capture video frames from these sources.
    1. Camera (USB/Embedded)
    2. Video file.
    3. RTSP Stream (CCTV stream)
  2. Store in 2 formats
    1. Image (Screenshot of one frame)
    2. Video

Using Video component#

Requirements to use Video component#

  1. Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices)
  2. Availability of any one video source. (for example: Webcamera or Video file)

Steps to use Video component#

  1. Set Capture settings in Shunya.
  2. Set Store settings in Shunya.
  3. Implementation for Capture
  4. Implementation for Store
note

Run the steps given below inside Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices) terminals.

Step 1: Set Capture settings in Shunya.#

Set Capture source, by editing the config file /etc/shunya/config.json inside Shunya OS.

A simple configuration should contain these parameters.

ConfigDescription
cameraIdCamera device number (optional)
videoPathFull Path to the Video file (optional)
streamUrlUrl for capturing RTSP/UDP streams. (optional)

Example configuration for Video source#

Writing the below json in `/etc/shunya/config.json` file, will tell the Shunya stack to capture from 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: Set Store settings in Shunya.#

Set Store Location, by editing the config file /etc/shunya/config.json inside Shunya OS.

A simple configuration should contain these parameters.

ConfigDescription
storeTypeStoring format, takes values image or video
storePathFull Path to the folder you want to store.
videoFpsFPS of the output video (only for storeType = video)

Example configuration for Video store#

You can store both images as well as video

Writing the below json in /etc/shunya/config.json file, will tell the Shunya stack to store an video at /home/shunya/Desktop folder

"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 3: Implementation for Capture#

Let's try to implement video Capture using Shunya stack

Steps are :

  1. Start with an ready to use template

    git clone https://gitlab.iotiot.in/repo-public/examples.git
    cd examples/simple-examples/capture-video
  2. Open the capture-video.cpp in an text editor and modify as per your use case.

  3. First we load the JSON settings for Capture, add the code in your main() function

    captureObj src;
    src = newCaptureDevice("video-source"); /* Argument = JSON Title, Load settings from JSON file */
  4. To Capture one frame, modify the code

    cv::Mat frame = captureFrameToMem(&src); /* Capture one frame at a time in a loop*/
    if (frame.empty()){ /* Check if frame is empty*/
    fprintf(stderr, "Frame empty.");
    closeCapture(&src); /* Close Video source and exit */
    return 0;
    }
    /* frame variable contains one frame, this can be sent to other components for processing */
  5. Once you are done editing, save and compile the code , by running

    mkdir build && cd build
    cmake ../
    make
  6. Run the code

    sudo ./capture-video

Step 4: Implementation for Store.#

Let's try to implement video Store using Shunya stack

Steps are :

  1. Start with an ready to use template

    git clone https://gitlab.iotiot.in/repo-public/examples.git
    cd examples/simple-examples/store-video
  2. Open the store-video.cpp in an text editor and modify as per your use case.

  3. First we load the JSON settings for Store, add the code in your main() function

    storeObj videoStore; /* Video Store instance */
    videoStore = newVideoStore("videoStore"); /* Argument = JSON Title, Load settings from JSON file */
  4. To Store one frame, modify the code

    storeStreamToDisk(&videoStore, frame, "file.avi"); /* Store frame into file.avi*/
  5. Cleanup the store operations with,

    closeStore(&videoStore);
  6. Once you are done editing, save and compile the code , by running

    mkdir build && cd build
    cmake ../
    make
  7. Run the code

    sudo ./store-video

Facing errors with the component?#