VideoCapture

class quanser.multimedia.video.VideoCapture(url=None, frame_rate=30.0, frame_width=1920, frame_height=1080, image_format=2, image_data_type=0, attributes=None, num_attributes=0)

A Python wrapper for the Quanser Media 2D API.

Valid URLs:

C:/Users/Me/Videos      - capture video from a file (path specified without file: scheme)
file://localhost/path   - capture video from a file
http://host/path        - capture video from the web (only supported in Windows). The https scheme
should also be supported (or other valid Internet schemes supported by Media Foundation).
video://localhost:id    - capture video from a camera or other device
Note that additional options could potentially be specified as query options in the URL.
Parameters
  • url (string) – The URL.

  • frame_rate (float) – The frame rate.

  • frame_width (int) – The frame width.

  • frame_height (int) – The frame height.

  • image_format (ImageFormat) –

    The format argument describes the format for the image frames retrieved.

    If the image format is ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, then the data is stored in CV_8UC3 format, which is a row-major interleaved format. In other words, the BGR components of each pixel are stored contiguously, with one byte per colour component, and pixels are stored row by row. The data type must be ImageDataType.UINT8 in this case.

    If the image format is ImageFormat.ROW_MAJOR_GREYSCALE, then the data is stored in CV_8UC1 format, which is a row-major format. In other words, there is one byte per pixel, and pixels are stored row by row. The data type must be ImageDataType.UINT8 in this case.

    If the image format is ImageFormat.COL_MAJOR_PLANAR_RGB, then the data is stored in a column-major planar format. In other words, pixels are stored column by column, with all the red components stored together, followed by all the green components, and finally, all the blue components.

    If the image format is ImageFormat.COL_MAJOR_GREYSCALE, then the data is stored in column-major format. In other words, there is one byte per pixel, and pixels are stored column by column.

  • image_data_type (ImageDataType) – The image data type.

  • attributes (array_like) – The video capture attributes.

  • num_attributes (int) – The number of attributes in the attributes array.

Raises

MediaError – On non-zero return code. A suitable error message may be retrieved using get_error_message.

Examples

Constructs a new video capture object without opening a connection to a device.

>>> from quanser.multimedia import VideoCapture
>>> capture = VideoCapture()
>>> # ...
...
>>> capture.close()

Constructs a new video capture object and opens a connection to the specified device with the specified parameters.

>>> from quanser.multimedia import (VideoCapture, ImageFormat, ImageDataType,
...                                 VideoCaptureAttribute, VideoCapturePropertyCode)
>>> url = "video://localhost:0"
>>> frame_rate = 30.0
>>> frame_width = 1920
>>> frame_height = 1080
>>> attributes = [
...     VideoCaptureAttribute(VideoCapturePropertyCode.BRIGHTNESS, 100.0, True),
...     VideoCaptureAttribute(VideoCapturePropertyCode.CONTRAST, 100.0, True)
... ]
...
>>> capture = VideoCapture(url,
...                        frame_rate, frame_width, frame_height,
...                        ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, ImageDataType.UINT8,
...                        attributes, len(attributes))
...
>>> # ...
...
>>> capture.close()
close()

Closes the handle to the underlying video capture object.

Raises

MediaError – On non-zero return code. A suitable error message may be retrieved using get_error_message.

Example

Close the first camera on the local host.

>>> from quanser.multimedia import VideoCapture, ImageDataType, ImageFormat
>>> url = "video://localhost:0"
>>> frame_rate = 30.0
>>> frame_width = 1920
>>> frame_height = 1080
>>> capture = VideoCapture()
>>> capture.open(url,
...              frame_rate, frame_width, frame_height,
...              ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, ImageDataType.UINT8,
...              None, 0)
...
>>> # ...
...
>>> capture.close()
open(url, frame_rate, frame_width, frame_height, image_format, image_data_type, attributes, num_attributes)

Opens a URL for video capture.

Valid URLs:

C:/Users/Me/Videos      - capture video from a file (path specified without file: scheme)
file://localhost/path   - capture video from a file
http://host/path        - capture video from the web (only supported in Windows). The https scheme
should also be supported (or other valid Internet schemes supported by Media Foundation).
video://localhost:id    - capture video from a camera or other device
Note that additional options could potentially be specified as query options in the URL.
Parameters
  • url (string) – The URL.

  • frame_rate (float) – The frame rate.

  • frame_width (int) – The frame width.

  • frame_height (int) – The frame height.

  • image_format (ImageFormat) –

    The format argument describes the format for the image frames retrieved.

    If the image format is ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, then the data is stored in CV_8UC3 format, which is a row-major interleaved format. In other words, the BGR components of each pixel are stored contiguously, with one byte per colour component, and pixels are stored row by row. The data type must be ImageDataType.UINT8 in this case.

    If the image format is ImageFormat.ROW_MAJOR_GREYSCALE, then the data is stored in CV_8UC1 format, which is a row-major format. In other words, there is one byte per pixel, and pixels are stored row by row. The data type must be ImageDataType.UINT8 in this case.

    If the image format is ImageFormat.COL_MAJOR_PLANAR_RGB, then the data is stored in a column-major planar format. In other words, pixels are stored column by column, with all the red components stored together, followed by all the green components, and finally, all the blue components.

    If the image format is ImageFormat.COL_MAJOR_GREYSCALE, then the data is stored in column-major format. In other words, there is one byte per pixel, and pixels are stored column by column.

  • image_data_type (ImageDataType) – The image data type.

  • attributes (array_like) – The video capture attributes.

  • num_attributes (int) – The number of attributes in the attributes array.

Raises

MediaError – On non-zero return code. A suitable error message may be retrieved using get_error_message.

Examples

Open the first camera on the local host.

>>> from quanser.multimedia import VideoCapture, ImageDataType, ImageFormat
>>> url = "video://localhost:0"
>>> frame_rate = 30.0
>>> frame_width = 1920
>>> frame_height = 1080
>>> capture = VideoCapture()
>>> capture.open(url,
...              frame_rate, frame_width, frame_height,
...              ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, ImageDataType.UINT8,
...              None, 0)
...
>>> # ...
...
>>> capture.close()

Open the first camera on the local host with the brightness and contrast both set to 100%.

>>> from quanser.multimedia import (VideoCapture, ImageDataType, ImageFormat,
...                                 VideoCaptureAttribute, VideoCapturePropertyCode)
>>> url = "video://localhost:0"
>>> frame_rate = 30.0
>>> frame_width = 1920
>>> frame_height = 1080
>>> attributes = [
...     VideoCaptureAttribute(VideoCapturePropertyCode.BRIGHTNESS, 100.0, True),
...     VideoCaptureAttribute(VideoCapturePropertyCode.CONTRAST, 100.0, True)
... ]
...
>>> capture = VideoCapture()
>>> capture.open(url,
...              frame_rate, frame_width, frame_height,
...              ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, ImageDataType.UINT8,
...              attributes, len(attributes)
...
>>> # ...
...
>>> capture.close()
read(image_data)

Read one frame from the image source. Returns True if a new image is read. Returns False if no new image is currently available. Otherwise, raises an exception.

See the documentation of the open function for the required data type of the image_data argument.

Parameters

image_data (array_like) – An array to hold the image data. image_data must point to memory large enough to hold the image in the specified format and data type (WxHx3, WxH, HxWx3 or HxW).

Raises

MediaError – On non-zero return code. A suitable error message may be retrieved using get_error_message.

Example

Read 1000 frames from the camera.

>>> import numpy as np
>>> from quanser.multimedia import VideoCapture, ImageDataType, ImageFormat
>>> url = "video://localhost:0"
>>> frame_rate = 30.0
>>> frame_width = 1920
>>> frame_height = 1080
>>> image_data = np.zeros((frame_height, frame_width, 3), dtype=np.uint8)
>>> capture = VideoCapture(url,
...                        frame_rate, frame_width, frame_height,
...                        ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, ImageDataType.UINT8,
...                        None, 0)
...
>>> capture.start()
>>> for i in range(1000):
...     capture.read(image_data)
...     # ...
...
>>> capture.stop()
>>> capture.close()
set_property(attributes, num_attributes)

Set properties for the video capture. The property values will be updated when the next frame is read.

Parameters
  • attributes (array_like) – The video capture attributes.

  • num_attributes (int) – The number of attributes in the attributes array.

Raises

MediaError – On non-zero return code. A suitable error message may be retrieved using get_error_message.

Example

Set the brightness and contrast to 100%.

>>> from quanser.multimedia import VideoCapture, ImageDataType, ImageFormat
>>> url = "video://localhost:0"
>>> frame_rate = 30.0
>>> frame_width = 1920
>>> frame_height = 1080
>>> capture = VideoCapture(url,
...                        frame_rate, frame_width, frame_height,
...                        ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, ImageDataType.UINT8,
...                        None, 0)
...
>>> # ...
...
>>> attributes = [
...     VideoCaptureAttribute(VideoCapturePropertyCode.BRIGHTNESS, 100.0, True),
...     VideoCaptureAttribute(VideoCapturePropertyCode.CONTRAST, 100.0, True)
... ]
...
>>> capture.set_property(attributes, len(attributes))
>>> # ...
...
>>> capture.close()
start()

Starts streaming from the video capture source.

Raises

MediaError – On non-zero return code. A suitable error message may be retrieved using get_error_message.

Example

Start streaming from the first camera.

>>> from quanser.multimedia import VideoCapture, ImageDataType, ImageFormat
>>> url = "video://localhost:0"
>>> frame_rate = 30.0
>>> frame_width = 1920
>>> frame_height = 1080
>>> capture = VideoCapture()
>>> capture.open(url,
...              frame_rate, frame_width, frame_height,
...              ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, ImageDataType.UINT8,
...              None, 0)
...
>>> capture.start()
...
>>> capture.stop()
>>> capture.close()
stop()

Stops streaming from the video capture source.

Raises

MediaError – On non-zero return code. A suitable error message may be retrieved using get_error_message.

Example

Stop streaming from the first camera.

>>> from quanser.multimedia import VideoCapture, ImageDataType, ImageFormat
>>> url = "video://localhost:0"
>>> frame_rate = 30.0
>>> frame_width = 1920
>>> frame_height = 1080
>>> capture = VideoCapture()
>>> capture.open(url,
...              frame_rate, frame_width, frame_height,
...              ImageFormat.ROW_MAJOR_INTERLEAVED_BGR, ImageDataType.UINT8,
...              None, 0)
...
>>> capture.start()
...
>>> capture.stop()
>>> capture.close()