SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSWebCamera Class Reference

#include <SENSWebCamera.h>

Inheritance diagram for SENSWebCamera:
[legend]

Public Member Functions

 SENSWebCamera ()
 
const SENSCameraConfigstart (std::string deviceId, const SENSCameraStreamConfig &streamConfig, bool provideIntrinsics=true) override
 
void stop () override
 Stop a started camera device. More...
 
const SENSCapturePropscaptureProperties () override
 Get SENSCaptureProps which contains necessary information about all available camera devices and their capabilities. More...
 
void grab ()
 
- Public Member Functions inherited from SENSBaseCamera
SENSFrameBasePtr latestFrame () override
 Get the latest captured frame. If no frame was captured the frame will be empty (null). More...
 
const SENSCameraConfigconfig () const override
 defines how the camera was configured during start More...
 
void registerListener (SENSCameraListener *listener) override
 
void unregisterListener (SENSCameraListener *listener) override
 
bool started () const override
 
bool permissionGranted () const override
 
void setPermissionGranted () override
 
- Public Member Functions inherited from SENSCamera
virtual ~SENSCamera ()
 

Private Attributes

cv::VideoCapture _videoCapture
 
std::atomic_bool _stop {false}
 
std::thread _cameraThread
 

Additional Inherited Members

- Protected Member Functions inherited from SENSBaseCamera
void updateFrame (cv::Mat bgrImg, cv::Mat intrinsics, int width, int height, bool intrinsicsChanged)
 
void processStart ()
 call from start function to do startup preprocessing More...
 
- Protected Attributes inherited from SENSBaseCamera
SENSCaptureProps _captureProperties
 
std::atomic< bool > _started {false}
 flags if camera was started More...
 
SENSCameraConfig _config
 indicates what is currently running More...
 
std::atomic< bool > _permissionGranted {false}
 
std::vector< SENSCameraListener * > _listeners
 
std::mutex _listenerMutex
 
SENSFrameBasePtr _frame
 current frame More...
 
std::mutex _frameMutex
 

Detailed Description

Definition at line 11 of file SENSWebCamera.h.

Constructor & Destructor Documentation

◆ SENSWebCamera()

SENSWebCamera::SENSWebCamera ( )
inline

Definition at line 14 of file SENSWebCamera.h.

15  {
16  _permissionGranted = true;
17  }
std::atomic< bool > _permissionGranted
Definition: SENSCamera.h:219

Member Function Documentation

◆ captureProperties()

const SENSCaptureProps & SENSWebCamera::captureProperties ( )
overridevirtual

Get SENSCaptureProps which contains necessary information about all available camera devices and their capabilities.

Implements SENSCamera.

Definition at line 93 of file SENSWebCamera.cpp.

94 {
95  if (!_captureProperties.size())
96  {
97  //definition of standard frame sizes that we want to test for support
98  static std::vector<cv::Size> testSizes = {
99  {640, 480},
100  {640, 360},
101  {960, 540},
102  {1280, 960},
103  {1280, 720},
104  {1920, 1080}};
105 
106  //There is an invisible list of devices populated from your os and your webcams appear there in the order you plugged them in.
107  //If you're e.g on a laptop with a builtin camera, that will be id 0, if you plug in an additional one, that's id 1.
108  for (int i = 0; i < 10; ++i)
109  {
110  _videoCapture.open(i);
111 
112  if (_videoCapture.isOpened())
113  {
114  SENSCameraDeviceProps characteristics(std::to_string(i), SENSCameraFacing::UNKNOWN);
115 
116  //try some standard capture sizes
117  for (auto s : testSizes)
118  {
119  _videoCapture.set(cv::CAP_PROP_FRAME_WIDTH, s.width);
120  _videoCapture.set(cv::CAP_PROP_FRAME_HEIGHT, s.height);
121  cv::Mat frame;
122  _videoCapture >> frame;
123  cv::Size newSize = frame.size();
124  if (!characteristics.contains(newSize) &&
125  newSize != cv::Size(0, 0))
126  {
127  //-1 means unknown focal length
128  characteristics.add(newSize.width, newSize.height, -1.f);
129  }
130  }
131 
132  _captureProperties.push_back(characteristics);
133  _videoCapture.release();
134  }
135  }
136 
137  //if still no caputure properties add a dummy
138  if (_captureProperties.size() == 0)
139  {
141  dummyProps.add(640, 480, -1.f);
142  _captureProperties.push_back(dummyProps);
143  }
144  }
145 
146  return _captureProperties;
147 }
SENSCaptureProps _captureProperties
Definition: SENSCamera.h:216
cv::VideoCapture _videoCapture
Definition: SENSWebCamera.h:30
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47

◆ grab()

void SENSWebCamera::grab ( )

Definition at line 77 of file SENSWebCamera.cpp.

78 {
79  while (!_stop)
80  {
81  cv::Mat bgrImg;
82  if (_videoCapture.read(bgrImg))
83  {
84  if (bgrImg.cols == _config.streamConfig.widthPix &&
85  bgrImg.rows == _config.streamConfig.heightPix)
86  updateFrame(bgrImg, cv::Mat(), false, bgrImg.rows, bgrImg.cols);
87  else
88  LOG_WEBCAM_WARN("video capture delivers wrong resolution!");
89  }
90  }
91 }
#define LOG_WEBCAM_WARN(...)
SENSCameraConfig _config
indicates what is currently running
Definition: SENSCamera.h:218
void updateFrame(cv::Mat bgrImg, cv::Mat intrinsics, int width, int height, bool intrinsicsChanged)
Definition: SENSCamera.cpp:220
std::atomic_bool _stop
Definition: SENSWebCamera.h:32
SENSCameraStreamConfig streamConfig
currently selected stream config index (use it to look up original capture size)
Definition: SENSCamera.h:131

◆ start()

const SENSCameraConfig & SENSWebCamera::start ( std::string  deviceId,
const SENSCameraStreamConfig streamConfig,
bool  provideIntrinsics = true 
)
overridevirtual

Call this function to configure the camera if you exactly know, what device you want to open and with which stream configuration. You can find out these properties by using SENSCaptureProps object (see getCaptureProperties())

Parameters
deviceIdcamera device id to start
streamConfigSENSCameraStreamConfig from camera device. This defines the size of the image. The image is converted to BGR and assigned to SENSFrame::imgBGR.
provideIntrinsicsspecifies if intrinsics estimation should be enabled. The estimated intrinsics are transferred with every SENSFrame as they may be different for every frame (e.g. on iOS). This value has different effects on different architectures.
Returns
the found configuration that is adjusted and used when SENSCamera::start() is called.

Implements SENSCamera.

Definition at line 13 of file SENSWebCamera.cpp.

16 {
17  if (_started)
18  {
19  Utils::warnMsg("SENSWebCamera", "Call to start was ignored. Camera is currently running!", __LINE__, __FILE__);
20  return _config;
21  }
22 
23  //retrieve all camera characteristics
24  if (_captureProperties.size() == 0)
26 
27  if (_captureProperties.size() == 0)
28  throw SENSException(SENSType::CAM, "Could not retrieve camera properties!", __LINE__, __FILE__);
29 
30  if (!_captureProperties.containsDeviceId(deviceId))
31  throw SENSException(SENSType::CAM, "DeviceId does not exist!", __LINE__, __FILE__);
32 
33  int id = std::stoi(deviceId);
34  _videoCapture.open(id);
35 
36  if (!_videoCapture.isOpened())
37  throw SENSException(SENSType::CAM, "Could not open camera with id: " + deviceId, __LINE__, __FILE__);
38 
39  _videoCapture.set(cv::CAP_PROP_FRAME_WIDTH, streamConfig.widthPix);
40  _videoCapture.set(cv::CAP_PROP_FRAME_HEIGHT, streamConfig.heightPix);
41  _started = true;
42  _permissionGranted = true;
43 
44  //init config here
45  _config = SENSCameraConfig(deviceId,
46  streamConfig,
49 
50  processStart();
51 
52  //start thread
53  _cameraThread = std::thread(&SENSWebCamera::grab, this);
54 
55  return _config;
56 }
std::atomic< bool > _started
flags if camera was started
Definition: SENSCamera.h:217
void processStart()
call from start function to do startup preprocessing
Definition: SENSCamera.cpp:294
bool containsDeviceId(const std::string &deviceId) const
Definition: SENSCamera.cpp:61
const SENSCaptureProps & captureProperties() override
Get SENSCaptureProps which contains necessary information about all available camera devices and thei...
std::thread _cameraThread
Definition: SENSWebCamera.h:33
void warnMsg(const char *tag, const char *msg, const int line, const char *file)
Platform independent warn message output.
Definition: Utils.cpp:1142

◆ stop()

void SENSWebCamera::stop ( )
overridevirtual

Stop a started camera device.

Implements SENSCamera.

Definition at line 58 of file SENSWebCamera.cpp.

59 {
60  _stop = true;
61  if (_cameraThread.joinable())
62  _cameraThread.join();
63  _stop = false;
64 
65  if (_videoCapture.isOpened())
66  {
67  _videoCapture.release();
68 
69  _started = false;
70  }
71  else
72  {
73  LOG_WEBCAM_INFO("stop: ignored because camera is not open!");
74  }
75 }
#define LOG_WEBCAM_INFO

Member Data Documentation

◆ _cameraThread

std::thread SENSWebCamera::_cameraThread
private

Definition at line 33 of file SENSWebCamera.h.

◆ _stop

std::atomic_bool SENSWebCamera::_stop {false}
private

Definition at line 32 of file SENSWebCamera.h.

◆ _videoCapture

cv::VideoCapture SENSWebCamera::_videoCapture
private

Definition at line 30 of file SENSWebCamera.h.


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