Metropolis Microservices/RidgeRun Services/PTZ: Difference between revisions
No edit summary |
No edit summary |
||
Line 227: | Line 227: | ||
Once the "stream" model is ready, we send the request to set it into the PTZ Microservice, using this command: | Once the "stream" model is ready, we send the request to set it into the PTZ Microservice, using this command: | ||
<pre> | <pre> | ||
#Send request to set stream in PTZ Microservice | |||
set_stream_result = client.set_stream(stream) | set_stream_result = client.set_stream(stream) | ||
Revision as of 17:48, 25 June 2024
PTZ Microservice is a custom microservice developed by RidgeRun. What makes it special is that it allows you to navigate a 360-degrre video through PTZ. PTZ are the controls available that can be updated at any time during execution, and it means:
- PAN (horizontal)
- TILT (vertical)
- ZOOM (magnify or decrease the view of the image)
This microservice leverages the RidgeRun Spherical Video PTZ to make possible the selection of your region of interest within the sphere.
Briefly, this service works in this way:
- Gets an RTSP stream as an input
- Performs the PTZ depending on the user instructions,
- Returns as the output the transformed stream using the same protocol, RTSP.
mortigoza: Add link to the technical documentation? (please remove this box when addressed) |
API documentation
Usually, in the Microservices architectures the communication with the outside world is handle through well-defined APIs. This APIs specify methods, data formats, and protocols for interaction.
In our PTZ Microservice, the API documentation can be found here, there you can the list of the available requests.
efernandez: We need to point this to the official documentation, not the repo (please remove this box when addressed) |
efernandez: We could include here the link to the project documentation (please remove this box when addressed) |
Running the service
efernandez: Here you can specify that the service can be run directly into the host or using docker. Then describe both options (please remove this box when addressed) |
PTZ Microservice can run as a standalone application or as docker image. The docker approach has some benefits, for example: since the application is encapsulated along with its dependencies, less conflicts arise in the process of deployment.
As a standalone application
Before running the service, you should make sure you have all the dependencies installed. The intructions to do it can be found here: Spherical Video PTZ Building and Installation
mortigoza: Add link to other dependencies as rtsp-sink? (please remove this box when addressed) |
Then you have to clone into your device the repository project, available [/ptz here]
mortigoza: Add link to the repository (please remove this box when addressed) |
The project is configured (via setup.py) to install the service with the name ptz. So to install it run:
pip install .
Then you will have the service with the following options:
usage: ptz [-h] [--port PORT] [--host HOST] [--ptz-window-size PTZ_WINDOW_SIZE] options: -h, --help show this help message and exit --port PORT Port for server --host HOST Server ip address --ptz-window-size PTZ_WINDOW_SIZE Size of the PTZ output window in pixels. The final resolution will be (Size x Size)
As a docker image
Before starting with docker support make sure you have nvidia runtime in your system. Follow these instructions to have docker up and runing in your Jetson Board.
Use prebuild image (dockerhub)
docker pull
efernandez: The service will be hosted in dockerhub, when uploaded we will update this section (please remove this box when addressed) |
Build your own image (Dockerfile)
Build the container
We can build the ptz microservice container using the Dockerfile in the docker directory. This includes a base NVIDA image and the dependencies to run the ptz microservice application.
mortigoza: Add link to the repository (please remove this box when addressed) |
First, we need to prepare the context directory for this build, please create a directory and include all the needed repositories (listed below). The Dockerfile will look for all the source code in the context directory and copy them to the container.
ptz-context/ . ├── gst-cuda ├── gst-rr-panoramaptz ├── gst-rtsp-sink ├── libpanorama ├── ptz └── rrms-utils
Then build the container image running the the following command from the folder containing the Dockerfile and context directory:
sudo docker build \ --network=host \ -f Dockerfile \ -t ridgerun/ptz-service:latest ptz-context/
Change ptz-context to your context's path and the tag (-t) to the name you want to give to your image.
Launch the container
The container can be launched by running the following command:
sudo docker run --runtime nvidia -it --privileged --net=host --ipc=host --name ptz-service ridgerun/ptz-service:latest
You can modify the name you want to give to your container with the option --name.
Here we are creating a container called __ptz-service__ that will start the ptz-service application in the default address and port and using the default output resolution. If a different address, port, or output resolution has to be used, you can do it by running:
sudo docker run --runtime nvidia -it --privileged --net=host --ipc=host --name ptz-service ridgerun/ptz-service:latest --host=HOST --port=PORT --ptz-window-size=PTZ_WINDOW_SIZE
Examples
As we mentioned in the beginning of the wiki. If you see PTZ Microservice as a black box, with an input and an output. It works in this way:
- Get an RTSP stream as the input.
- Performs the PTZ in the stream, depending on the user instructions.
- Returns the stream transformed using the same protocol, RTSP, as the output.
We can use the module available in here**, called rrms_utils, to talk to the PTZ Microservice.
mortigoza: Add link to explanation of how to install rrms-utils? (please remove this box when addressed) |
These are the imports, and commands needed to do it: (Below we explain the purpose of each one)
from rrms_utils.ptz import PTZ from rrms_utils.models.ptz.stream import Stream from rrms_utils.models.ptz.position import Position from rrms_utils.models.ptz.position import Zoom # Create client (by default will talk to address 127.0.0.1 and port 5020) client = PTZ(host=192.168.100.15, port=7000) # Configure Stream stream = Stream(in_uri="rtsp://192.168.100.20:4000/sream_in", out_port=8000, out_mapping="stream_out") set_stream_result = client.set_stream(stream) if not set_stream_result: print("Something went wrong") # Perform PTZ #Change pan and tilt position = Position(pan=50, tilt=20) set_position_result = client.set_position(position) if not set_position_result: print("Something went wrong") # Change zoom zoom = Zoom(zoom=0.5) set_zoom_result = client.set_zoom(zoom) if not set_zoom_result: print("Something went wrong") #Receive the PTZ output - RTSP Stream: vlc rtsp://192.168.100.15:8000/stream_out
Explanation
In the example above we assume the following;
- There is an RTSP Stream being played in the URI: rtsp://192.168.100.20:4000/sream_in,
- The PTZ microservice is runnig in: (IP) host=192.168.100.15, port=7000
Once installed, we can use the rrms_utils module to create a client that "talks" to the server created into the ptz microservices.
mortigoza: Add link to explanation of how to install rrms-utils? (please remove this box when addressed) |
First we have to import the required models:
from rrms_utils.ptz import PTZ from rrms_utils.models.ptz.stream import Stream from rrms_utils.models.ptz.position import Position from rrms_utils.models.ptz.position import Zoom
Then we create the "client" using this command:
# Create client client = PTZ(host=192.168.100.15, port=7000)
By default, like this: PTZ(), it will talk to address 127.0.0.1 and port 5020. In this example we are configuring it to talk to address 192.168.100.15 and port 7000
Here we prepare the "stream" model we want to configure into the PTZ Microservice:
# Configure Stream stream = Stream(in_uri="rtsp://192.168.100.20:4000/sream_in", out_port=8000, out_mapping="stream_out")
- in_uri is the URI where the PTZ Microservice is going to "listen" the incoming RTSP stream
- out_port is the port where the PTZ Microservice is going to locate its output (RTSP stream transformed with the PTZ specified), in this case is the port 8000
- out_mapping is the mapping where the PTZ Microservice is going to locate its output (RTSP stream transformed with the PTZ specified), in this case is /stream_out
Once the "stream" model is ready, we send the request to set it into the PTZ Microservice, using this command:
#Send request to set stream in PTZ Microservice set_stream_result = client.set_stream(stream) if not set_stream_result: print("Something went wrong")
Using the same logic as in the stream. We can send requests to update the PTZ, using this commands:
# Perform PTZ #Set the Position model to send position = Position(pan=50, tilt=20) #Send the request to change pan and tilt set_position_result = client.set_position(position) if not set_position_result: print("Something went wrong")
Using the same logic as in the stream. We can send requests to update the PTZ, using this commands:
#Set the Zoom model to send zoom = Zoom(zoom=0.5) #Send the request to change the zooom set_zoom_result = client.set_zoom(zoom) if not set_zoom_result: print("Something went wrong")
Finally, we can see the output result with VLC using the commands:
#Receive the PTZ output - RTSP Stream: vlc rtsp://192.168.100.15:8000/stream_out
Here the IP, the port and the mapping match with the definitions set in this example.
if you have a different configuration you can modify this command:
#Receive the PTZ output - RTSP Stream: vlc rtsp://ip_addres:port/mapping