-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFrameStreamer.hpp
106 lines (75 loc) · 2.44 KB
/
FrameStreamer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once
#include "DrawUtils.hpp"
#include "PoseEstimator.hpp"
#include <functional>
#include <string>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
// ##############################
class FrameStreamer;
class ImageStreamer;
class VideoStreamer;
template <typename T, typename... Ts>
concept IsAnyOf = ( std::same_as<T, Ts> || ... );
template <typename T>
concept Streamer = IsAnyOf<T, ImageStreamer, VideoStreamer>;
template <Streamer T>
std::unique_ptr<FrameStreamer> CreateFrameStreamer( const std::string fileName )
{
auto streamer = std::make_unique<T>( fileName );
if ( streamer->Initialize( ) )
return streamer;
else
return nullptr;
}
// ##################################
class FrameStreamer {
public:
FrameStreamer( ) : mFps( 0. ), mNumberOfFrames( 0 ) { }
virtual ~FrameStreamer( ) = default;
virtual bool Initialize( ) = 0;
// TODO: Make the result type more generic and not pose estimation dependant
struct Result {
std::vector<PoseEstimator::Detection> modelOutput;
DrawUtils::ScaleFactor scaleFactor;
};
using FrameProcessFunction = std::function<Result( const cv::Mat& inputFrame )>;
void Run( FrameProcessFunction f = nullptr );
protected:
float mFps;
int mNumberOfFrames;
private:
virtual bool AcquireNextFrame( cv::Mat& frame ) = 0;
virtual bool AcquirePreviousFrame( cv::Mat& frame ) = 0;
static constexpr std::string mWindowName = "Stream";
};
// ##################################
class ImageStreamer final : public FrameStreamer {
public:
ImageStreamer( const std::string& imageFilePath ) : mIsInitialized( false ), mImageFilePath( imageFilePath ) { }
bool Initialize( ) override;
bool AcquireNextFrame( cv::Mat& frame ) override;
bool AcquirePreviousFrame( cv::Mat& frame ) override;
private:
bool mIsInitialized;
const std::string mImageFilePath;
cv::Mat mImage;
};
// ##################################
class VideoStreamer final : public FrameStreamer {
public:
VideoStreamer( const std::string& videoFilePath ) :
mIsInitialized( false ),
mLoopVideo( true ),
mVideoFilePath( videoFilePath )
{
}
bool Initialize( ) override;
bool AcquireNextFrame( cv::Mat& frame ) override;
bool AcquirePreviousFrame( cv::Mat& frame ) override;
private:
bool mIsInitialized;
const std::string mVideoFilePath;
const bool mLoopVideo;
cv::VideoCapture mCap;
};