-
-
Notifications
You must be signed in to change notification settings - Fork 256
Compression in NetGear API
NetGear API now supports real-time frame Encoding/Decoding compression for optimizing performance while sending the frames of large size directly over the network by encoding the frame before sending it and decoding it on the client's end automatically all in real-time. This aid us to achieve better control over the quality of the frame being sent over the network and thereby helps in optimizing the performance at cost quality. Thereby the user can now specify the format and other parameters (such as compression level, compression type) for the outgoing frame over the network easily
To achieve this NetGear API utilizes OpenCV's imencode
& imdecode
methods in conjunction with its flexible APIs at the Server's and Client's respectively. This feature can be easily activated through compression_format
string attribute in **option dictionary parameter at Server's end of the Netgear API during its initialization.
- 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
- Only support for
JPG/JPEG
,PNG
&BMP
encoding formats as of now. - Incorrect Format-specific parameters through
compression_param
attribute are skipped automatically. - This feature as of now only available with the testing branch only!
To manipulate this feature, NetGear API currently provide following attribute of the **option dictionary parameters:
-
compression_format
(string): [For Server's End only] This attribute activates compression with selected encoding format at the server's end. The possible values are.jpg
,.png
,.bmp
. Its usage is as follows:options = {compression_format: '.jpg'} #activates jpeg encoding
💡 See its 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. It's usage is as below:⚠️ 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 for Encoding as follows:
options = {compression_format: '.jpg', 'compression_param':[cv2.IMWRITE_JPEG_OPTIMIZE, 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 integer for Decoding as follows:
options = {'compression_param':cv2.IMREAD_UNCHANGED} #decode image as is with alpha channel
💡 All supported decoding(
Imread
)Flags can be found here
-
💡NOTE: For sake of simplicity in these examples, We will use a bare minimum example, but this feature can be activated/compatible with any NetGear API messaging pattern and exclusive mode like Multi-Server mode similarly.
git clone https://github.com/abhiTronix/vidgear.git
cd vidgear
git checkout testing
sudo pip install .
cd
In this Bare-Minimum example we will send frames from Server's end with jpeg
encoding and enable its optimizations at half compression quality(i.e. 50) and then decode it client's end our local system.
Open your favorite terminal and execute the following python code:
Note::bulb: You can end streaming anytime on both server and client-side by pressing [Ctrl+c] on your keyboard!
# import libraries
from vidgear.gears import VideoGear
from vidgear.gears import NetGear
options = {compression_format: '.jpg', 'compression_param':[cv2.IMWRITE_JPEG_OPTIMIZE, 50]} #activate jpeg encoding and specify parameters
stream = VideoGear(source='test.mp4').start() #Open any video stream
server = NetGear(pattern = 1, logging = True, **options) #Define netgear server
# infinite loop until [Ctrl+C] is pressed
while True:
try:
frame = stream.read()
# read frames
# check if frame is None
if frame is None:
#if True break the infinite loop
break
# do something with frame here
# send frame to server
server.send(frame)
except KeyboardInterrupt:
#break the infinite loop
break
# safely close video stream
stream.stop()
# safely close server
server.close()
Then open another terminal on the same system and execute the following python code and see the output:
# import libraries
from vidgear.gears import NetGear
import cv2
options = {'compression_param':cv2.IMREAD_COLOR} #decode image as 3 channel BGR color image.
#define netgear client with `receive_mode = True`
client = NetGear(pattern = 1, receive_mode = True, logging = True, **options)
# infinite loop
while True:
# receive frames from network
frame = client.recv()
# check if frame is None
if frame is None:
#if True break the infinite loop
break
# do something with frame here
# Show output window
cv2.imshow("Output Frame", frame)
key = cv2.waitKey(1) & 0xFF
# check for 'q' key-press
if key == ord("q"):
#if 'q' key-pressed break out
break
# close output window
cv2.destroyAllWindows()
# safely close client
client.close()