JeVois
1.22
JeVois Smart Embedded Machine Vision Toolkit
|
|
#include <jevois/Core/Camera.H>
JeVois camera driver class - grabs frames from a Video4Linux camera sensor.
On the platform hardware, the Camera class provides access to the camera sensor on the parallel camera bus (CSI). On other hardware, it can provide access to any Video4Linux2 camera (e.g., USB webcam), although with some limitations, such as:
The camera runs a thread that handles capturing frames automatically. Users can access the next grabbed frame using get(), which may block until the frame is fully captured. Once they are finished with a frame obtained through get(), users should hand it back to the Camera by calling done(). When using the JeVois Engine and a Module, get() and done() are automatically called in an exception-safe manner through the use of the InputFrame class. Indeed, video grabbing uses a fixed set of image buffers that are allocated by the Linux kernel when a video format is configured, and the camera hardware then cycles through the buffers. On the platform, the camera image buffers are allocated in the ION carveout memory area, which is a reserved memory zone configured in the Linux kernel, and the USB output buffers are also allocated in that zone. To avoid reserving too much memory that will not be used, the ION carveout reservation is quite small by default, but is sufficient for 3 1280x1024 YUYV buffers.
Camera implements a zero-copy, zero-wait access to input video frames, that is:
Camera will drop frames if they are not processed by user code fast enough. That is, if all buffers have been grabbed already and not yet handed over through get() and then returned by done(), then the next camera sensor frame will be dropped.
Most programmers will never use Camera directly, instead using Engine and InputFrame.
Public Member Functions | |
Camera (std::string const &devname, jevois::CameraSensor s=jevois::CameraSensor::any, unsigned int const nbufs=0) | |
Construct and open the device. | |
~Camera () | |
Close the device and free all resources. | |
void | streamOn () override |
Start streaming. | |
void | abortStream () override |
Abort streaming. | |
void | streamOff () override |
Stop streaming. | |
void | get (RawImage &img) override |
Get the next captured buffer. | |
bool | hasScaledImage () const override |
Check whether a second input image scaled by the JeVoisPro Platform ISP is available. | |
void | get2 (RawImage &img) override |
Get the next captured buffer, for second ISP-scaled image. | |
void | done (RawImage &img) override |
Indicate that user processing is done with an image previously obtained via get() | |
void | done2 (RawImage &img) override |
Indicate that user processing is done with an image previously obtained via get2() | |
void | queryControl (struct v4l2_queryctrl &qc) const override |
Get information about a control, throw if unsupported by hardware. | |
void | queryMenu (struct v4l2_querymenu &qm) const override |
Get the available menu entry names for a menu-type control, throw if unsupported by hardware. | |
void | getControl (struct v4l2_control &ctrl) const |
Get a control's current value, throw if unsupported by hardware. | |
void | setControl (struct v4l2_control const &ctrl) override |
Set a control, throw if control not supported or the hardware rejects the value. | |
void | setFormat (VideoMapping const &m) override |
Set the video format and frame rate. | |
void | writeRegister (unsigned short reg, unsigned short val) |
Write a value to one of the camera's registers. | |
unsigned short | readRegister (unsigned short reg) |
Read a value from one of the camera's registers. | |
int | lock () |
Lock the camera and return its file descriptor. | |
void | unlock () |
Unlock the camera that was previously locked by lock() | |
Public Member Functions inherited from jevois::VideoInput | |
VideoInput (std::string const &devname, unsigned int const nbufs=0) | |
Constructor. | |
virtual | ~VideoInput () |
Virtual destructor for save inheritance. | |
Protected Types | |
enum | Flags { JEVOIS_SENSOR_COLOR = 0x00 , JEVOIS_SENSOR_MONO = 0x01 , JEVOIS_SENSOR_ICM20948 = 0x02 } |
Sensor flags. More... | |
Protected Member Functions | |
Flags | readFlags () |
Get the sensor flags. | |
Additional Inherited Members | |
Protected Attributes inherited from jevois::VideoInput | |
std::string const | itsDevName |
Our device or movie file name. | |
unsigned int const | itsNbufs |
Our number of buffers. | |
|
protected |
jevois::Camera::Camera | ( | std::string const & | devname, |
jevois::CameraSensor | s = jevois::CameraSensor::any , |
||
unsigned int const | nbufs = 0 |
||
) |
Construct and open the device.
devname | device name, e.g., /dev/video0 |
s | currently installed camera sensor |
nbufs | number of video grab buffers, or 0 for automatic. |
Definition at line 34 of file Camera.C.
References JEVOIS_SENSOR_ICM20948, JEVOIS_SENSOR_MONO, JEVOIS_TRACE, LDEBUG, and readFlags().
jevois::Camera::~Camera | ( | ) |
Close the device and free all resources.
Definition at line 151 of file Camera.C.
References JEVOIS_TRACE, and jevois::warnAndIgnoreException().
|
overridevirtual |
Abort streaming.
This only cancels future get() and done() calls, one should still call streamOff() to turn off streaming.
Implements jevois::VideoInput.
Definition at line 172 of file Camera.C.
References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and jevois::warnAndIgnoreException().
|
overridevirtual |
Indicate that user processing is done with an image previously obtained via get()
You should call this as soon after get() as possible, once you are finished with the RawImage data so that it can be recycled.
Calling done() on a RawImage invalidates the image and in particular its pixel buffer. Users should make sure that no attempt to use the image or the pixel buffer will be made after done() is called.
Implements jevois::VideoInput.
Definition at line 215 of file Camera.C.
References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and LFATAL.
|
overridevirtual |
Indicate that user processing is done with an image previously obtained via get2()
You should call this as soon after get2() as possible, once you are finished with the RawImage data so that it can be recycled.
Calling done2() on a RawImage invalidates the image and in particular its pixel buffer. Users should make sure that no attempt to use the image or the pixel buffer will be made after done2() is called.
Reimplemented from jevois::VideoInput.
Definition at line 224 of file Camera.C.
References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and LFATAL.
|
overridevirtual |
Get the next captured buffer.
Throws if we are not streaming or blocks until an image is available (has been captured). img should have been created by the caller (with no pixel buffer allocated) and will be filled in by what we receive from the device here.
Implements jevois::VideoInput.
Definition at line 190 of file Camera.C.
References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and LFATAL.
|
overridevirtual |
Get the next captured buffer, for second ISP-scaled image.
On JeVois-Pro Platform only, the camera ISP can output 2 frames: 1) raw from sensor, 2) scaled by ISP. This function is to access the ISP scaled frame. Throws if not JeVois-Pro Platform or the camera stream type is not jevois::StreamType::RawAndScaled. Throws if we are not streaming or blocks until an image is available (has been captured). img should have been created by the caller (with no pixel buffer allocated) and will be filled in by what we receive from the device here.
Reimplemented from jevois::VideoInput.
Definition at line 205 of file Camera.C.
References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and LFATAL.
|
virtual |
Get a control's current value, throw if unsupported by hardware.
This is just a pass-through to VIDIOC_G_CTRL, users should zero-out ctrl and then set in ctrl.id the desired control ID.
Implements jevois::VideoInput.
Definition at line 249 of file Camera.C.
References JEVOIS_TIMED_LOCK, LFATAL, and XIOCTL_QUIET.
|
overridevirtual |
Check whether a second input image scaled by the JeVoisPro Platform ISP is available.
Returns false unless we are on JeVois-Pro Platform and the camera format modifier jevois::CropType::CropScale is currently in use.
Reimplemented from jevois::VideoInput.
int jevois::Camera::lock | ( | ) |
Lock the camera and return its file descriptor.
Used by IMUi2c to access the IMU registers over the I2C bus shared with the camera sensor. Use with caution and make sure you catch exceptions so you can guarantee that you will call unlock(), otherwise your camera will be stuck and enable to stream.
Definition at line 310 of file Camera.C.
References LFATAL.
|
overridevirtual |
Get information about a control, throw if unsupported by hardware.
Caller should zero-out qc and then set the id field to the desired control id. See VIDIOC_QUERYCTRL for more information.
Implements jevois::VideoInput.
Definition at line 233 of file Camera.C.
References JEVOIS_TIMED_LOCK, LFATAL, and XIOCTL_QUIET.
|
overridevirtual |
Get the available menu entry names for a menu-type control, throw if unsupported by hardware.
Caller should zero-out qm and then set the id and index fields to the desired control id and menu item index. See VIDIOC_QUERYMENU for more information.
Implements jevois::VideoInput.
Definition at line 241 of file Camera.C.
References JEVOIS_TIMED_LOCK, LFATAL, and XIOCTL_QUIET.
|
protected |
unsigned short jevois::Camera::readRegister | ( | unsigned short | reg | ) |
Read a value from one of the camera's registers.
This very low-level access is for development of optimal camera settings only and should not be used in normal operation, it can crash your system.
Definition at line 289 of file Camera.C.
References JEVOIS_TIMED_LOCK, LDEBUG, LFATAL, jevois::sformat(), and XIOCTL.
|
overridevirtual |
Set a control, throw if control not supported or the hardware rejects the value.
This is just a pass-through to VIDIOC_S_CTRL
Implements jevois::VideoInput.
Definition at line 261 of file Camera.C.
References JEVOIS_TIMED_LOCK, LFATAL, and XIOCTL_QUIET.
|
overridevirtual |
Set the video format and frame rate.
Implements jevois::VideoInput.
Definition at line 49 of file Camera.C.
References jevois::VideoMapping::c2fmt, jevois::VideoMapping::c2h, jevois::VideoMapping::c2w, jevois::VideoMapping::cfmt, jevois::VideoMapping::cfps, jevois::VideoMapping::ch, jevois::VideoMapping::crop, jevois::VideoMapping::cw, ISP_META_HEIGHT, ISP_META_WIDTH, ISP_V4L2_PIX_FMT_META, JEVOIS_TIMED_LOCK, JEVOIS_TRACE, LFATAL, LINFO, and jevois::sensorPrepareSetFormat().
|
overridevirtual |
Stop streaming.
Implements jevois::VideoInput.
Definition at line 181 of file Camera.C.
References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and jevois::warnAndIgnoreException().
|
overridevirtual |
Start streaming.
Implements jevois::VideoInput.
Definition at line 163 of file Camera.C.
References JEVOIS_TIMED_LOCK, JEVOIS_TRACE, and jevois::warnAndIgnoreException().
void jevois::Camera::unlock | ( | ) |
void jevois::Camera::writeRegister | ( | unsigned short | reg, |
unsigned short | val | ||
) |
Write a value to one of the camera's registers.
This very low-level access is for development of optimal camera settings only and should not be used in normal operation, it can crash your system.
Definition at line 273 of file Camera.C.
References JEVOIS_TIMED_LOCK, LDEBUG, LFATAL, jevois::sformat(), and XIOCTL.