Frame Compression for NetGear API¶
Overview¶
NetGear API enables real-time JPEG Frame Compression capabilities for optimizing performance significantly while sending frames over the network.
For enabling Frame Compression, NetGear uses powerful simplejpeg library at its backend, which is based on recent versions of libjpeg-turbo JPEG image codec, to accelerate baseline JPEG compression and decompression on all modern systems. NetGear API employs its exposed decode_jpeg and encode_jpeg methods to encode video-frames to JFIF format before sending it at Server, and cleverly decode it at the Client(s) all in real-time, thereby leveraging performance at cost of minor loss in frame quality.
Frame Compression is enabled by default in NetGear (only if simplejpeg is installed, otherwise frames are sent uncompressed), and can be easily controlled through jpeg_compression_quality, jpeg_compression_fastdct, jpeg_compression_fastupsample, jpeg_compression_colorsubsampling like attributes of its options dictionary parameter during initialization.
Â
Useful Information about Frame Compression
- Frame Compression is enabled by default in NetGear along with fast dct and compression-quality at 90%, but only if
simplejpegis installed. Otherwise thejpeg_compressionattribute is silently ignored, and frames are sent uncompressed. - Exclusive
jpeg_compressionattribute can also be used to disable Frame Compression. - Frame Compression can leverage performance up-to 5-10% with exclusive performance attributes.
- Frame Compression is compatible with all messaging pattern and modes.
- Row-strided frames such as cropped ROIs (e.g.
frame[y0:y1, x0:x1]) are encoded directly, without an extra copy.
Frame Compression is primarily controlled by Server end. That means, if Frame Compression is enabled at Server, then Client(s) will automatically decode the compressed frames. Otherwise if it is disabled, then Client(s) disables it too. However, each end uses its OWN performance attributes, i.e. jpeg_compression_quality, jpeg_compression_colorsubsampling and jpeg_compression_fastdct affect encoding at Server, whereas jpeg_compression_fastdct and jpeg_compression_fastupsample affect decoding at Client(s).
Frame Requirements for Frame Compression
- Frames MUST be of
uint8datatype, and their channel count MUST match the selected colorspace (i.e. 1 channel or a 2-D array forGRAY, 3 channels forBGR/RGB, and 4 channels for others). Otherwise, such frames are sent uncompressed for that frame (with a one-time warning), instead of failing. - JPEG has no alpha channel. Hence with
RGBA,BGRA,ABGRandARGBcolorspaces, the alpha channel is dropped, and decodes as255at Client(s). - Client(s) drop corrupt JPEG frames (logged), and frames whose JPEG header claims more than
2**26pixels (i.e. 8K×8K), instead of crashing.
Â
Exclusive Attributes¶
For implementing Frame Compression, NetGear API currently provide following exclusive attribute for its options dictionary parameter to leverage performance with Frame Compression:
-
jpeg_compression: (bool/str) This internal attribute is used to activate/deactivate JPEG Frame Compression as well as to specify incoming frames colorspace with compression. Its usage is as follows:-
For activating JPEG Frame Compression (Boolean):
In this case, colorspace will default to
BGR.You can set
jpeg_compressionvalue toFalseat Server end to completely disable Frame Compression. -
For specifying Input frames colorspace (String):
In this case, JPEG Frame Compression is activated automatically.
Supported colorspace values are
RGB,BGR,RGBX,BGRX,XBGR,XRGB,GRAY,RGBA,BGRA,ABGR,ARGB,CMYK. More information can be found here âž¶JPEG has no alpha channel, so it is dropped with
RGBA,BGRA,ABGRandARGBcolorspaces (decodes as255). Any other invalid value is skipped with a warning.
-
-
Performance Attributes
¶
-
jpeg_compression_quality: (int/float) This attribute controls the JPEG quantization factor. Its value varies from10to100(the higher is the better quality but performance will be lower). Its default value is90. Its usage is as follows: -
jpeg_compression_fastdct: (bool) This attribute if True, NetGear API uses fastest DCT method that speeds up encoding/decoding by 4-5% for a minor loss in quality. It affects each end independently (i.e. encoding at Server and decoding at Client). Its default value is alsoTrue, and its usage is as follows: -
jpeg_compression_fastupsample: (bool) This attribute if True, NetGear API use fastest color upsampling method while decoding. It only affects decoding at the end it is set on (i.e. Client). Its default value isFalse, and its usage is as follows: -
jpeg_compression_colorsubsampling: (str) This attribute sets the chroma subsampling used while encoding at Server end (ignored withGRAYcolorspace). Its possible values are"444","422","420","440"and"411", and its default value is"422". Client(s) need no change, as the decoder reads it from the JPEG itself. Its usage is as follows:Use
"420"for bandwidth- or CPU-bound Servers (such as a Raspberry Pi). Compared to"422", it produces ~7% fewer bytes and encodes 20–30% faster, for a minor loss in quality (−0.3 to −0.4 dB PSNR).
-
Â
Â
Usage Examples¶
Bare-Minimum Usage¶
Following is the bare-minimum code you need to get started with Frame Compression in NetGear API:
Server End¶
Open your favorite terminal and execute the following python code:
You can terminate both sides anytime by pressing Ctrl+C on your keyboard!
Â
Client End¶
Then open another terminal on the same system and execute the following python code and see the output:
You can terminate client anytime by pressing Ctrl+C on your keyboard!
If compression is enabled at Server, then Client will automatically decode the compressed frames using its own jpeg_compression_fastdct and jpeg_compression_fastupsample attributes.
Â
Â
Bare-Minimum Usage with Variable Colorspace¶
Frame Compression also supports specify incoming frames colorspace with compression. In following bare-minimum code, we will be sending GRAY frames from Server to Client:
New in v0.2.2
This example was added in v0.2.2.
This example works in conjunction with Source ColorSpace manipulation for VideoCapture Gears âž¶
Supported colorspace values are RGB, BGR, RGBX, BGRX, XBGR, XRGB, GRAY, RGBA, BGRA, ABGR, ARGB, CMYK. More information can be found here âž¶
JPEG has no alpha channel, so it is dropped with RGBA, BGRA, ABGR and ARGB colorspaces (decodes as 255).
Server End¶
Open your favorite terminal and execute the following python code:
You can terminate both sides anytime by pressing Ctrl+C on your keyboard!
Â
Client End¶
Then open another terminal on the same system and execute the following python code and see the output:
You can terminate client anytime by pressing Ctrl+C on your keyboard!
If compression is enabled at Server, then Client will automatically decode the compressed frames using its own jpeg_compression_fastdct and jpeg_compression_fastupsample attributes.
Client's end also automatically enforces Server's colorspace, there's no need to define it again.
Â
Â
Using Frame Compression with Variable Parameters¶
Client's End¶
Open a terminal on Client System (where you want to display the input frames received from the Server) and execute the following python code:
Note down the local IP-address of this system(required at Server's end) and also replace it in the following code. You can follow this FAQ for this purpose.
If compression is enabled at Server, then Client will automatically decode the compressed frames using its own jpeg_compression_fastdct and jpeg_compression_fastupsample attributes.
You can terminate client anytime by pressing Ctrl+C on your keyboard!
Â
Server End¶
Now, Open the terminal on another Server System (with a webcam connected to it at index 0), and execute the following python code:
Replace the IP address in the following code with Client's IP address you noted earlier.
You can terminate stream on both side anytime by pressing Ctrl+C on your keyboard!
Â
Â
Using Bidirectional Mode for Video-Frames Transfer with Frame Compression
¶
NetGear now supports Dual Frame Compression for transferring video-frames with its exclusive Bidirectional Mode for achieving unmatchable performance bidirectionally. You can easily enable Frame Compression with its performance attributes at both ends to boost performance bidirectionally.
In this example we are going to implement a bare-minimum example, where we will be sending video-frames (3-Dimensional numpy arrays) of the same Video bidirectionally at the same time for testing the real-time performance and synchronization between the Server and Client using Bidirectional Mode. Furthermore, we're going to use optimal Dual Frame Compression Setting for Sending and Receiving frames at both Server and Client end.
This example is great for building applications like Real-time Video Chat System.
This Dual Frame Compression feature also available for Multi-Clients Mode.
We're also using reducer Helper method for reducing frame-size on-the-go for additional performance.
Remember to define Frame Compression's performance attributes both on Server and Client ends in Dual Frame Compression to boost performance bidirectionally!
Server End¶
Open your favorite terminal and execute the following python code:
You can terminate both side anytime by pressing Ctrl+C on your keyboard!
Everything except numpy.ndarray datatype data is accepted as
target_datainmessageparameter.
Â
Client End¶
Then open another terminal on the same system and execute the following python code and see the output:
You can terminate client anytime by pressing Ctrl+C on your keyboard!
Â