dm_camera Module

Module for taking still images from RTSP video streams and USB webcams, using FFmpeg.

On Linux, install the packages ffmpeg and v4l-utils:

$ sudo apt-get install ffmpeg v4l-utils

List connected USB cameras:

$ v4l2-ctl --list-devices
UVC Camera (046d:0825) (usb-0000:00:1d.7-1):
        /dev/video0
        /dev/video1
        /dev/media0

The following example captures an image from an attached USB webcam at /dev/video0 and adds a timestamp in ISO 8601 format to it using GraphicMagick:

character(len=*), parameter :: IMAGE_PATH = '/tmp/image.jpg'

integer                :: rc
type(camera_type)      :: camera
type(gm_text_box_type) :: text_box

camera = camera_type(input  = '/dev/video0', &
                     device = CAMERA_DEVICE_V4L2, &
                     width  = 1280, &
                     height = 720)

rc = dm_camera_capture(camera, IMAGE_PATH)
if (dm_is_error(rc)) call dm_error_out(rc)

text_box = gm_text_box_type(font='DroidSansMono', font_size=16)
rc = dm_gm_add_text_box(IMAGE_PATH, text=dm_time_now(), text_box=text_box)
if (dm_is_error(rc)) call dm_error_out(rc)

The camera must support the resolution of 1280×720 in this case. If no resolution is specified, the camera default is used. Run ffmpeg(1) to list the supported output dimensions:

$ ffmpeg -f v4l2 -list_formats all -i /dev/video0

RTSP streams are always captured in the stream resolution:

camera = camera_type(input='rtsp://10.10.10.15:8554/camera1', device=CAMERA_DEVICE_RTSP)

The attribute input must be set to the stream URL and may include user name and password.


Uses

  • module~~dm_camera~~UsesGraph module~dm_camera dm_camera module~dm_error dm_error module~dm_camera->module~dm_error module~dm_file dm_file module~dm_camera->module~dm_file module~dm_string dm_string module~dm_camera->module~dm_string module~dm_ascii dm_ascii module~dm_error->module~dm_ascii module~dm_kind dm_kind module~dm_error->module~dm_kind module~dm_file->module~dm_error module~dm_file->module~dm_kind module~dm_string->module~dm_error module~dm_string->module~dm_kind iso_fortran_env iso_fortran_env module~dm_kind->iso_fortran_env

Used by

  • module~~dm_camera~~UsedByGraph module~dm_camera dm_camera module~dmpack dmpack module~dmpack->module~dm_camera

Variables

Type Visibility Attributes Name Initial
integer, public, parameter :: CAMERA_DEVICE_NONE = 0

No device selected.

integer, public, parameter :: CAMERA_DEVICE_RTSP = 1

RTSP stream.

integer, public, parameter :: CAMERA_DEVICE_V4L2 = 2

USB webcam via Video4Linux2.

integer, public, parameter :: CAMERA_DEVICE_LAST = 2

Never use this.

integer, public, parameter :: CAMERA_DEVICE_NAME_LEN = 4
character(len=*), public, parameter :: CAMERA_DEVICE_NAMES(CAMERA_DEVICE_NONE:CAMERA_DEVICE_LAST) = [character(len=CAMERA_DEVICE_NAME_LEN)::'none', 'rtsp', 'v4l2']

Camera device names.


Derived Types

type, public ::  camera_type

Camera settings type.

Components

Type Visibility Attributes Name Initial
character(len=FILE_PATH_LEN), public :: input = ' '

Input device path (/dev/video0 or rtsp://10.0.0.1/).

integer, public :: device = CAMERA_DEVICE_NONE

Input device.

integer, public :: width = 0

Camera stream width in pixels (optional).

integer, public :: height = 0

Camera stream height in pixels (optional).


Functions

public function dm_camera_capture(camera, output, command) result(rc)

Captures a single frame from a V4L2 device or RTSP stream with FFmpeg, and optionally adds a timestamp with GraphicsMagick. If the input is an RTSP stream, the URL must start with rtsp://.

Read more…

Arguments

Type IntentOptional Attributes Name
type(camera_type), intent(in) :: camera

Camera type.

character(len=*), intent(in) :: output

Output file.

character(len=:), intent(out), optional, allocatable :: command

Executed command.

Return Value integer

public pure elemental function dm_camera_device_from_name(name) result(device)

Returns device enumerator from name. On error, the result is CAMERA_DEVICE_NONE.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: name

Device name.

Return Value integer

public pure elemental function dm_camera_device_is_valid(device) result(is)

Returns .true. if device enumerator is valid. The device CAMERA_DEVICE_NONE is invalid.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: device

Camera device enumerator.

Return Value logical