arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | List of all members
CameraFrameSourceOpenCV Class Reference

VideoFrameSource that pulls frames from an OpenCV cv::VideoCapture (e.g. a USB/CSI webcam via V4L2 on Linux/Raspberry Pi, or any other OpenCV-supported backend) - the desktop/Linux counterpart to CameraFrameSource, which targets the ESP32 esp_camera.h driver instead. More...

#include <CameraFrameSourceOpenCV.h>

Inheritance diagram for CameraFrameSourceOpenCV:
VideoFrameSource

Public Member Functions

 CameraFrameSourceOpenCV ()=default
 
 CameraFrameSourceOpenCV (int device)
 
bool begin ()
 
void end ()
 Releases the capture device.
 
const uint8_t * nextFrame (size_t &len) override
 
void setApiPreference (int api)
 
void setDevice (const char *device)
 
void setJpegQuality (int quality)
 
void setVideoInfo (VideoInfo info)
 
VideoInfo videoInfo () override
 
bool writeTo (Print &out)
 Write the frame to the indicated destination.
 

Protected Attributes

int api_preference = cv::CAP_ANY
 
cv::VideoCapture cap
 
int device_index = 0
 
const char * device_string = nullptr
 
cv::Mat frame
 
VideoInfo info
 
std::vector< uint8_t > jpeg_buffer
 
int jpeg_quality = 90
 

Detailed Description

VideoFrameSource that pulls frames from an OpenCV cv::VideoCapture (e.g. a USB/CSI webcam via V4L2 on Linux/Raspberry Pi, or any other OpenCV-supported backend) - the desktop/Linux counterpart to CameraFrameSource, which targets the ESP32 esp_camera.h driver instead.

nextFrame() grabs a picture with cv::VideoCapture::read() and, by default (videoInfo().format == VideoFormat::MJPEG), JPEG-encodes it via cv::imencode() so the result matches CameraFrameSource's own already-encoded-JPEG contract. setVideoInfo() with VideoFormat::RAW instead returns the picture as captured - OpenCV's native 3 bytes/pixel BGR layout, which is what VideoFormat::RAW itself denotes - with no re-encoding, e.g. to feed a VideoEncoder (H264Encoder) directly. As with any VideoFrameSource, the returned pointer is only valid until the next nextFrame() call.

width/height/fps are read back from the capture device in begin() - set them beforehand via setVideoInfo()/the constructor to request specific values from cv::VideoCapture (best effort; not all backends/devices honor every value, so the actually-applied values are what ends up in videoInfo() afterwards).

Author
Phil Schatzmann

Constructor & Destructor Documentation

◆ CameraFrameSourceOpenCV() [1/2]

◆ CameraFrameSourceOpenCV() [2/2]

CameraFrameSourceOpenCV ( int  device)
inline
Parameters
devicecv::VideoCapture index (e.g. 0 for /dev/video0)

Member Function Documentation

◆ begin()

bool begin ( )
inline

Opens the capture device (cv::VideoCapture::open()), applies any width/height/fps requested via setVideoInfo(), then reads back the values actually in effect.

◆ end()

void end ( )
inline

Releases the capture device.

◆ nextFrame()

const uint8_t * nextFrame ( size_t &  len)
inlineoverridevirtual

Captures a new picture and, unless videoInfo().format is VideoFormat::RAW, JPEG-encodes it - nullptr (len 0) if the capture failed.

Implements VideoFrameSource.

◆ setApiPreference()

void setApiPreference ( int  api)
inline

Selects the capture backend (e.g. cv::CAP_V4L2) - default cv::CAP_ANY lets OpenCV pick. Call before begin().

◆ setDevice()

void setDevice ( const char *  device)
inline

Use a device path/URL/GStreamer pipeline (cv::VideoCapture(const String&)) instead of a numeric index - call before begin().

◆ setJpegQuality()

void setJpegQuality ( int  quality)
inline

JPEG quality (0-100) used to encode frames when videoInfo().format is VideoFormat::MJPEG (the default) - see cv::IMWRITE_JPEG_QUALITY.

◆ setVideoInfo()

void setVideoInfo ( VideoInfo  info)
inline

Requests width/height/fps/format from the capture device - all optional (0/UNKNOWN = leave the device's own default); actually applied on a best-effort basis in begin() via cv::VideoCapture::set(). format only selects nextFrame()'s own output (see class comment), it is not passed to OpenCV. Call before begin().

◆ videoInfo()

VideoInfo videoInfo ( )
inlineoverridevirtual

Provides the width/height/fps/format determined in begin() (or requested explicitly beforehand via setVideoInfo() - see there).

Implements VideoFrameSource.

◆ writeTo()

bool writeTo ( Print out)
inlineinherited

Write the frame to the indicated destination.

Member Data Documentation

◆ api_preference

int api_preference = cv::CAP_ANY
protected

◆ cap

cv::VideoCapture cap
protected

◆ device_index

int device_index = 0
protected

◆ device_string

const char* device_string = nullptr
protected

◆ frame

cv::Mat frame
protected

◆ info

VideoInfo info
protected

◆ jpeg_buffer

std::vector<uint8_t> jpeg_buffer
protected

◆ jpeg_quality

int jpeg_quality = 90
protected

The documentation for this class was generated from the following file: