# import required librariesfromvidgear.gearsimportVideoGearfromvidgear.gears.helperimportBackendimportcv2# open any valid video stream(for e.g `myvideo.mp4` file)stream=VideoGear(api=Backend.CAMGEAR,source="myvideo.mp4").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",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()
Select FFGear backend with api=Backend.FFGEAR to access FFGear API for hardware-accelerated FFmpeg-powered decoding.
Following is the bare-minimum code you need to access FFGear API with VideoGear:
FFGear Backend requires the deffcode library
FFGear API MUST have the deffcode library installed, along with a valid FFmpeg executable. Any failure in detection will raise ImportError/RuntimeError immediately.
# import required librariesfromvidgear.gearsimportVideoGearfromvidgear.gears.helperimportBackendimportcv2# select FFGear backend via api parameter# and open any valid video stream(for e.g `myvideo.mp4` file)stream=VideoGear(api=Backend.FFGEAR,source="myvideo.mp4").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",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()
Select PiGear backend with api=Backend.PIGEAR to access PiGear API.
Following is the bare-minimum code you need to access PiGear API with VideoGear:
Under the hood, PiGear API (version 0.3.3 onwards) prioritizes the new picamera2 API backend.
However, PiGear API seamlessly switches to the legacy picamera backend, if the picamera2 library is unavailable or not installed.
It is advised to enable logging(logging=True) to see which backend is being used.
The picamera library is built on the legacy camera stack that is NOT (and never has been) supported on 64-bit OS builds.
You could also enforce the legacy picamera API backend in PiGear by using the enforce_legacy_picamera user-defined optional parameter boolean attribute.
# import required librariesfromvidgear.gearsimportVideoGearfromvidgear.gears.helperimportBackendimportcv2# select PiGear backend via api parameterstream=VideoGear(api=Backend.PIGEAR).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",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()
VideoGear API provides a special internal wrapper around VidGear's Exclusive Video Stabilizer class and provides easy way of activating stabilization for various video-streams (real-time or not) with its stabilize boolean parameter during initialization.
The usage example is as follows:
For a more detailed information on Video-Stabilizer Class, Read here âž¶
The stabilizer might be slower for High-Quality/Resolution videos-frames.
# import required librariesfromvidgear.gearsimportVideoGearimportnumpyasnpimportcv2# open any valid video stream with stabilization enabled(`stabilize = True`)stream_stab=VideoGear(source="test.mp4",stabilize=True).start()# loop overwhileTrue:# read stabilized framesframe_stab=stream_stab.read()# check for stabilized frame if None-typeifframe_stabisNone:break# {do something with the frame here}# Show output windowcv2.imshow("Stabilized Output",frame_stab)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close streamsstream_stab.stop()
VideoGear provides internal access to CamGear, PiGear, and FFGear APIs, and thereby all additional parameters of each backend are also easily accessible within VideoGear API.
The usage example of VideoGear API with Variable Camera Properties is as follows:
This example demonstrates how to use the VideoGear API in a similar manner to the CamGear's example for controlling variable source properties. Any CamGear usage example can be implemented using the VideoGear API in a similar way.
All the supported Source Tweak Parameters can be found here âž¶
# import required librariesfromvidgear.gearsimportVideoGearfromvidgear.gears.helperimportBackendimportcv2# define suitable tweak parameters for your stream.options={"CAP_PROP_FRAME_WIDTH":320,# resolution 320x240"CAP_PROP_FRAME_HEIGHT":240,"CAP_PROP_FPS":60,# framerate 60fps}# To open live video stream on webcam at first index(i.e. 0) # device and apply source tweak parametersstream=VideoGear(api=Backend.CAMGEAR,source=0,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()
VideoGear provides internal access to both CamGear and PiGear APIs, and thereby all additional parameters of PiGear API or CamGear API are also easily accessible within VideoGear API.
The usage example of VideoGear API with Variable Camera Properties is as follows:
This example demonstrates how to use the VideoGear API in a similar manner to the PiGear's example for using variable camera properties. Any PiGear usage example can be implemented using the VideoGear API in a similar way.
Backend PiGear API now fully supports the newer picamera2 python library under the hood for Raspberry Pi camera modules. Follow this guide âž¶ for its installation.
# import required librariesfromvidgear.gearsimportVideoGearfromvidgear.gears.helperimportBackendfromlibcameraimportTransformimportcv2# formulate various Picamera2 API # configurational parametersoptions={"queue":True,"buffer_count":4,"controls":{"Brightness":0.5,"ExposureValue":2.0},"transform":Transform(hflip=1),"auto_align_output_config":True,# auto-align camera configuration}# open pi video stream with defined parametersstream=VideoGear(api=Backend.PIGEAR,resolution=(640,480),framerate=60,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",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()
Under the hood, Backend PiGear API (version 0.3.3 onwards) prioritizes the new picamera2 API backend.
However, the API seamlessly switches to the legacy picamera backend, if the picamera2 library is unavailable or not installed.
It is advised to enable logging(logging=True) to see which backend is being used.
The picamera library is built on the legacy camera stack that is NOT (and never has been) supported on 64-bit OS builds.
You could also enforce the legacy picamera API backend in PiGear by using the enforce_legacy_picamera user-defined optional parameter boolean attribute.
# import required librariesfromvidgear.gearsimportVideoGearfromvidgear.gears.helperimportBackendimportcv2# formulate various Picamera API # configurational parametersoptions={"hflip":True,"exposure_mode":"auto","iso":800,"exposure_compensation":15,"awb_mode":"horizon","sensor_mode":0,}# open pi video stream with defined parametersstream=VideoGear(api=Backend.PIGEAR,resolution=(640,480),framerate=60,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",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()
FFGear API MUST have the deffcode library installed, along with a valid FFmpeg executable. Any failure in detection will raise ImportError/RuntimeError immediately.
The usage example below demonstrates hardware-accelerated decoding and stabilization together using the FFGear backend:
This example demonstrates how to use the VideoGear API in a similar manner to the FFGear's NVIDIA CUVID Decoding example. Any FFGear usage example can be implemented using the VideoGear API in a similar way.
# import required librariesfromvidgear.gearsimportVideoGearfromvidgear.gears.helperimportBackendimportcv2# pass FFmpeg hardware-acceleration and filter options via `options`# use H.264 CUVID hardware decoder; enable OpenCV patch for YUV framesoptions={"-vcodec":"h264_cuvid",# NVIDIA CUVID hardware decoder"-enforce_cv_patch":True,# auto-convert YUV420p → BGR in FFGear"-vf":"scale=1280:720",# apply scale filter}# open video with FFGear backend using hardware acceleration and stabilization enabledstream=VideoGear(api=Backend.FFGEAR,source="myvideo.mp4",stabilize=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",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()
The STABILIZER_MODE option and the StabilizerMode enum were added in v0.3.5. Omitting STABILIZER_MODE keeps the previous behavior — VideoGear defaults to StabilizerMode.ASW.
When stabilization is enabled, VideoGear forwards Stabilizer parameters via the options dictionary:
# import required librariesfromvidgear.gears.stabilizerimportStabilizerModefromvidgear.gearsimportVideoGearimportcv2# configure the stabilizer backend + tuning parametersoptions={"STABILIZER_MODE":StabilizerMode.ASW,# default"SMOOTHING_RADIUS":30,"BORDER_SIZE":5,"CROP_N_ZOOM":True,}# open any valid video stream with stabilization enabledstream_stab=VideoGear(source="test.mp4",stabilize=True,logging=True,**options).start()# loop overwhileTrue:# read stabilized framesframe_stab=stream_stab.read()# check for stabilized frame if None-typeifframe_stabisNone:break# {do something with the frame here}cv2.imshow("Stabilized Output",frame_stab)key=cv2.waitKey(1)&0xFFifkey==ord("q"):breakcv2.destroyAllWindows()stream_stab.stop()
StabilizerMode.KALMAN is reserved for a future release and currently raises NotImplementedError. Non-StabilizerMode values passed via STABILIZER_MODE silently fall back to StabilizerMode.ASW.
# import required librariesfromvidgear.gearsimportVideoGearimportcv2# Open any source of your choice, like Webcam first index(i.e. 0) # and change its colorspace to `HSV`stream=VideoGear(source=0,colorspace="COLOR_BGR2HSV",logging=True).start()# loop overwhileTrue:# read HSV framesframe=stream.read()# check for frame if NonetypeifframeisNone:break# {do something with the HSV frame here}# Show output windowcv2.imshow("Output Frame",frame)# check for key if pressedkey=cv2.waitKey(1)&0xFF# check for 'q' key is pressedifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()