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

#include <SENSVideoStream.h>

Public Member Functions

 SENSVideoStream (const std::string &videoFileName, bool videoLoops, bool mirrorH, bool mirrorV, float targetFps=0)
 
SENSFramePtr grabNextFrame ()
 
SENSFramePtr grabNextResampledFrame ()
 
SENSFramePtr grabPreviousResampledFrame ()
 
SENSFramePtr grabPreviousFrame ()
 
cv::Size getFrameSize () const
 
const std::string & videoFilename () const
 
int nextFrameIndex () const
 
int frameCount () const
 
float fps () const
 
bool isOpened () const
 
const SENSCalibration *const calibration () const
 
void setCalibration (SENSCalibration calibration, bool buildUndistortionMaps)
 
void guessAndSetCalibration (float fovGuess)
 

Private Member Functions

void moveCapturePosition (int n)
 

Private Attributes

cv::VideoCapture _cap
 
cv::Size2i _videoFrameSize
 
int _frameCount = 0
 
std::string _videoFileName
 
bool _videoLoops = false
 
float _fps = 0.f
 
float _targetFps
 
bool _mirrorH = false
 
bool _mirrorV = false
 
std::unique_ptr< SENSCalibration_calibration
 

Detailed Description

Definition at line 8 of file SENSVideoStream.h.

Constructor & Destructor Documentation

◆ SENSVideoStream()

SENSVideoStream::SENSVideoStream ( const std::string &  videoFileName,
bool  videoLoops,
bool  mirrorH,
bool  mirrorV,
float  targetFps = 0 
)

Definition at line 8 of file SENSVideoStream.cpp.

13  : _videoLoops(videoLoops),
14  _mirrorH(mirrorH),
15  _mirrorV(mirrorV)
16 {
17  if (!Utils::fileExists(videoFileName))
18  {
19  throw SENSException(SENSType::VIDEO, "Video file does not exist: " + videoFileName, __LINE__, __FILE__);
20  }
21 
22  _cap.open(videoFileName);
23  if (!_cap.isOpened())
24  throw SENSException(SENSType::VIDEO, "Could not open video file stream: " + videoFileName, __LINE__, __FILE__);
25 
26  _videoFrameSize = {(int)_cap.get(cv::CAP_PROP_FRAME_WIDTH), (int)_cap.get(cv::CAP_PROP_FRAME_HEIGHT)};
27  _frameCount = (int)_cap.get(cv::CAP_PROP_FRAME_COUNT);
28  _fps = (float)_cap.get(cv::CAP_PROP_FPS);
29 
30  if (targetFps == 0)
31  _targetFps = _fps;
32  else
33  _targetFps = targetFps;
34 
35  _videoFileName = videoFileName;
36 }
cv::Size2i _videoFrameSize
cv::VideoCapture _cap
std::string _videoFileName
bool fileExists(const string &pathfilename)
Returns true if a file exists.
Definition: Utils.cpp:894

Member Function Documentation

◆ calibration()

const SENSCalibration* const SENSVideoStream::calibration ( ) const
inline

Definition at line 26 of file SENSVideoStream.h.

27  {
28  return _calibration.get();
29  }
std::unique_ptr< SENSCalibration > _calibration

◆ fps()

float SENSVideoStream::fps ( ) const
inline

Definition at line 22 of file SENSVideoStream.h.

22 { return _fps; }

◆ frameCount()

int SENSVideoStream::frameCount ( ) const
inline

Definition at line 21 of file SENSVideoStream.h.

21 { return _frameCount; }

◆ getFrameSize()

cv::Size SENSVideoStream::getFrameSize ( ) const
inline

Definition at line 17 of file SENSVideoStream.h.

17 { return _videoFrameSize; }

◆ grabNextFrame()

SENSFramePtr SENSVideoStream::grabNextFrame ( )

Definition at line 38 of file SENSVideoStream.cpp.

39 {
40  if (!_cap.isOpened())
41  throw SENSException(SENSType::VIDEO, "Video file stream is not open!", __LINE__, __FILE__);
42 
43  if (_videoLoops)
44  {
45  if (_cap.get(cv::CAP_PROP_POS_FRAMES) == _frameCount)
46  _cap.set(cv::CAP_PROP_POS_FRAMES, 0);
47  }
48 
49  SENSFramePtr sensFrame;
50  cv::Mat bgrImg;
51  cv::Mat grayImg;
52 
53  if (_cap.read(bgrImg))
54  {
56  cv::cvtColor(bgrImg, grayImg, cv::COLOR_BGR2GRAY);
57 
58  sensFrame = std::make_unique<SENSFrame>(
59  SENSClock::now(),
60  bgrImg,
61  grayImg,
62  _mirrorH,
63  _mirrorV,
64  1.0f,
65  cv::Mat(),
66  bgrImg.cols,
67  bgrImg.rows);
68  }
69 
70  return sensFrame;
71 }
std::shared_ptr< SENSFrame > SENSFramePtr
Definition: SENSFrame.h:73
void mirrorImage(cv::Mat &img, bool mirrorH, bool mirrorV)
Definition: SENSUtils.cpp:170

◆ grabNextResampledFrame()

SENSFramePtr SENSVideoStream::grabNextResampledFrame ( )

Definition at line 73 of file SENSVideoStream.cpp.

74 {
75  if (!_cap.isOpened())
76  throw SENSException(SENSType::VIDEO, "Video file stream is not open!", __LINE__, __FILE__);
77 
78  if (_videoLoops)
79  {
80  if (_cap.get(cv::CAP_PROP_POS_FRAMES) == _frameCount)
81  _cap.set(cv::CAP_PROP_POS_FRAMES, 0);
82  }
83 
84  SENSFramePtr sensFrame;
85  cv::Mat bgrImg;
86  cv::Mat grayImg;
87 
88  float frameDuration = 1.0f / _targetFps;
89  int frameIndex = (int)_cap.get(cv::CAP_PROP_POS_FRAMES);
90  int skippedFrame = 0;
91  float frameTime;
92  float lastFrameTime = frameIndex / _fps;
93  do
94  {
95  skippedFrame++;
96  frameTime = (float)(frameIndex + skippedFrame) / _fps;
97  } while ((frameTime - lastFrameTime) < frameDuration);
98 
99  float frameError = (frameTime - lastFrameTime - frameDuration); //Compute overflow
100  int nbFrame = (int)(frameIndex * _targetFps / _fps); //Expected number of frames
101  float totalError = (nbFrame * frameError); //Total cumulated error
102  if ((fmod(totalError, frameDuration) + frameError) > frameDuration && skippedFrame > 1)
103  {
104  skippedFrame--;
105  }
106 
107  moveCapturePosition(skippedFrame);
108 
109  if (_cap.read(bgrImg))
110  {
112  cv::cvtColor(bgrImg, grayImg, cv::COLOR_BGR2GRAY);
113 
114  sensFrame = std::make_unique<SENSFrame>(
115  SENSClock::now(),
116  bgrImg,
117  grayImg,
118  _mirrorH,
119  _mirrorV,
120  1.0,
121  cv::Mat(),
122  bgrImg.cols,
123  bgrImg.rows);
124  }
125 
126  return sensFrame;
127 }
void moveCapturePosition(int n)

◆ grabPreviousFrame()

SENSFramePtr SENSVideoStream::grabPreviousFrame ( )

Definition at line 146 of file SENSVideoStream.cpp.

147 {
149  return grabNextFrame();
150 }
SENSFramePtr grabNextFrame()

◆ grabPreviousResampledFrame()

SENSFramePtr SENSVideoStream::grabPreviousResampledFrame ( )

Definition at line 129 of file SENSVideoStream.cpp.

130 {
131  int frameIndex = (int)_cap.get(cv::CAP_PROP_POS_FRAMES);
132  int skippedFrame = 0;
133  float frameTime;
134  float currentFrameTime = frameIndex / _fps;
135 
136  do
137  {
138  frameTime = frameIndex - skippedFrame / _fps;
139  skippedFrame++;
140  } while ((currentFrameTime - frameTime) > 1.0 / _targetFps);
141 
142  moveCapturePosition(-skippedFrame + 1);
143  return grabNextFrame();
144 }

◆ guessAndSetCalibration()

void SENSVideoStream::guessAndSetCalibration ( float  fovGuess)

Definition at line 176 of file SENSVideoStream.cpp.

177 {
178  _calibration = std::make_unique<SENSCalibration>(_videoFrameSize,
179  fovGuess,
180  false,
181  false,
184 }
Class for holding computer information.
Definition: Utils.h:288
std::vector< char > & get(std::string path)
Definition: SLIOMemory.cpp:24

◆ isOpened()

bool SENSVideoStream::isOpened ( ) const
inline

Definition at line 24 of file SENSVideoStream.h.

24 { return _cap.isOpened(); }

◆ moveCapturePosition()

void SENSVideoStream::moveCapturePosition ( int  n)
private

Definition at line 152 of file SENSVideoStream.cpp.

153 {
154  int frameIndex = (int)_cap.get(cv::CAP_PROP_POS_FRAMES);
155  frameIndex += n;
156 
157  if (frameIndex < 0)
158  frameIndex = 0;
159  else if (frameIndex > _frameCount)
160  frameIndex = _frameCount;
161 
162  _cap.set(cv::CAP_PROP_POS_FRAMES, frameIndex);
163 }

◆ nextFrameIndex()

int SENSVideoStream::nextFrameIndex ( ) const
inline

Definition at line 20 of file SENSVideoStream.h.

20 { return (int)_cap.get(cv::CAP_PROP_POS_FRAMES); }

◆ setCalibration()

void SENSVideoStream::setCalibration ( SENSCalibration  calibration,
bool  buildUndistortionMaps 
)

Definition at line 165 of file SENSVideoStream.cpp.

166 {
167  if (!_cap.isOpened())
168  throw SENSException(SENSType::CAM, "setCalibration not possible if video stream is not started!", __LINE__, __FILE__);
169 
170  _calibration = std::make_unique<SENSCalibration>(calibration);
171  //now we adapt the calibration to the target size
172  if (_videoFrameSize.width != calibration.imageSize().width || _videoFrameSize.height != calibration.imageSize().height)
173  _calibration->adaptForNewResolution({_videoFrameSize.width, _videoFrameSize.height}, buildUndistortionMaps);
174 }
cv::Size imageSize() const
const SENSCalibration *const calibration() const

◆ videoFilename()

const std::string& SENSVideoStream::videoFilename ( ) const
inline

Definition at line 19 of file SENSVideoStream.h.

19 { return _videoFileName; }

Member Data Documentation

◆ _calibration

std::unique_ptr<SENSCalibration> SENSVideoStream::_calibration
private

Definition at line 49 of file SENSVideoStream.h.

◆ _cap

cv::VideoCapture SENSVideoStream::_cap
private

Definition at line 37 of file SENSVideoStream.h.

◆ _fps

float SENSVideoStream::_fps = 0.f
private

Definition at line 43 of file SENSVideoStream.h.

◆ _frameCount

int SENSVideoStream::_frameCount = 0
private

Definition at line 39 of file SENSVideoStream.h.

◆ _mirrorH

bool SENSVideoStream::_mirrorH = false
private

Definition at line 46 of file SENSVideoStream.h.

◆ _mirrorV

bool SENSVideoStream::_mirrorV = false
private

Definition at line 47 of file SENSVideoStream.h.

◆ _targetFps

float SENSVideoStream::_targetFps
private

Definition at line 44 of file SENSVideoStream.h.

◆ _videoFileName

std::string SENSVideoStream::_videoFileName
private

Definition at line 40 of file SENSVideoStream.h.

◆ _videoFrameSize

cv::Size2i SENSVideoStream::_videoFrameSize
private

Definition at line 38 of file SENSVideoStream.h.

◆ _videoLoops

bool SENSVideoStream::_videoLoops = false
private

Definition at line 42 of file SENSVideoStream.h.


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