Open a terminal on Client System where you want to display the input frames (and setup WebGear server) received from the Server and execute the following python code:
After running this code, Make sure to open Browser immediately otherwise NetGear_Async will soon exit with TimeoutError. You can also try setting timeout parameter to a higher value to extend this timeout.
Make sure you use different port value for NetGear_Async and WebGear API.
High CPU utilization may occur on Client's end. User discretion is advised.
Note down the IP-address of this system (required at Server's end) by executing the hostname -I command and also replace it in the following code."
# import librariesfromvidgear.gears.asyncioimportNetGear_Asyncfromvidgear.gears.asyncioimportWebGearfromvidgear.gears.asyncio.helperimportreducerimportuvicorn,asyncio,cv2# Define NetGear_Async Client at given IP address and define parameters# !!! change following IP address '192.168.x.xxx' with yours !!!client=NetGear_Async(receive_mode=True,pattern=1,logging=True,).launch()# create your own custom frame producerasyncdefmy_frame_producer():# loop over Client's Asynchronous Frame Generatorasyncforframeinclient.recv_generator():# {do something with received frames here}# reducer frames size if you want more performance otherwise comment this lineframe=awaitreducer(frame,percentage=30,interpolation=cv2.INTER_AREA)# reduce frame by 30%# handle JPEG encodingencodedImage=cv2.imencode(".jpg",frame)[1].tobytes()# yield frame in byte formatyield(b"--frame\r\nContent-Type:image/jpeg\r\n\r\n"+encodedImage+b"\r\n")awaitasyncio.sleep(0)if__name__=="__main__":# Set event loop to client'sasyncio.set_event_loop(client.loop)# initialize WebGear app without any sourceweb=WebGear(logging=True)# add your custom frame producer to config with adequate IP addressweb.config["generator"]=my_frame_producer# run this app on Uvicorn server at address http://localhost:8000/uvicorn.run(web(),host="localhost",port=8000)# safely close clientclient.close()# close app safelyweb.shutdown()
On successfully running this code, the output stream will be displayed at address http://localhost:8000/ in your Client's Browser.
# import libraryfromvidgear.gears.asyncioimportNetGear_Asyncimportcv2,asyncio# initialize Server without any sourceserver=NetGear_Async(source=None,address="192.168.x.xxx",port="5454",protocol="tcp",pattern=1,logging=True,)# Create a async frame generator as custom sourceasyncdefmy_frame_generator():# !!! 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)# 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)# close streamstream.release()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:# finally close the serverserver.close()