Skip to content

VideoGear API Usage Examples:

After going through following Usage Examples, Checkout more of its advanced configurations here ➶

Bare-Minimum Usage with CamGear backend

VideoGear by default provides direct internal access to CamGear API.

Following is the bare-minimum code you need to access CamGear API with VideoGear:

# import required libraries
from vidgear.gears import VideoGear
import cv2


# open any valid video stream(for e.g `myvideo.avi` file)
stream = VideoGear(source="myvideo.avi").start()

# loop over
while True:

    # read frames from stream
    frame = stream.read()

    # check for frame if Nonetype
    if frame is None:
        break

    # {do something with the frame here}

    # Show output window
    cv2.imshow("Output Frame", frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.stop()

 

Bare-Minimum Usage with PiGear backend

VideoGear contains a special enablePiCamera flag that when True provides internal access to PiGear API.

Following is the bare-minimum code you need to access PiGear API with VideoGear:

Make sure to enable Raspberry Pi hardware-specific settings prior using PiGear Backend, otherwise nothing will work.

# import required libraries
from vidgear.gears import VideoGear
import cv2

# enable enablePiCamera boolean flag to access PiGear API backend
stream = VideoGear(enablePiCamera=True).start()

# loop over
while True:

    # read frames from stream
    frame = stream.read()

    # check for frame if Nonetype
    if frame is None:
        break

    # {do something with the frame here}

    # Show output window
    cv2.imshow("Output Frame", frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.stop()

 

Using VideoGear with Video Stabilizer backend

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 libraries
from vidgear.gears import VideoGear
import numpy as np
import cv2

# open any valid video stream with stabilization enabled(`stabilize = True`)
stream_stab = VideoGear(source="test.mp4", stabilize=True).start()

# loop over
while True:

    # read stabilized frames
    frame_stab = stream_stab.read()

    # check for stabilized frame if None-type
    if frame_stab is None:
        break

    # {do something with the frame here}

    # Show output window
    cv2.imshow("Stabilized Output", frame_stab)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close streams
stream_stab.stop()

 

Advanced VideoGear usage with CamGear Backend

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:

Info

This example is basically a VideoGear API implementation of this CamGear usage example for controlling its properties (such as its brightness, saturation, resolution, framerate, gain etc.). Thereby, any CamGear or PiGear usage examples can be implemented with VideoGear API in the similar manner.

All the supported Source Tweak Parameters can be found here ➶

# import required libraries
from vidgear.gears import VideoGear
import cv2


# 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 parameters
stream = VideoGear(source=0, logging=True, **options).start()

# loop over
while True:

    # read frames from stream
    frame = stream.read()

    # check for frame if Nonetype
    if frame is None:
        break

    # {do something with the frame here}

    # Show output window
    cv2.imshow("Output", frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.stop()

 

Advanced VideoGear usage with PiGear Backend

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 PiCamera Properties is as follows:

Info

This example is basically a VideoGear API implementation of this PiGear usage example. Thereby, any CamGear or PiGear usage examples can be implemented with VideoGear API in the similar manner.

Make sure to enable Raspberry Pi hardware-specific settings prior using PiGear Backend, otherwise nothing will work.

# import required libraries
from vidgear.gears import VideoGear
import cv2

# add various Picamera tweak parameters to dictionary
options = {
    "hflip": True,
    "exposure_mode": "auto",
    "iso": 800,
    "exposure_compensation": 15,
    "awb_mode": "horizon",
    "sensor_mode": 0,
}

# activate enablePiCamera and open pi video stream with defined parameters
stream = VideoGear(
    enablePiCamera=True, resolution=(640, 480), framerate=60, logging=True, **options
).start()

# loop over
while True:

    # read frames from stream
    frame = stream.read()

    # check for frame if Nonetype
    if frame is None:
        break

    # {do something with the frame here}

    # Show output window
    cv2.imshow("Output Frame", frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.stop()

 

Using VideoGear with Colorspace Manipulation

VideoGear API also supports Colorspace Manipulation but NOT Direct like other VideoCapture Gears.

Important

  • color_space global variable is NOT Supported in VideoGear API, calling it will result in AttribueError. More details can be found here ➶

  • Any incorrect or None-type value on colorspace parameter will be skipped automatically.

In following example code, we will convert source colorspace to HSV on initialization:

# import required libraries
from vidgear.gears import VideoGear
import cv2

# 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 over
while True:

    # read HSV frames
    frame = stream.read()

    # check for frame if Nonetype
    if frame is None:
        break

    # {do something with the HSV frame here}

    # Show output window
    cv2.imshow("Output Frame", frame)

    # check for key if pressed
    key = cv2.waitKey(1) & 0xFF

    # check for 'q' key is pressed
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.stop()

 

Bonus Examples

Checkout more advanced VideoGear examples with unusual configuration here ➶

 


Last update: June 27, 2022