-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMultiSourceVideo.cpp
executable file
·285 lines (258 loc) · 7.78 KB
/
MultiSourceVideo.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* @file MultiSourceVideo.cpp
* @ingroup Go-CamRecorder
* @author Dominique Vaufreydaz, personnal project
* @copyright All right reserved.
*/
#include "MultiSourceVideo.h"
#ifdef GO_CAM_KINECT_VERSION
using namespace MobileRGBD;
/**
* @brief Overload callback to get Color (RGBA) frame from Kinect
* @param Buffer [in] Raw data buffer
* @param BufferSize [in] Size of Buffer
* @param Width [in] Width of the image
* @param Height [in] Height of the image
* @param ImgType [in] Type of data (here RGBA)
* @param NumFrame [in] Frame number
* @param FrameTimestamp [in] Frame timestamp (received time by this application)
* @param InternalFrameTime [in] Internal frame timestamp from the kinect
*/
/* virtual */ void MultiVideoSource::ProcessColorFrame(void * Buffer, unsigned int BufferSize, int Width, int Height, ImgType FrameType, int NumFrame, const struct timeb& FrameTimestamp, TIMESPAN InternalFrameTime)
{
Omiscid::SmartLocker SD_Lock(*this);
cv::cvtColor( cv::Mat( Height, Width, CV_8UC4, Buffer ), RGBImage, CV_BGRA2BGR );
// fprintf( stderr, "%s\n", type2str(RGBImage.type()).GetStr());
}
/**
* @brief Overload callback to get Depth frame from Kinect
* @param Buffer [in] Raw data buffer
* @param BufferSize [in] Size of Buffer
* @param Width [in] Width of the image
* @param Height [in] Height of the image
* @param ImgType [in] Type of data (here UINT16)
* @param NumFrame [in] Frame number
* @param FrameTimestamp [in] Frame timestamp (received time by this application)
* @param InternalFrameTime [in] Internal frame timestamp from the kinect
*/
/* virtual */ void MultiVideoSource::ProcessDepthFrame(void * Buffer, unsigned int BufferSize, int Width, int Height, ImgType FrameType, int NumFrame, const struct timeb& FrameTimestamp, TIMESPAN InternalFrameTime)
{
Omiscid::SmartLocker SD_Lock(*this);
cv::Mat( Height, Width, CV_16UC1, Buffer ).copyTo(DepthImage);
}
/**
* @brief Function to wait for the first ~synchronous RGB1/Depth images
* @param MaxTries [in] Number of tries
* @param SleepTimeInLoop [in] Sleep time for each time (in ms)
* @return True if frames are available
*/
bool MultiVideoSource::WaitForFrames(int MaxTries, int SleepTimeInLoop)
{
// Multiple tries if we reach this point before received the frames
for( int NbTries = 0; NbTries < MaxTries; NbTries++ )
{
Omiscid::SmartLocker SL_this(*this);
if ( RGBImage.empty() == false && DepthImage.empty() == false )
{
return true;
}
Omiscid::Thread::Sleep(SleepTimeInLoop); // 100 ms
}
return false;
}
#endif
/**
* @brief Constructor
*/
MultiVideoSource::MultiVideoSource()
{
NumberOfFrame = 0;
Mode = Unk_Mode;
IsOpened = false;
LastFrameTimestamp = 0.0;
}
/**
* @brief Virtual destructor
*/
/* virtual */ MultiVideoSource::~MultiVideoSource()
{
#ifdef GO_CAM_KINECT_VERSION
StopThread(5000);
Release();
#endif
};
/**
* @brief Open an input source
* @param [in] Input name for the source. Is if is a number, it will be used as Opencv Number. If it equlas to "kinect1:",
the kinect device will be used (if compiled with kinect mode active). If a file name is provided, it will be open.
* @return true if the device was opened.
*/
bool MultiVideoSource::Open( const char * InputName )
{
// Check if it is a device number
int DeviceNum = -1;
int NumberOfCharRead = 0;
if ( sscanf( InputName, "%d%n", &DeviceNum, &NumberOfCharRead ) == 1 )
{
// ok, a number start
if ( DeviceNum >= 0 && DeviceNum <= MaxDeviceNum && NumberOfCharRead == strlen(InputName) )
{
// Ok, found a probable device number, try to open it
if ( OpenCV_VideoCapture.open(DeviceNum) == true )
{
// Check if we can ask for RGB frames only
// if ( OpenCV_VideoCapture.set(CV_CAP_PROP_CONVERT_RGB, 1.0) == true )
{
fprintf( stderr, "Device %d openned as input source\n", DeviceNum );
IsOpened = true;
Mode = Live_Mode;
TotalTime.Reset();
NumberOfFrame = 0;
return true;
}
fprintf( stderr, "Unable to ask for RGB frames from device %d.\n", DeviceNum );
}
else
{
fprintf( stderr, "Could not open device %d as input source\n", DeviceNum );
}
// Bad case
IsOpened = false;
Mode = Unk_Mode;
NumberOfFrame = 0;
LastFrameTimestamp = 0.0;
return false;
}
}
if ( strcasecmp("kinect1:", InputName) == 0 )
{
#ifndef GO_CAM_KINECT_VERSION
fprintf( stderr, "Kinect1 not supported. Recompile with '-DUSE_KINECT:BOOLEAN=TRUE' cmake option.\n" );
return false;
#else
fprintf( stderr, "Try to open Kinect1 in near mode... " );
SetNearMode(true);
if ( Init( FrameSourceTypes_Color | FrameSourceTypes_Depth ) == false )
{
fprintf( stderr, "Error when Init Kinect1 device.\n" );
// Bad case
IsOpened = false;
Mode = Unk_Mode;
NumberOfFrame = 0;
LastFrameTimestamp = 0.0;
return false;
}
IsOpened = true;
Mode = Kinect1_Mode;
TotalTime.Reset();
NumberOfFrame = 0;
fprintf( stderr, "Device opened. Wait for first frames... " );
if ( WaitForFrames(100, 100) == true )
{
fprintf( stderr, "Device ready.\n" );
return true;
}
fprintf( stderr, "Device not ready, abording.\n" );
return false;
#endif
}
// Try to open the name a a usual file
if ( VideoReader.Open(InputName, "" ) == true ) // if we want to start at 3:55n change to '"-ss 00:03:55" ) == true )'
{
fprintf( stderr, "File '%s' openned as input source\n", InputName );
IsOpened = true;
Mode = File_Mode;
TotalTime.Reset();
NumberOfFrame = 0;
LastFrameTimestamp = 0.0;
return true;
}
fprintf( stderr, "Could not open file '%s' openned as input source\n", InputName );
IsOpened = false;
Mode = Unk_Mode;
NumberOfFrame = 0;
LastFrameTimestamp = 0.0;
return false;
}
/**
* @brief Close source (if opened).
*/
void MultiVideoSource::Close()
{
switch(Mode)
{
case Live_Mode:
OpenCV_VideoCapture.release();
break;
case File_Mode:
VideoReader.Close();
break;
}
IsOpened = false;
Mode = Unk_Mode;
}
/**
* @brief Get Frame per second count. Either file FPS or actual FPS for live source.
*/
double MultiVideoSource::GetFPS()
{
switch(Mode)
{
case Live_Mode:
case Kinect1_Mode:
return (double)NumberOfFrame/TotalTime.GetInSeconds();
case File_Mode:
return VideoReader.Fps;
}
return 0.0;
}
/**
* @brief Read frames. VideoImg is read from RGB source. In case of kinect usage, DepthImg will be filled also (remain untouch when not using kinect).
* @param VideoImg [out] BGR Image for Opencv processing
* @param DepthImg [out] Depth Image for Opencv processing
* @return true if frame(s) has been retrieved
*/
bool MultiVideoSource::ReadFrame( cv::Mat& VideoImg, cv::Mat& DepthImg )
{
switch(Mode)
{
case Live_Mode:
if ( OpenCV_VideoCapture.retrieve( VideoImg ) == true )
{
LastFrameTimestamp = TotalTime.GetInSeconds();
return true;
}
return false;
case File_Mode:
if ( VideoReader.ReadFrame( VideoImg ) == true )
{
LastFrameTimestamp += 1.0/VideoReader.Fps;
return true;
}
return false;
case Kinect1_Mode:
{
#ifdef GO_CAM_KINECT_VERSION
// Multiple tries (max 33 tries of 30 ms) if we reach this point before received the frames
if ( WaitForFrames(33, 30) == true )
{
Omiscid::SmartLocker SL_this(*this);
if ( RGBImage.empty() == false && DepthImage.empty() == false )
{
RGBImage.copyTo(VideoImg);
cv::flip(VideoImg, VideoImg, 1);
DepthImage.copyTo(DepthImg);
cv::flip(DepthImg, DepthImg, 1);
LastFrameTimestamp = TotalTime.GetInSeconds();
return true;
}
}
#endif
return false;
}
default:
throw "No file opened at this time!!";
}
// to make compiler happy
return false;
}