-
Notifications
You must be signed in to change notification settings - Fork 3
/
GenCameraDriver.h
591 lines (515 loc) · 15.1 KB
/
GenCameraDriver.h
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/**
@brief Generic Camera Driver Class
@author Shane Yuan
@date Dec 29, 2017
*/
#ifndef __GENERIC_CAMERA_DRIVER_H__
#define __GENERIC_CAMERA_DRIVER_H__
// include std
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <thread>
#include <memory>
// opencv
#include <opencv2/opencv.hpp>
// cuda
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#endif
#include <cuda.h>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
namespace cam {
// colorful terminal output and file utility
enum class ConsoleColor {
red = 12,
blue = 9,
green = 10,
yellow = 14,
white = 15,
pink = 13,
cyan = 11
};
#ifndef WIN32
#define BLACK_TEXT(x) "\033[30;1m" << x << "\033[0m"
#define RED_TEXT(x) "\033[31;1m" << x << "\033[0m"
#define GREEN_TEXT(x) "\033[32;1m" << x << "\033[0m"
#define YELLOW_TEXT(x) "\033[33;1m" << x << "\033[0m"
#define BLUE_TEXT(x) "\033[34;1m" << x << "\033[0m"
#define MAGENTA_TEXT(x) "\033[35;1m" << x << "\033[0m"
#define CYAN_TEXT(x) "\033[36;1m" << x << "\033[0m"
#define WHITE_TEXT(x) "\033[37;1m" << x << "\033[0m"
#endif
class SysUtil {
public:
/***********************************************************/
/* mkdir function */
/***********************************************************/
static int mkdir(char* dir) {
#ifdef WIN32
_mkdir(dir);
#else
char command[256];
sprintf(command, "mkdir %s", dir);
system(command);
#endif
return 0;
}
static int mkdir(std::string dir) {
return mkdir((char *)dir.c_str());
}
/***********************************************************/
/* sleep function */
/***********************************************************/
static int sleep(size_t miliseconds) {
std::this_thread::sleep_for(std::chrono::milliseconds(miliseconds));
return 0;
}
/***********************************************************/
/* make colorful console output */
/***********************************************************/
static int setConsoleColor(ConsoleColor color) {
#ifdef WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), static_cast<int>(color));
#endif
return 0;
}
/***********************************************************/
/* warning error output */
/***********************************************************/
static int errorOutput(std::string info) {
#ifdef WIN32
SysUtil::setConsoleColor(ConsoleColor::red);
std::cerr << "ERROR: " << info.c_str() << std::endl;
SysUtil::setConsoleColor(ConsoleColor::white);
#else
std::cerr << RED_TEXT("ERROR: ") << RED_TEXT(info.c_str())
<< std::endl;
#endif
return 0;
}
static int warningOutput(std::string info) {
#ifdef WIN32
SysUtil::setConsoleColor(ConsoleColor::yellow);
std::cerr << "WARNING: " << info.c_str() << std::endl;
SysUtil::setConsoleColor(ConsoleColor::white);
#else
std::cerr << YELLOW_TEXT("WARNING: ") << YELLOW_TEXT(info.c_str())
<< std::endl;
#endif
return 0;
}
static int infoOutput(std::string info) {
#ifdef WIN32
SysUtil::setConsoleColor(ConsoleColor::green);
std::cerr << "INFO: " << info.c_str() << std::endl;
SysUtil::setConsoleColor(ConsoleColor::white);
#else
std::cerr << GREEN_TEXT("INFO: ") << GREEN_TEXT(info.c_str())
<< std::endl;
#endif
return 0;
}
static int debugOutput(std::string info) {
#ifdef WIN32
SysUtil::setConsoleColor(ConsoleColor::pink);
std::cerr << "DEBUG INFO: " << info.c_str() << std::endl;
SysUtil::setConsoleColor(ConsoleColor::white);
#else
std::cerr << MAGENTA_TEXT("DEBUG INFO: ") << MAGENTA_TEXT(info.c_str())
<< std::endl;
#endif
return 0;
}
};
enum class Status {
on = 1,
off = 0
};
enum class CameraModel {
XIMEA_xiC = 0,
PointGrey_u3 = 1,
Network = 2,
File = 3
};
/**
@brief bayer pattern type (same as Nvidia NPP setting)
*/
enum class GenCamBayerPattern {
BayerBGGR = 0,
BayerRGGB = 1,
BayerGBRG = 2,
BayerGRBG = 3
};
/**
@brief camera info class
*/
class GenCamInfo {
public:
std::string sn;
int width;
int height;
float fps;
Status autoExposure;
GenCamBayerPattern bayerPattern;
float redGain;
float greenGain;
float blueGain;
// raw wb type
// true: raw data is after white balance
// false: raw data is before white balance
bool isWBRaw;
};
/**
@brief capture mode
*/
enum class GenCamCaptureMode {
Single = 0, // capture one by one without buffer
Continous = 1, // capture and buffer images
SingleTrigger = 2,
ContinousTrigger = 3
};
/**
@brief capture purpose
*/
enum class GenCamCapturePurpose {
Streaming = 0, // capture images to buffers circularly
Recording = 1, // capture images to fill the buffer once
FileCameraRecording = 2
};
/**
@brief buffer type
*/
enum class GenCamBufferType {
Raw = 0, // save 8-bit raw images in buffer
JPEG = 1, // save jpeg compressed images in buffer
// usually need as power GPU to compress the raw images
RGB24 = 2, // save demosaiced 3 channel RGB images in buffer
Raw16 = 3 // save 16-bit raw images in buffer
};
/**
@brief class to save JPEG data
*/
struct Imagedata {
char* data; // data pointer
GenCamBufferType type; // buffer data type
size_t maxLength; // max malloced memory size
size_t length; // jpeg data length
Imagedata(): data(NULL), length(0) {}
~Imagedata() {}
/**
@brief deep copy
@return
*/
Imagedata deepCopy() {
Imagedata out;
out.type = this->type;
out.length = this->length;
out.maxLength = this->maxLength;
// malloc memory
out.data = new char[out.maxLength];
memcpy(out.data, this->data, out.length);
return out;
}
/**
@brief release function
@return int
*/
int release() { delete[] data; }
};
/**
@brief generic camera class
*/
class GenCamera {
protected:
// camera model
CameraModel camModel;
// capture for real-time view or saving
GenCamCapturePurpose camPurpose;
std::vector<GenCamInfo> camInfos;
// camera status
bool isInit;
bool isCapture;
bool isVerbose; // enable log in capturing images
// camera buffer
GenCamBufferType bufferType;
std::vector<std::vector<Imagedata>> bufferImgs;
int bufferSize;
size_t cameraNum;
// capture model
GenCamCaptureMode captureMode;
// frame indices in buffer
std::vector<int> thBufferInds;
// npp jpeg coder class
int JPEGQuality;
float sizeRatio;
// image mapping vector in capturing function
std::vector<size_t> mappingVector;
public:
protected:
public:
GenCamera();
~GenCamera();
/*************************************************************/
/* basic camera function */
/*************************************************************/
/**
@brief init camera
@return int
*/
virtual int init() = 0;
/**
@brief let cameras start capturing images
@return int
*/
virtual int startCapture() = 0;
/**
@brief let cameras stop capturing images
@return int
*/
virtual int stopCapture() = 0;
/**
@brief release camera
@return int
*/
virtual int release() = 0;
/**
@brief get camera information
@param std::vector<GenCamInfo> & camInfos: output camera infos
@return int
*/
virtual int getCamInfos(std::vector<GenCamInfo> & camInfos) = 0;
/*************************************************************/
/* camera setting function */
/*************************************************************/
/**
@brief set frame rate
@param float fps: input fps
@param float exposureUpperLimitRatio: exposure upper limit time, make
exposure upper limit time = 1000000us / fps * 0.8
@return int
*/
virtual int setFPS(int camInd, float fps, float exposureUpperLimitRatio = 0.8) = 0;
/**
@brief set auto white balance
@param int ind: index of camera (-1 means all the cameras)
@return int
*/
virtual int setAutoWhiteBalance(int camInd) = 0;
/**
@brief set auto white balance
@param int ind: index of camera (-1 means all the cameras)
@param float redGain: red gain of the white balance
@param float greenGain: green gain of the white balance
@param float blueGain: blue gain of the white balance
@return int
*/
virtual int setWhiteBalance(int camInd, float redGain,
float greenGain, float blueGain) = 0;
/**
@brief set auto exposure
@param int ind: index of camera (-1 means all the cameras)
@param Status autoExposure: if use auto exposure
@return int
*/
virtual int setAutoExposure(int camInd, Status autoExposure) = 0;
/**
@brief set auto exposure level (only support XIMEA cameras)
@param int ind: index of camera (-1 means all the cameras)
@param float level: auto exposure level, average intensity of output
signal AEAG should achieve
@return int
*/
virtual int setAutoExposureLevel(int camInd, float level) = 0;
/**
@brief set auto exposure compensation (only support PointGrey cameras)
@param int ind: index of camera (-1 means all the cameras)
@param Status status: if use auto EV value
@param float relativeEV: only valid when the second argument is off.
The reason why use relative EV value here is to directly set a absolute
value is difficult
@return int
*/
virtual int setAutoExposureCompensation(int camInd,
Status status, float relativeEV) = 0;
/**
@brief set brightness time
@param int brightness: input brightness
+1: brighten, -1: darken, 0: do nothing
@return int
*/
virtual int adjustBrightness(int camInd, int brightness) = 0;
/**
@brief set exposure time
@param int camInd: index of camera (-1 means all the cameras)
@param int time: exposure time (in microseconds)
@return int
*/
virtual int setExposure(int camInd, int time) = 0;
/**
@brief set/get bayer pattern
@param int camInd: input camera index
@param GenCamBayerPattern & bayerPattern: output bayer pattern
@return int
*/
virtual int getBayerPattern(int camInd, GenCamBayerPattern & bayerPattern) = 0;
/**
@brief make setting effective
by capturing some frames
@return int
*/
virtual int makeSetEffective(int k = 10) = 0;
/*************************************************************/
/* capturing function */
/*************************************************************/
/**
@brief set capturing mode
@param GenCamCaptureMode captureMode: capture mode
@param int size: buffer size
@return int
*/
virtual int setCaptureMode(GenCamCaptureMode captureMode,
int bufferSize) = 0;
/**
@brief wait for recording threads to finish
@return int
*/
virtual int waitForRecordFinish() = 0;
/**
@brief start capturing threads
capturing threads captures images from cameras, and buffer to
bufferImgs vector, if buffer type is jpeg, this function will start
a thread to compress captured images into buffer vector
@return int
*/
virtual int startCaptureThreads() = 0;
/**
@brief stop capturing threads
@return int
*/
virtual int stopCaptureThreads() = 0;
/*************************************************************/
/* function to update images in buffer */
/*************************************************************/
/**
@brief buffer next frame
@return int
*/
virtual int reBufferFileCamera() = 0;
/**
@brief buffer next frame
@return int
*/
virtual int bufferNextFrame() = 0;
/*************************************************************/
/* non-virtual setting function */
/*************************************************************/
/**
@brief set verbose
@param bool isVerbose: true, verbose mode, output many infomations
for debugging
@return int
*/
int setVerbose(bool isVerbose);
/**
@brief set buffer type
@param GenCamBufferType type: buffer type
@return int
*/
int setCamBufferType(GenCamBufferType type);
/**
@brief set jpeg compression quality
@param int quality: JPEG compression quality (1 - 100)
@param float sizeRatio: expected compression ratio used for
pre-malloc memory, too small will cause npp jpeg coder crash
(default 0.2)
@return int
*/
int setJPEGQuality(int quality, float sizeRatio = 0.2);
/**
@brief set capture purpose
@param GenCamCapturePurpose camPurpose: purpose, for streaming or recording
@return int
*/
int setCapturePurpose(GenCamCapturePurpose camPurpose);
/**
@brief get capture purpose
@param GenCamCapturePurpose camPurpose: purpose, for streaming or recording
@return int
*/
GenCamCapturePurpose getCapturePurpose();
/*************************************************************/
/* function to save capture images to files */
/*************************************************************/
/**
@brief save captured images to dir
@param std::string dir: input dir to save images
@return int
*/
int saveImages(std::string dir);
/**
@brief save captured videos to dir
@param std::string dir: input dir to save videos
@return int
*/
int saveVideos(std::string dir);
/*************************************************************/
/* function to set mapping vector of capture function */
/* and function to capture images */
/* old function will be deprecated in the future */
/*************************************************************/
/**
@brief set mapping vector of capture function
@param std::vector<size_t> mappingVector: input mapping vector
@return int
*/
int setMappingVector(std::vector<size_t> mappingVector);
/**
@brief capture one frame
@param std::vector<Imagedata> & imgs: output captured images
if in single mode, memory of image mats should be malloced
before using this function
@return int
*/
int captureFrame(std::vector<Imagedata> & imgs);
/**
@brief capture one frame with mapping
@param std::vector<Imagedata> & imgs: output captured images
if in single mode, memory of image mats should be malloced
before using this function
@return int
*/
int captureFrameWithMapping(std::vector<Imagedata> & imgs);
/**
@brief get camera infos list with mapping
@param std::vector<cam::GenCamInfo> & camInfos: output camera info list
@return int
*/
int getCameraInfoListsWithMapping(std::vector<cam::GenCamInfo> & camInfos);
/*************************************************************/
/* function to capture images */
/*************************************************************/
/**
@brief capture one frame
@param std::vector<Imagedata> & refImgs: output reference images
@param std::vector<Imagedata> & localImgs: output localview images
@param std::vector<int> refInds: input reference indices
@param std::vector<int> localInds: input local indices
@return int
*/
int captureFrame(std::vector<Imagedata> & refImgs,
std::vector<Imagedata> & localImgs,
std::vector<int> refInds,
std::vector<int> localInds);
};
/**
@breif function to init camera array
@return
*/
std::shared_ptr<GenCamera> createCamera(CameraModel camModel,
std::string dir = "");
};
#endif