Skip to content

Compression in NetGear API

Abhishek Thakur edited this page Apr 25, 2020 · 16 revisions

VidGear Logo

Frame Encoding/Decoding Compression capabilities for NetGear API

NetGear API now supports real-time frame Encoding/Decoding compression capabilities for optimizing performance while sending frames over the network, by encoding the frame before sending it, and decoding it on the client's end automatically all in real-time.

This feature aid us to achieve better control over the compression of the frame being sent over the network and thereby helps in optimizing the performance at the cost of quality.

To enable this feature, NetGear API utilizes OpenCV's imencode & imdecode methods in conjunction with its flexible APIs at the Server end and Client end respectively.

This feature can be easily activated in NetGear API through compression_format (string) and compression_format (int & list/tuple) attributes of its **option dictionary parameter at the Server and Client end, during its initialization.

 

 

Table of Contents:

 

 

Key Features:

  • enables compression support through resal-time frame encoding on the server-side

  • client-side decodes frame automatically based on the encoding used

  • Encoding and decoding supports all Format-specific flags

  • support for JPG, PNG & BMP encoding formats

  • provides exclusive options attribute compression_format & compression_param to tweak this feature

  • Compatible with any messaging pattern and exclusive Multi-Server mode

 

 


Important Information ⚠️

  • This feature only supports JPG/JPEG, PNG & BMP encoding formats as of now.

  • Incorrect Format-specific parameters through compression_param attribute are skipped automatically.


 

 

Attributes:

To manipulate this feature, NetGear API currently provide following attribute for its **option dictionary parameter:

  • compression_format (string): [For Server's End only] This attribute activates compression with selected encoding format at the server's end. Its possible valid values are: '.jpg'/'.jpeg' or '.png' or '.bmp', and its usage is as follows:

    options = {'compression_format': '.jpg'} #activates jpeg encoding

    💡 See usage example below 🔽.

  • compression_param (int & list/tuple): This attribute allow us to pass different format-specific encoding parameters (such as compression quality) and decoding flags. Its possible value are as follows:

    ⚠️ Wrong/Invalid Format-specific parameters or flag will be skipped/discarded automatically!

    • Encoding Usage(For Server's end only): Its value can be a list/tuple of parameter values for Encoding as follows:

      options = {'compression_format': '.jpg', 'compression_param':[cv2.IMWRITE_JPEG_QUALITY, 80]} # activate jpeg encoding optimizations and compression quality 80

      💡 All supported encoding(Imwrite) Flags can be found here ➶

    • Decoding Usage(For Client's end only): Its value can be a decoding flag(integer) as follows:

      options = {'compression_param':cv2.IMREAD_UNCHANGED} # decode image as is with alpha channel

      💡 All supported decoding(Imread)Flags can be found here ➶

 

 

Basic Usage:

💡 For sake of simplicity in these examples, We will use a bare minimum example, but this feature is compatible with any NetGear API messaging pattern and exclusive mode similarly.

 

In this Bare-Minimum example, we will send frames over the network from Server's end to Client's end with JPEG encoding and set its compression quality to half (i.e. 50), and then decode received frames Client's end automatically:

A. Server End:(Bare-Minimum Example)

Open your favorite terminal and execute the following python code:

Tip 💡 : You can end streaming anytime by pressing [Ctrl+C]/[⌘+C] on your keyboard!

# import required libraries
from vidgear.gears import VideoGear
from vidgear.gears import NetGear

# open any valid video stream(for e.g `test.mp4` file)
stream = VideoGear(source='test.mp4').start()

# activate jpeg encoding and specify other related parameters
options = {'compression_format': '.jpg', 'compression_param':[cv2.IMWRITE_JPEG_QUALITY, 50]} 

#Define Netgear Server with defined parameters
server = NetGear(pattern = 1, logging = True, **options) 

# loop over until KeyBoard Interrupted
while True:

  try: 

     # read frames from stream
    frame = stream.read()

    # check for frame if Nonetype
    if frame is None:
        break

    # {do something with the frame here}

    # send frame to server
    server.send(frame)
  
  except KeyboardInterrupt:
    break

# safely close video stream
stream.stop()

# safely close server
server.close()

B. Client End:(Bare-Minimum example)

Then open another terminal on the same system and execute the following python code and see the output:

# import required libraries
from vidgear.gears import NetGear
import cv2

# define decode image as 3 channel BGR color image
options = {'compression_param':cv2.IMREAD_COLOR}

#define Netgear Client with `receive_mode = True` and defined parameter
client = NetGear(receive_mode = True, pattern = 1, logging = True, **options)

# loop over
while True:

    # receive frames from network
    frame = client.recv()

    # check for received frame if Nonetype
    if frame is None:
        break


    # {do something with the frame here}


    # Show output window
    cv2.imshow("Output Frame", frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close client
client.close()