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 will soon exit with RuntimeError. You can also try setting max_retries and request_timeout like attributes to a higher value to avoid this.
Make sure you use different port value for NetGear and WebGear API.
High CPU utilization may occur on Client's end. User discretion is advised.
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.
# import necessary libsimportuvicorn,asyncio,cv2fromvidgear.gearsimportNetGearfromvidgear.gears.asyncioimportWebGearfromvidgear.gears.asyncio.helperimportreducer# initialize WebGear app without any sourceweb=WebGear(logging=True)# activate jpeg encoding and specify other related parametersoptions={"jpeg_compression":True,"jpeg_compression_quality":90,"jpeg_compression_fastdct":True,"jpeg_compression_fastupsample":True,}# create your own custom frame producerasyncdefmy_frame_producer():# initialize global params# Define NetGear Client at given IP address and define parameters# !!! change following IP address '192.168.x.xxx' with yours !!!client=NetGear(receive_mode=True,address="192.168.x.xxx",port="5454",protocol="tcp",pattern=1,logging=True,**options,)# loop over frameswhileTrue:# receive frames from networkframe=client.recv()# if NoneTypeifframeisNone:break# do something with your OpenCV frame 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)# close streamclient.close()# 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)# 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 required librariesfromvidgear.gearsimportVideoGearfromvidgear.gearsimportNetGearimportcv2# activate jpeg encoding and specify other related parametersoptions={"jpeg_compression":True,"jpeg_compression_quality":90,"jpeg_compression_fastdct":True,"jpeg_compression_fastupsample":True,}# Open live video stream on webcam at first index(i.e. 0) devicestream=VideoGear(source=0).start()# Define NetGear server at given IP address and define parameters # !!! change following IP address '192.168.x.xxx' with client's IP address !!!server=NetGear(address="192.168.x.xxx",port="5454",protocol="tcp",pattern=1,logging=True,**options)# loop over until KeyBoard InterruptedwhileTrue:try:# read frames from streamframe=stream.read()# check for frame if None-typeifframeisNone:break# {do something with the frame here}# send frame to serverserver.send(frame)exceptKeyboardInterrupt:break# safely close video streamstream.stop()# safely close serverserver.close()
Open a terminal on Client System where you want to display the input frames (and setup WebGear_RTC server) received from the Server and execute the following python code:
After running this code, Make sure to open Browser immediately otherwise NetGear will soon exit with RuntimeError. You can also try setting max_retries and request_timeout like attributes to a higher value to avoid this.
Make sure you use different port value for NetGear and WebGear_RTC API.
High CPU utilization may occur on Client's end. User discretion is advised.
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.
For VideoCapture APIs you also need to implement start() in addition to read() and stop() methods in your Custom Streaming Class as shown in following example, otherwise WebGear_RTC will fail to work!
# import necessary libsimportuvicorn,cv2fromvidgear.gearsimportNetGearfromvidgear.gears.helperimportreducerfromvidgear.gears.asyncioimportWebGear_RTC# create your own custom streaming classclassCustom_Stream_Class:""" Custom Streaming using NetGear Receiver """def__init__(self,address=None,port="5454",protocol="tcp",pattern=1,logging=True,**options,):# initialize global params# Define NetGear Client at given IP address and define parametersself.client=NetGear(receive_mode=True,address=address,port=port,protocol=protocol,pattern=pattern,logging=logging,**options)self.running=Falsedefstart(self):# don't forget this function!!!# This function is specific to VideoCapture APIs onlyifnotself.sourceisNone:self.source.start()defread(self):# don't forget this function!!!# check if source was initialized or notifself.sourceisNone:returnNone# check if we're still runningifself.running:# receive frames from networkframe=self.client.recv()# check if frame is availableifnot(frameisNone):# do something with your OpenCV frame here# reducer frames size if you want more performance otherwise comment this lineframe=reducer(frame,percentage=20)# reduce frame by 20%# return our gray framereturnframeelse:# signal we're not running nowself.running=False# return None-typereturnNonedefstop(self):# don't forget this function!!!# flag that we're not runningself.running=False# close streamifnot(self.clientisNone):self.client.close()self.client=None# activate jpeg encoding and specify NetGear related parametersoptions={"jpeg_compression":True,"jpeg_compression_quality":90,"jpeg_compression_fastdct":True,"jpeg_compression_fastupsample":True,}# assign your Custom Streaming Class with adequate NetGear parameters# to `custom_stream` attribute in options parameter of WebGear_RTC.options={"custom_stream":Custom_Stream_Class(address="192.168.x.xxx",port="5454",protocol="tcp",pattern=1,logging=True,**options)}# initialize WebGear_RTC app without any sourceweb=WebGear_RTC(logging=True,**options)# run this app on Uvicorn server at address http://localhost:8000/uvicorn.run(web(),host="localhost",port=8000)# 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 required librariesfromvidgear.gearsimportVideoGearfromvidgear.gearsimportNetGearimportcv2# activate jpeg encoding and specify other related parametersoptions={"jpeg_compression":True,"jpeg_compression_quality":90,"jpeg_compression_fastdct":True,"jpeg_compression_fastupsample":True,}# Open live video stream on webcam at first index(i.e. 0) devicestream=VideoGear(source=0).start()# Define NetGear server at given IP address and define parameters # !!! change following IP address '192.168.x.xxx' with client's IP address !!!server=NetGear(address="192.168.x.xxx",port="5454",protocol="tcp",pattern=1,logging=True,**options)# loop over until KeyBoard InterruptedwhileTrue:try:# read frames from streamframe=stream.read()# check for frame if NonetypeifframeisNone:break# {do something with the frame here}# send frame to serverserver.send(frame)exceptKeyboardInterrupt:break# safely close video streamstream.stop()# safely close serverserver.close()