In this example both streams and corresponding frames will be processed synchronously i.e. with no delay:
Using same source with more than one instances of CamGear can lead to Global Interpreter Lock (GIL) that degrades performance even when it is not a bottleneck.
# import required librariesfromvidgear.gearsimportCamGearimportcv2importtime# define and start the stream on first source ( For e.g #0 index device)stream1=CamGear(source=0,logging=True).start()# define and start the stream on second source ( For e.g #1 index device)stream2=CamGear(source=1,logging=True).start()# infinite loopwhileTrue:frameA=stream1.read()# read frames from stream1frameB=stream2.read()# read frames from stream2# check if any of two frame is NoneifframeAisNoneorframeBisNone:#if True break the infinite loopbreak# do something with both frameA and frameB herecv2.imshow("Output Frame1",frameA)cv2.imshow("Output Frame2",frameB)# Show output window of stream1 and stream 2 separatelykey=cv2.waitKey(1)&0xFF# check for 'q' key-pressifkey==ord("q"):#if 'q' key-pressed break outbreakifkey==ord("w"):#if 'w' key-pressed save both frameA and frameB at same timecv2.imwrite("Image-1.jpg",frameA)cv2.imwrite("Image-2.jpg",frameB)#break #uncomment this line to break out after taking imagescv2.destroyAllWindows()# close output window# safely close both video streamsstream1.stop()stream2.stop()
# import required librariesfromvidgear.gearsimportCamGearimportcv2# specify attributesoptions={"STREAM_RESOLUTION":"720p","STREAM_PARAMS":{"nocheckcertificate":True}}# Add YouTube Video URL as input source (for e.g https://youtu.be/bvetuLwJIkA)# and enable Stream Mode (`stream_mode = True`)stream=CamGear(source="https://youtu.be/bvetuLwJIkA",stream_mode=True,logging=True,**options).start()# loop overwhileTrue:# read frames from streamframe=stream.read()# check for frame if NonetypeifframeisNone:break# {do something with the frame here}# Show output windowcv2.imshow("Output",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()
fromvidgear.gearsimportCamGearimportcv2importdatetimeimporttimeclassReconnecting_CamGear:def__init__(self,cam_address,reset_attempts=50,reset_delay=5):self.cam_address=cam_addressself.reset_attempts=reset_attemptsself.reset_delay=reset_delayself.source=CamGear(source=self.cam_address).start()self.running=Truedefread(self):ifself.sourceisNone:returnNoneifself.runningandself.reset_attempts>0:frame=self.source.read()ifframeisNone:self.source.stop()self.reset_attempts-=1print("Re-connection Attempt-{} occured at time:{}".format(str(self.reset_attempts),datetime.datetime.now().strftime("%m-%d-%Y %I:%M:%S%p"),))time.sleep(self.reset_delay)self.source=CamGear(source=self.cam_address).start()# return previous framereturnself.frameelse:self.frame=framereturnframeelse:returnNonedefstop(self):self.running=Falseself.reset_attempts=0self.frame=Noneifnotself.sourceisNone:self.source.stop()if__name__=="__main__":# open any valid video streamstream=Reconnecting_CamGear(cam_address="rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov",reset_attempts=20,reset_delay=5,)# loop overwhileTrue:# read frames from streamframe=stream.read()# check for frame if None-typeifframeisNone:break# {do something with the frame here}# Show output windowcv2.imshow("Output",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()