-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaveVideoClass.cpp
301 lines (238 loc) · 7.81 KB
/
SaveVideoClass.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "SaveVideoClass.h"
#define sleep(x) std::this_thread::sleep_for(std::chrono::milliseconds(x))
#define M_TIMER_ELAPSED ((double)(( std::clock() - m_timer ) / (double) CLOCKS_PER_SEC))
VideoSaverFlyCapture::VideoSaverFlyCapture()
{ // base class constructor are automatically called
m_isFlycapture = false;
};
/****************************************************************************************/
VideoSaverFlyCapture::~VideoSaverFlyCapture()
{
close();
}
int VideoSaverFlyCapture::close()
{
VideoSaver::close();
if (m_Camera.IsConnected()) {
FlyCapture2::Error error = m_Camera.Disconnect();
if ( error != FlyCapture2::PGRERROR_OK ) {
error.PrintErrorTrace();
return -1;
}
sleep(500);
}
return 0;
}
/****************************************************************************************/
int VideoSaverFlyCapture::init(int camIdx){
m_isFlycapture = false;
return VideoSaver::init(camIdx);
}
/****************************************************************************************/
int VideoSaverFlyCapture::init(FlyCapture2::PGRGuid camIdx)
{
FlyCapture2::Error error;
FlyCapture2::CameraInfo camInfo;
// Connect the camera
error = m_Camera.Connect( &camIdx );
if ( error != FlyCapture2::PGRERROR_OK )
{
std::cout << "Failed to connect to camera" << std::endl;
return -1;
}
// Get the camera info and print it out
error = m_Camera.GetCameraInfo( &camInfo );
if ( error != FlyCapture2::PGRERROR_OK )
{
std::cout << "Failed to get camera info from camera" << std::endl;
return -1;
}
std::cout << camInfo.vendorName << " "
<< camInfo.modelName << " "
<< camInfo.serialNumber << std::endl;
//-----------------
// get frame rate
// Check if the camera supports the FRAME_RATE property
FlyCapture2::PropertyInfo propInfo;
propInfo.type = FlyCapture2::FRAME_RATE;
error = m_Camera.GetPropertyInfo( &propInfo );
if (error != FlyCapture2::PGRERROR_OK)
{
error.PrintErrorTrace();
return -1;
}
m_FrameRateToUse = 15.0f;
if ( propInfo.present == true )
{
// Get the frame rate
FlyCapture2::Property prop;
prop.type = FlyCapture2::FRAME_RATE;
error = m_Camera.GetProperty( &prop );
if (error != FlyCapture2::PGRERROR_OK)
{
error.PrintErrorTrace();
return -1;
}
else
{
// Set the frame rate.
// Note that the actual recording frame rate may be slower,
// depending on the bus speed and disk writing speed.
m_FrameRateToUse = prop.absValue;
}
}
printf("Using frame rate of %3.1f\n", m_FrameRateToUse);
//get the width and height
FlyCapture2::Format7ImageSettings settings;
unsigned int packetSize;
float percentage;
error = m_Camera.GetFormat7Configuration( &settings,&packetSize,&percentage );
if ( error != FlyCapture2::PGRERROR_OK ) {
error.PrintErrorTrace();
return -1;
}
m_FrameSize = cv::Size(settings.width,settings.height);
settings.pixelFormat = FlyCapture2::PIXEL_FORMAT_RAW8;
bool valid;
FlyCapture2::Format7PacketInfo pinfo;
error = m_Camera.ValidateFormat7Settings( &settings,&valid,&pinfo);
if ( error != FlyCapture2::PGRERROR_OK ) {
error.PrintErrorTrace();
return -1;
}
if (!valid) {
std::cout << "Could not validate Format 7." << std::endl;
return -1;
}
error = m_Camera.SetFormat7Configuration( &settings,pinfo.recommendedBytesPerPacket);
if ( error != FlyCapture2::PGRERROR_OK ) {
error.PrintErrorTrace();
return -1;
}
// set time stamping on
FlyCapture2::EmbeddedImageInfo info;
// Get configuration
error = m_Camera.GetEmbeddedImageInfo( &info );
if ( error != FlyCapture2::PGRERROR_OK )
{
error.PrintErrorTrace();
return -1;
}
info.timestamp.onOff = true;
// Set configuration
error = m_Camera.SetEmbeddedImageInfo( &info );
if ( error != FlyCapture2::PGRERROR_OK )
{
error.PrintErrorTrace();
return -1;
}
m_isFlycapture = true;
m_isBGR = true;
return 0;
}
/****************************************************************************************/
int VideoSaverFlyCapture::startCapture() {
if (!isFlycapture()) {
return VideoSaver::startCapture();
} else {
if (isFinished() && (isInit())) {
// start thread to begin capture and populate Mat frame
FlyCapture2::Error error = m_Camera.StartCapture();
if ( error == FlyCapture2::PGRERROR_ISOCH_BANDWIDTH_EXCEEDED )
{
std::cout << "Bandwidth exceeded" << std::endl;
return -1;
}
else if ( error != FlyCapture2::PGRERROR_OK )
{
std::cout << "Failed to start image capture" << std::endl;
return -1;
}
//start the grabbing thread
m_KeepWritingAlive = false; // not to be started
m_WritingFinished = true;
m_newFrameAvailable = false;
std::cout << "Start video grabbing .." << std::endl;
m_captureThread = new std::thread(&VideoSaverFlyCapture::captureThread,this);
m_capturing = true;
sleep(500);
waitForNewFrame();
return 0;
} else {
if (isInit()) {
std::cout << "Warning: capture not yet finished !" << std::endl;
} else {
std::cout << "Warning: camera not available!" << std::endl;
}
return -1;
}
}
}
/****************************************************************************************/
bool VideoSaverFlyCapture::isInit()
{
if (isFlycapture())
return (m_Camera.IsConnected() && (!m_capturing));
else
return VideoSaver::isInit();
}
/****************************************************************************************/
void VideoSaverFlyCapture::captureThread()
{
m_GrabbingFinished = false;
m_KeepThreadAlive = true;
m_frameNumber = 0;
m_newFrameAvailable = false;
FlyCapture2::Image rgbImage;
FlyCapture2::Image rawImage;
FlyCapture2::Image rawImage2;
m_timer= std::clock();
while (m_KeepThreadAlive)
{
FlyCapture2::Error error = m_Camera.RetrieveBuffer( &rawImage );
if ( error != FlyCapture2::PGRERROR_OK )
{
error.PrintErrorTrace();
}
//get the time stamp
const double localtimestamp = M_TIMER_ELAPSED;
// convert to bgr
rawImage2.DeepCopy(&rawImage); // not sure if really needed since we convert below...
rawImage2.Convert(FlyCapture2::PIXEL_FORMAT_RGB, &rgbImage );
// convert to Mat
unsigned int rowBytes = (double) rgbImage.GetReceivedDataSize()/(double)rgbImage.GetRows();
// copy to frame variable and update times
{ std::unique_lock<std::mutex> lock(m_FrameMutex);
m_Frame.release();
m_Frame = cv::Mat(rgbImage.GetRows(), rgbImage.GetCols(), CV_8UC3, rgbImage.GetData(),rowBytes);
// could this happen ?
if (m_Frame.size().width==0)
m_Frame = cv::Mat::zeros(m_FrameSize,CV_8UC3);
m_TimeStamp = localtimestamp;
m_frameNumber++;
m_newFrameAvailable = true;
m_newFrameAvailableCond.notify_one();
}
}
rawImage.ReleaseBuffer();
rawImage2.ReleaseBuffer();
rgbImage.ReleaseBuffer();
m_newFrameAvailableCond.notify_one();
stopCapturing();
m_GrabbingFinished = true;
}
/****************************************************************************************/
int VideoSaverFlyCapture::stopCapturing() {
if (!isFlycapture()) {
return VideoSaver::stopCapturing();
} else {
// stop the camera
FlyCapture2::Error error = m_Camera.StopCapture();
if ( error != FlyCapture2::PGRERROR_OK ) {
error.PrintErrorTrace();
return -1;
} else {
return 0;
}
}
}