# import required librariesfromvidgear.gearsimportNetGearfromvidgear.gearsimportWriteGearimportcv2# define various tweak flagsoptions={"flag":0,"copy":True,"track":False}# Define Netgear Client at given IP address and define parameters # !!! change following IP address '192.168.x.xxx' with yours !!!client=NetGear(address="192.168.x.xxx",port="5454",protocol="tcp",pattern=1,receive_mode=True,logging=True,**options)# Define writer with default parameters and suitable output filename for e.g. `Output.mp4`writer=WriteGear(output="Output.mp4")# loop overwhileTrue:# receive frames from networkframe=client.recv()# check for received frame if NonetypeifframeisNone:break# {do something with the frame here}# write frame to writerwriter.write(frame)# close output windowcv2.destroyAllWindows()# safely close clientclient.close()# safely close writerwriter.close()
# import required librariesfromvidgear.gearsimportScreenGearfromvidgear.gearsimportNetGear# define dimensions of screen w.r.t to given monitor to be capturedoptions={"top":40,"left":0,"width":100,"height":100}# open stream with defined parametersstream=ScreenGear(logging=True,**options).start()# define various netgear tweak flagsoptions={"flag":0,"copy":True,"track":False}# 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()
# import necessary libsimportuvicorn,cv2fromvidgear.gearsimportScreenGearfromvidgear.gears.asyncioimportWebGear_RTC# assign your ScreenGear class with adequate parameters # to `custom_stream` attribute in options parameteroptions={"custom_stream":ScreenGear(logging=True)}# 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()
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.gearsimportScreenGearfromvidgear.gears.helperimportreducerfromvidgear.gears.asyncioimportWebGear_RTC# create your own custom streaming classclassCustom_Stream_Class:""" Custom Streaming using ScreenGear """def__init__(self,backend="mss",logging=False):# !!! define your own video source here !!!self.source=ScreenGear(backend=backend,logging=logging)# define running flagself.running=Truedefstart(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:# read frame from provided sourceframe=self.source.read()# 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 streamifnotself.sourceisNone:self.source.stop()# assign your Custom Streaming Class with adequate ScreenGear parameters# to `custom_stream` attribute in options parameteroptions={"custom_stream":Custom_Stream_Class(backend="pil",logging=True)}# 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()