# import librariesfromvidgear.gears.asyncioimportNetGear_Asyncimportasyncio# initialize Server with suitable sourceserver=NetGear_Async(source="/home/foo/foo1.mp4").launch()if__name__=="__main__":# set event loopasyncio.set_event_loop(server.loop)try:# run your main function task until it is completeserver.loop.run_until_complete(server.task)except(KeyboardInterrupt,SystemExit):# wait for interruptspassfinally:# finally close the serverserver.close()
# import librariesfromvidgear.gears.asyncioimportNetGear_Asyncimportcv2,asyncio# define and launch Client with `receive_mode=True`client=NetGear_Async(receive_mode=True).launch()# Create a async function where you want to show/manipulate your received framesasyncdefmain():# loop over Client's Asynchronous Frame Generatorasyncforframeinclient.recv_generator():# do something with received frames here# Show output windowcv2.imshow("Output Frame",frame)key=cv2.waitKey(1)&0xFF# await before continuingawaitasyncio.sleep(0)if__name__=="__main__":# Set event loop to client'sasyncio.set_event_loop(client.loop)try:# run your main function task until it is completeclient.loop.run_until_complete(main())except(KeyboardInterrupt,SystemExit):# wait for interruptspass# close all output windowcv2.destroyAllWindows()# safely close clientclient.close()
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!
# import librariesfromvidgear.gears.asyncioimportNetGear_Asyncimportcv2,asyncio# define and launch Client with `receive_mode=True`. #change following IP address '192.168.x.xxx' with yoursclient=NetGear_Async(address="192.168.x.xxx",port="5454",protocol="tcp",pattern=2,receive_mode=True,logging=True,).launch()# Create a async function where you want to show/manipulate your received framesasyncdefmain():# loop over Client's Asynchronous Frame Generatorasyncforframeinclient.recv_generator():# do something with received frames here# Show output windowcv2.imshow("Output Frame",frame)key=cv2.waitKey(1)&0xFF# await before continuingawaitasyncio.sleep(0)if__name__=="__main__":# Set event loop to client'sasyncio.set_event_loop(client.loop)try:# run your main function task until it is completeclient.loop.run_until_complete(main())except(KeyboardInterrupt,SystemExit):# wait for interruptspass# close all output windowcv2.destroyAllWindows()# safely close clientclient.close()
# import librariesfromvidgear.gears.asyncioimportNetGear_Asyncimportasyncio# initialize Server with suitable sourceserver=NetGear_Async(source=0,address="192.168.x.xxx",port="5454",protocol="tcp",pattern=2,logging=True,).launch()if__name__=="__main__":# set event loopasyncio.set_event_loop(server.loop)try:# run your main function task until it is completeserver.loop.run_until_complete(server.task)except(KeyboardInterrupt,SystemExit):# wait for interruptspassfinally:# finally close the serverserver.close()
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:
# import libraryfromvidgear.gears.asyncioimportNetGear_Asyncimportcv2,asyncio# initialize Server without any sourceserver=NetGear_Async(source=None,logging=True)# !!! define your own video source here !!!# Open any video stream such as live webcam# video stream on first index(i.e. 0) devicestream=cv2.VideoCapture(0)# Create a async frame generator as custom sourceasyncdefmy_frame_generator():# loop over stream until its terminatedwhileTrue:# read frames(grabbed,frame)=stream.read()# check if frame emptyifnotgrabbed:break# do something with the frame to be sent here# yield frameyieldframe# sleep for sometimeawaitasyncio.sleep(0)if__name__=="__main__":# set event loopasyncio.set_event_loop(server.loop)# Add your custom source generator to Server configurationserver.config["generator"]=my_frame_generator()# Launch the Serverserver.launch()try:# run your main function task until it is completeserver.loop.run_until_complete(server.task)except(KeyboardInterrupt,SystemExit):# wait for interruptspassfinally:# close streamstream.release()# finally close the serverserver.close()
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 librariesfromvidgear.gears.asyncioimportNetGear_Asyncimportcv2,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 framesasyncdefmain():# loop over Client's Asynchronous Frame Generatorasyncforframeinclient.recv_generator():# {do something with received frames here}# Show output windowcv2.imshow("Output Frame",frame)key=cv2.waitKey(1)&0xFF# await before continuingawaitasyncio.sleep(0)if__name__=="__main__":# Set event loop to client'sasyncio.set_event_loop(client.loop)try:# run your main function task until it is completeclient.loop.run_until_complete(main())except(KeyboardInterrupt,SystemExit):# wait for interruptspass# close all output windowcv2.destroyAllWindows()# safely close clientclient.close()
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:
# import librariesfromvidgear.gears.asyncioimportNetGear_Asyncimportasyncio# initialize Server with suitable source and enable stabilizationserver=NetGear_Async(source="/home/foo/foo1.mp4",stabilize=True,logging=True).launch()if__name__=="__main__":# set event loopasyncio.set_event_loop(server.loop)try:# run your main function task until it is completeserver.loop.run_until_complete(server.task)except(KeyboardInterrupt,SystemExit):# wait for interruptspassfinally:# finally close the serverserver.close()
# import librariesfromvidgear.gears.asyncioimportNetGear_Asyncfromvidgear.gearsimportWriteGearimportcv2,asyncio# define and launch Client with `receive_mode=True`client=NetGear_Async(receive_mode=True).launch()# Define writer with output filename 'Output.mp4'writer=WriteGear(output="Output.mp4",logging=True)# Create a async function where you want to show/manipulate your received framesasyncdefmain():# loop over Client's Asynchronous Frame Generatorasyncforframeinclient.recv_generator():# {do something with received frames here}# write a modified frame to writerwriter.write(frame)# Show output windowcv2.imshow("Output Frame",frame)key=cv2.waitKey(1)&0xFF# await before continuingawaitasyncio.sleep(0)if__name__=="__main__":# Set event loop to client'sasyncio.set_event_loop(client.loop)try:# run your main function task until it is completeclient.loop.run_until_complete(main())except(KeyboardInterrupt,SystemExit):# wait for interruptspass# close all output windowcv2.destroyAllWindows()# safely close clientclient.close()# safely close writerwriter.close()