NetGear_Async API Usage Examples:¶
Helpful Tips
-
It is advised to enable logging(
logging = True) on the first run for easily identifying any runtime errors. -
It is advised to comprehend NetGear API before using this API.
After going through following Usage Examples, Checkout more bonus examples here âž¶
Requirement¶
NetGear_Async API is the part of asyncio package of VidGear, thereby you need to install VidGear with asyncio support as follows:
 
Bare-Minimum Usage¶
Following is the bare-minimum code you need to get started with NetGear_Async API:
Server's End¶
Open your favorite terminal and execute the following python code:
You can terminate stream on both side anytime by pressing Ctrl+C on your keyboard! If the Client exits first, server.close() stops waiting for its confirmation after timeout seconds.
Client's End¶
Then open another terminal on the same system and execute the following python code and see the output:
Client will throw TimeoutError if it fails to connect to the Server in given timeout value!
You can terminate client anytime by pressing Ctrl+C on your keyboard!
In Receive Mode, launch() does nothing (it just returns the instance), so calling it at Client's end is optional. Frames are received by iterating recv_generator(), and they are writeable numpy.ndarray arrays.
Â
Using NetGear_Async 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.
Client will throw TimeoutError if it fails to connect to the Server in given timeout value!
You can terminate client anytime by pressing Ctrl+C on your keyboard!
Server's 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! If the Client exits first, server.close() stops waiting for its confirmation after timeout seconds.
Â
Using NetGear_Async with a Custom Source(OpenCV)¶
NetGear_Async allows you to easily define your own custom Source at Server-end that you want to use to transform your frames before sending them onto the network.
Let's implement a bare-minimum example with a Custom Source using NetGear_Async API and OpenCV:
The assigned config["generator"] must be an asynchronous generator. It is validated only when the Server's task starts running (for e.g. in run_until_complete(server.task)), not at launch(), and any invalid value raises ValueError from that task.
Server's End¶
Open your favorite terminal and execute the following python code:
You can terminate stream on both side anytime by pressing Ctrl+C on your keyboard! If the Client exits first, server.close() stops waiting for its confirmation after timeout seconds.
Client's End¶
Then open another terminal on the same system and execute the following python code and see the output:
Client will throw TimeoutError if it fails to connect to the Server in given timeout value!
You can terminate client anytime by pressing Ctrl+C on your keyboard!
# import libraries
from vidgear.gears.asyncio import NetGear_Async
import cv2, asyncio
# define and launch Client with `receive_mode=True`
client = NetGear_Async(receive_mode=True, logging=True).launch()
# Create a async function where you want to show/manipulate your received frames
async def main():
# loop over Client's Asynchronous Frame Generator
async for frame in client.recv_generator():
# {do something with received frames here}
# Show output window
cv2.imshow("Output Frame", frame)
key = cv2.waitKey(1) & 0xFF
# await before continuing
await asyncio.sleep(0)
if __name__ == "__main__":
# Set event loop to client's
asyncio.set_event_loop(client.loop)
try:
# run your main function task until it is complete
client.loop.run_until_complete(main())
except (KeyboardInterrupt, SystemExit):
# wait for interrupts
pass
# close all output window
cv2.destroyAllWindows()
# safely close client
client.close()
Â
Using NetGear_Async with Other Gears¶
NetGear_Async can be used with any other Gears without any compatibility issues.
Let's implement a bare-minimum example where we are sending Stabilized frames from Server-end and saving them at Client's end with WriteGear as follows:
Server's End¶
Open your favorite terminal and execute the following python code:
You can terminate stream on both side anytime by pressing Ctrl+C on your keyboard! If the Client exits first, server.close() stops waiting for its confirmation after timeout seconds.
Client's End¶
Then open another terminal on the same system and execute the following python code and see the output:
Client will throw TimeoutError if it fails to connect to the Server in given timeout value!
You can terminate client anytime by pressing Ctrl+C on your keyboard!
Â
Using NetGear_Async with asyncio.run()¶
NetGear_Async can also be created inside an already running event loop (for e.g. inside asyncio.run()), in which case it uses that loop instead of creating its own.
Closing NetGear_Async
- Inside a running event loop, close NetGear_Async with
await aclose(), or use it asasync with NetGear_Async(...) as gear:to close it automatically. Callingclose()there only schedulesaclose()and returns its task, which you need to await. - Outside a running event loop,
close()runsaclose()to completion, and then closes the event loop owned by NetGear_Async (as in the examples above). Useclose(skip_loop=True)to keep that loop open, which also skips waiting for the Client's termination confirmation. - Server termination is always bounded by
timeout, even if the Client is already gone.
Server's End¶
Open your favorite terminal and execute the following python code:
You can terminate stream on both side anytime by pressing Ctrl+C on your keyboard! If the Client exits first, the Server stops waiting for its confirmation after timeout seconds.
Client's End¶
Then open another terminal on the same system and execute the following python code and see the output:
Client will throw TimeoutError if it fails to connect to the Server in given timeout value!
You can terminate client anytime by pressing Ctrl+C on your keyboard!
Â