RidgeRun Camera Microservice

From RidgeRun Developer Wiki



Preferred Partner Logo 3




RidgeRun's Camera Microservice captures from MIPI cameras and generates an RTSP stream for each camera using RidgeRun's RTSPSink Gstreamer ELement. Additionally, it removes camera distortion using RidgeRun's Cuda Undistort element.

Ridgerun's Camera Microservice General Idea


TL;DR

You can run this microservice with the following command:

docker run \
--runtime nvidia \
-it \
--network host \
--privileged \
-v /tmp/argus_socket:/tmp/argus_socket \
-v /dev/:/dev/ \
-v <configuration file path on host>:/init_config.json \
--name camera-service \
ridgerun/camera-service:latest \
--config_file /init_config.json --host=127.0.0.1 --port=5050
This will run the Camera service in port 5050


Documentation


See API Usage for an example of how to use the API documentation to control the service


Service Configuration

By default the service will not start capturing from any camera, you need to give the service the camera(s) configuration through a file when starting the service or with the configuration request ((see the [service API documentation])).

In both cases, the configuration is defined in JSON format as follows:

{
    "camera0": {
        "index": 0,
        "resolution": {
            "width": 1920,
            "height": 1080
        },
        "streaming": {
            "port": 6060,
            "mapping": "stream0",
            "bitrate": 2000,
            "uri": "rtsp://127.0.0.1:6060/stream0"
        }
    },
    "camera1": {
        "index": 2,
        "resolution": {
            "width": 1920,
            "height": 1080
        },
        "streaming": {
            "port": 6061,
            "mapping": "stream1",
            "bitrate": 2000,
            "uri": "rtsp://127.0.0.1:6061/stream1"
        }
    }
}

This configuration allows to include any number of cameras using this format. The identifies camera0, camera1, cameraN can be any name selected by the user.

  • index: This value corresponds to the camera device index under /dev. For example, an index of 0 will refer to the device /dev/video0.
  • resolution:
    • width: Specifies the width of the video frame to capture, in pixels.
    • height: Specifies the height of the video frame to capture in pixels.
  • streaming:
    • port: Defines the network port on which the video rtsp stream will be available.
    • mapping: Indicates the network mapping for the stream.
    • bitrate: Sets the encoding bitrate in bits per second (bps).
    • uri: RTSP URI of the camera.


Info
The uri entry is included in the retrieved configuration. However, when setting the configuration, this entry is optional. If provided during configuration, it will be ignored, as the service handles this internally.


Running the Service

Using Docker

You can obtain or build a docker image for the camera microservice, check below the method of your preference. The image includes a base L4T image and the dependencies to run the camera microservice application. The image was developed and tested for NVIDIA JetPack 6.0. Once you get your image with either method proceed to Launch the container

Pre-build Image

Info
Contact Ridgerun if you want an evaluation version of the camera service docker image.


Once you obtained the camera-service docker image, you can install it in your jetson with the following command:

docker load < camera-service.tar.gz

Build Image

Info
Contact Ridgerun if you want access to the camera service source code.


You can build the camera service image using the Dockerfile in the docker directory. First we need to prepare the context directory for this build, you need to create a directory and include this repository and the remaining dependencies. The Dockerfile will look for all the packages in the context directory and copy them to the container.

After this, your context directory should look like this:

camera-context/
├── camera
├── cuda-undistort
├── gst-cuda
├── gst-rtsp-sink
└── rrms-utils

Then build the container image with the following command:

docker build \
--network=host \
-f Dockerfile \
-t ridgerun/camera-service:latest \
camera-context/

Change camera-context/ to your context's path and the tag to the name you want to give to the image.

Launch the container

You can ensure the image has been created successfully by running

docker images

You should get an entry showing the ridgerun/camera-service image

nvidia@ubuntu:~$ docker images
REPOSITORY                                  TAG
ridgerun/camera-service                  latest

The container can be launched by running the following command:

docker run \
--runtime nvidia \
-it \
--network host \
--privileged \
-v /tmp/argus_socket:/tmp/argus_socket \
-v /dev/:/dev/ \
--name camera-service \
ridgerun/camera-service:latest

Here we are creating a container called camera-service that will start the camera service application (check Using Standalone Application), launching the server in 127.0.0.1 and port 5050. With this default command you will have to perform a confirmation request in order to properly start capturing. In order to start the camera service with the right configuration you can use the following command.

docker run \
--runtime nvidia \
-it \
--network host \
--privileged \
-v /tmp/argus_socket:/tmp/argus_socket \
-v /dev/:/dev/ \
-v <configuration file path on host>:/init_config.json \
--name camera-service \
ridgerun/camera-service:latest \
--config_file /init_config.json --host=127.0.0.1 --port=5050

Just change <configuration file path on host> with the path to your configuration file in local host.

You can verify the container is running with:

docker ps

You should see an entry with the camera-service container:

nvidia@ubuntu:~$ docker ps
CONTAINER ID   IMAGE                                            COMMAND                  CREATED          STATUS          PORTS     NAMES
529b11d69113   ridgerun/camera-service                       "camera --config…"   24 hours ago     Up 14 minutes             camera-service

Using Standalone Application

You can install the camera service by running the following command from the project directory:

pip install .

Then you will have the service with the following options:

usage: camera [-h] [--config_file CONFIG_FILE] [--port PORT] [--host HOST]

options:
  -h, --help            show this help message and exit
  --config_file CONFIG_FILE
                        JSON file with cameras configuration
  --port PORT           Port for server
  --host HOST           Server ip address

To start the service in address 127.0.0.1 and port 5050 and no initial configuration just run:

camera-service

If you want to serve in a different port or address, use the --port and --host options.

If you want to start with a configuration, you can use the --config_file argument to specify the path to your configuration.