-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·523 lines (413 loc) · 15.3 KB
/
main.cpp
File metadata and controls
executable file
·523 lines (413 loc) · 15.3 KB
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
#include "main.h"
int find_argument(int argc, char** argv, const char* argument_name)
{
for(int i = 1; i < argc; ++i)
{
if(strcmp(argv[i], argument_name) == 0)
{
return (i);
}
}
return (-1);
}
int parse_argument(int argc, char** argv, const char* str, int &val)
{
int index = find_argument(argc, argv, str) + 1;
if(index > 0 && index < argc)
{
val = atoi(argv[index]);
}
return (index - 1);
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
int width = 640;
int height = 480;
int fps = 30;
int tcp = 0;
parse_argument(argc, argv, "-w", width);
parse_argument(argc, argv, "-h", height);
parse_argument(argc, argv, "-f", fps);
tcp = find_argument(argc, argv, "-t") != -1;
MainWindow * window = new MainWindow(width, height, fps, tcp);
window->show();
return app.exec();
}
MainWindow::MainWindow(int width, int height, int fps, bool tcp)
: logger(0),
depthImage(width, height, QImage::Format_RGB888),
rgbImage(width, height, QImage::Format_RGB888),
recording(false),
lastDrawn(-1),
width(width),
height(height),
fps(fps),
tcp(tcp),
comms(tcp ? new Communicator : 0)
{
this->setMaximumSize(width * 2, height + 160);
this->setMinimumSize(width * 2, height + 160);
QVBoxLayout * wrapperLayout = new QVBoxLayout;
QHBoxLayout * mainLayout = new QHBoxLayout;
QHBoxLayout * fileLayout = new QHBoxLayout;
QHBoxLayout * optionLayout = new QHBoxLayout;
QHBoxLayout * buttonLayout = new QHBoxLayout;
wrapperLayout->addLayout(mainLayout);
depthLabel = new QLabel(this);
depthLabel->setPixmap(QPixmap::fromImage(depthImage));
mainLayout->addWidget(depthLabel);
imageLabel = new QLabel(this);
imageLabel->setPixmap(QPixmap::fromImage(rgbImage));
mainLayout->addWidget(imageLabel);
wrapperLayout->addLayout(fileLayout);
wrapperLayout->addLayout(optionLayout);
QLabel * logLabel = new QLabel("Log file: ", this);
logLabel->setMaximumWidth(logLabel->fontMetrics().boundingRect(logLabel->text()).width());
fileLayout->addWidget(logLabel);
logFile = new QLabel(this);
logFile->setTextInteractionFlags(Qt::TextSelectableByMouse);
logFile->setStyleSheet("border: 1px solid grey");
fileLayout->addWidget(logFile);
#ifdef __APPLE__
int cushion = 25;
#else
int cushion = 10;
#endif
browseButton = new QPushButton("Browse", this);
browseButton->setMaximumWidth(browseButton->fontMetrics().boundingRect(browseButton->text()).width() + cushion);
connect(browseButton, SIGNAL(clicked()), this, SLOT(fileBrowse()));
fileLayout->addWidget(browseButton);
dateNameButton = new QPushButton("Date filename", this);
dateNameButton->setMaximumWidth(dateNameButton->fontMetrics().boundingRect(dateNameButton->text()).width() + cushion);
connect(dateNameButton, SIGNAL(clicked()), this, SLOT(dateFilename()));
fileLayout->addWidget(dateNameButton);
autoExposure = new QCheckBox("Auto Exposure");
autoExposure->setChecked(false);
autoWhiteBalance = new QCheckBox("Auto White Balance");
autoWhiteBalance->setChecked(false);
compressed = new QCheckBox("Compressed");
compressed->setChecked(true);
memoryRecord = new QCheckBox("Record to RAM");
memoryRecord->setChecked(false);
memoryStatus = new QLabel("");
connect(autoExposure, SIGNAL(stateChanged(int)), this, SLOT(setExposure()));
connect(autoWhiteBalance, SIGNAL(stateChanged(int)), this, SLOT(setWhiteBalance()));
connect(compressed, SIGNAL(released()), this, SLOT(setCompressed()));
connect(memoryRecord, SIGNAL(stateChanged(int)), this, SLOT(setMemoryRecord()));
optionLayout->addWidget(autoExposure);
optionLayout->addWidget(autoWhiteBalance);
optionLayout->addWidget(compressed);
optionLayout->addWidget(memoryRecord);
optionLayout->addWidget(memoryStatus);
wrapperLayout->addLayout(buttonLayout);
startStop = new QPushButton(tcp ? "Stream && Record" : "Record", this);
connect(startStop, SIGNAL(clicked()), this, SLOT(recordToggle()));
buttonLayout->addWidget(startStop);
QPushButton * quitButton = new QPushButton("Quit", this);
connect(quitButton, SIGNAL(clicked()), this, SLOT(quit()));
buttonLayout->addWidget(quitButton);
setLayout(wrapperLayout);
startStop->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
quitButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QFont currentFont = startStop->font();
currentFont.setPointSize(currentFont.pointSize() + 8);
startStop->setFont(currentFont);
quitButton->setFont(currentFont);
painter = new QPainter(&depthImage);
painter->setPen(Qt::green);
painter->setFont(QFont("Arial", 30));
painter->drawText(10, 50, "Attempting to start OpenNI2...");
depthLabel->setPixmap(QPixmap::fromImage(depthImage));
#ifndef OS_WINDOWS
char * homeDir = getenv("HOME");
logFolder.append(homeDir);
logFolder.append("/");
#else
char * homeDrive = getenv("HOMEDRIVE");
char * homeDir = getenv("HOMEPATH");
logFolder.append(homeDrive);
logFolder.append("\\");
logFolder.append(homeDir);
logFolder.append("\\");
#endif
logFolder.append("Kinect_Logs");
boost::filesystem::path p(logFolder.c_str());
boost::filesystem::create_directory(p);
logFile->setText(QString::fromStdString(getNextFilename()));
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerCallback()));
timer->start(15);
}
MainWindow::~MainWindow()
{
timer->stop();
delete logger;
}
std::string MainWindow::getNextFilename()
{
static char const* const fmt = "%Y-%m-%d";
std::ostringstream ss;
ss.imbue(std::locale(std::cout.getloc(), new boost::gregorian::date_facet(fmt)));
ss << boost::gregorian::day_clock::universal_day();
std::string dateFilename;
if(!lastFilename.length())
{
dateFilename = ss.str();
}
else
{
dateFilename = lastFilename;
}
std::string currentFile;
int currentNum = 0;
while(true)
{
std::stringstream strs;
strs << logFolder;
#ifndef OS_WINDOWS
strs << "/";
#else
strs << "\\";
#endif
strs << dateFilename << ".";
strs << std::setfill('0') << std::setw(2) << currentNum;
strs << ".klg";
if(!boost::filesystem::exists(strs.str().c_str()))
{
return strs.str();
}
currentNum++;
}
return "";
}
void MainWindow::dateFilename()
{
lastFilename.clear();
logFile->setText(QString::fromStdString(getNextFilename()));
}
void MainWindow::fileBrowse()
{
QString message = "Log file selection";
QString types = "All files (*)";
QString fileName = QFileDialog::getSaveFileName(this, message, ".", types);
if(!fileName.isEmpty())
{
if(!fileName.contains(".klg", Qt::CaseInsensitive))
{
fileName.append(".klg");
}
#ifndef OS_WINDOWS
logFolder = fileName.toStdString().substr(0, fileName.toStdString().rfind("/"));
lastFilename = fileName.toStdString().substr(fileName.toStdString().rfind("/") + 1, fileName.toStdString().rfind(".klg"));
#else
logFolder = fileName.toStdString().substr(0, fileName.toStdString().rfind("\\"));
lastFilename = fileName.toStdString().substr(fileName.toStdString().rfind("\\") + 1, fileName.toStdString().rfind(".klg"));
#endif
lastFilename = lastFilename.substr(0, lastFilename.size() - 4);
logFile->setText(QString::fromStdString(getNextFilename()));
}
}
void MainWindow::recordToggle()
{
if(!recording)
{
if(logFile->text().length() == 0)
{
QMessageBox::information(this, "Information", "You have not selected an output log file");
}
else
{
memoryRecord->setEnabled(false);
compressed->setEnabled(false);
logger->startWriting(logFile->text().toStdString());
startStop->setText("Stop");
recording = true;
}
}
else
{
logger->stopWriting(this);
memoryRecord->setEnabled(true);
compressed->setEnabled(true);
startStop->setText(tcp ? "Stream && Record" : "Record");
recording = false;
logFile->setText(QString::fromStdString(getNextFilename()));
}
}
void MainWindow::setExposure()
{
logger->getOpenNI2Interface()->setAutoExposure(autoExposure->isChecked());
}
void MainWindow::setWhiteBalance()
{
logger->getOpenNI2Interface()->setAutoWhiteBalance(autoWhiteBalance->isChecked());
}
void MainWindow::setCompressed()
{
if(compressed->isChecked())
{
logger->setCompressed(compressed->isChecked());
}
else if(!compressed->isChecked())
{
if(QMessageBox::question(this, "Compression?", "If you don't have a fast machine or an SSD hard drive you might drop frames, are you sure?", "&No", "&Yes", QString::null, 0, 1 ))
{
logger->setCompressed(compressed->isChecked());
}
else
{
compressed->setChecked(true);
logger->setCompressed(compressed->isChecked());
}
}
}
void MainWindow::setMemoryRecord()
{
logger->setMemoryRecord(memoryRecord->isChecked());
}
void MainWindow::quit()
{
if(QMessageBox::question(this, "Quit?", "Are you sure you want to quit?", "&No", "&Yes", QString::null, 0, 1 ))
{
if(recording)
{
recordToggle();
}
this->close();
}
}
void MainWindow::timerCallback()
{
int64_t usedMemory = MemoryBuffer::getUsedSystemMemory();
int64_t totalMemory = MemoryBuffer::getTotalSystemMemory();
int64_t processMemory = MemoryBuffer::getProcessMemory();
float usedGB = (usedMemory / (float)1073741824);
float totalGB = (totalMemory / (float)1073741824);
#ifdef __APPLE__
float processGB = (processMemory / (float)1073741824);
#else
float processGB = (processMemory / (float)1048576);
#endif
QString memoryInfo = QString::number(usedGB, 'f', 2) + "/" + QString::number(totalGB, 'f', 2) + "GB memory used, " + QString::number(processGB, 'f', 2) + "GB by Logger2";
memoryStatus->setText(memoryInfo);
if(!logger)
{
if(frameStats.size() >= 15)
{
logger = new Logger2(width, height, fps, tcp);
if(!logger->getOpenNI2Interface()->ok())
{
memset(depthImage.bits(), 0, width * height * 3);
painter->setPen(Qt::red);
painter->setFont(QFont("Arial", 30));
painter->drawText(10, 50, "Attempting to start OpenNI2... failed!");
depthLabel->setPixmap(QPixmap::fromImage(depthImage));
QMessageBox msgBox;
msgBox.setText("Sorry, OpenNI2 is having trouble (it's still in beta). Please try running Logger2 again.");
msgBox.setDetailedText(QString::fromStdString(logger->getOpenNI2Interface()->error()));
msgBox.exec();
exit(-1);
}
else
{
memset(depthImage.bits(), 0, width * height * 3);
painter->setPen(Qt::green);
painter->setFont(QFont("Arial", 30));
painter->drawText(10, 50, "Starting stream...");
depthLabel->setPixmap(QPixmap::fromImage(depthImage));
}
return;
}
else
{
frameStats.push_back(0);
return;
}
}
int lastDepth = logger->getOpenNI2Interface()->latestDepthIndex.getValue();
if(lastDepth == -1)
{
return;
}
int bufferIndex = lastDepth % OpenNI2Interface::numBuffers;
if(bufferIndex == lastDrawn)
{
return;
}
if(lastFrameTime == logger->getOpenNI2Interface()->frameBuffers[bufferIndex].second)
{
return;
}
memcpy(&depthBuffer[0], logger->getOpenNI2Interface()->frameBuffers[bufferIndex].first.first, width * height * 2);
if(!(tcp && recording))
{
memcpy(rgbImage.bits(), logger->getOpenNI2Interface()->frameBuffers[bufferIndex].first.second, width * height * 3);
}
cv::Mat1w depth(height, width, (unsigned short *)&depthBuffer[0]);
normalize(depth, tmp, 0, 255, cv::NORM_MINMAX, 0);
cv::Mat3b depthImg(height, width, (cv::Vec<unsigned char, 3> *)depthImage.bits());
cv::cvtColor(tmp, depthImg, CV_GRAY2RGB);
painter->setPen(recording ? Qt::red : Qt::green);
painter->setFont(QFont("Arial", 30));
painter->drawText(10, 50, recording ? (tcp ? "Streaming & Recording" : "Recording") : "Viewing");
frameStats.push_back(abs(logger->getOpenNI2Interface()->frameBuffers[bufferIndex].second - lastFrameTime));
if(frameStats.size() > 15)
{
frameStats.erase(frameStats.begin());
}
int64_t speedSum = 0;
for(unsigned int i = 0; i < frameStats.size(); i++)
{
speedSum += frameStats[i];
}
int64_t avgSpeed = (float)speedSum / (float)frameStats.size();
float fps = 1.0f / ((float)avgSpeed / 1000000.0f);
fps = floor(fps * 10.0f);
fps /= 10.0f;
std::stringstream str;
str << (int)ceil(fps) << "fps";
lastFrameTime = logger->getOpenNI2Interface()->frameBuffers[bufferIndex].second;
painter->setFont(QFont("Arial", 24));
#ifdef __APPLE__
int offset = 20;
#else
int offset = 10;
#endif
painter->drawText(10, height - offset, QString::fromStdString(str.str()));
if(tcp)
{
cv::Mat3b modelImg(height / 4, width / 4);
cv::Mat3b modelImgBig(height, width, (cv::Vec<unsigned char, 3> *)rgbImage.bits());
std::string dataStr = comms->tryRecv();
if(dataStr.length())
{
std::vector<char> data(dataStr.begin(), dataStr.end());
modelImg = cv::imdecode(cv::Mat(data), 1);
cv::Size bigSize(width, height);
cv::resize(modelImg, modelImgBig, bigSize, 0, 0);
}
}
depthLabel->setPixmap(QPixmap::fromImage(depthImage));
imageLabel->setPixmap(QPixmap::fromImage(rgbImage));
if(logger->getMemoryBuffer().memoryFull.getValue())
{
assert(recording);
recordToggle();
QMessageBox msgBox;
msgBox.setText("Recording has been automatically stopped to prevent running out of system memory.");
msgBox.exec();
}
std::pair<bool, int64_t> dropping = logger->dropping.getValue();
if(!tcp && dropping.first)
{
assert(recording);
recordToggle();
std::stringstream strs;
strs << "Recording has been automatically stopped. Logging experienced a jump of " << dropping.second / 1000
<< "ms, indicating a drop below 10fps. Please try enabling compression or recording to RAM to prevent this.";
QMessageBox msgBox;
msgBox.setText(QString::fromStdString(strs.str()));
msgBox.exec();
}
}