-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideoThread.cpp.orig
More file actions
650 lines (513 loc) · 18.3 KB
/
videoThread.cpp.orig
File metadata and controls
650 lines (513 loc) · 18.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
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
// Opencv includes.
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
// Aldebaran includes.
#include <alvision/alimage.h>
#include <alcommon/alproxy.h>
#include <alvision/alvisiondefinitions.h>
#include <alproxies/altexttospeechproxy.h>
#include <alvalue/alvalue.h>
#include <alproxies/almotionproxy.h>
#include <alproxies/alrobotpostureproxy.h>
#include <alproxies/almemoryproxy.h>
#include <alproxies/alsonarproxy.h>
// Math includes
#include <almath/types/alpose2d.h>
#include <almath/tools/aldubinscurve.h>
#include <almath/tools/almathio.h>
#include <almath/tools/altrigonometry.h>
//File
#include <sys/stat.h>
#include <cstdio>
#include <sstream>
#include "videoThread.hpp"
#include <QMutex>
#include <QDateTime>
volatile int quit_signal=0;
#ifdef __unix__
#include <signal.h>
extern "C" void quit_signal_handler(int signum) {
if (quit_signal!=0) exit(0); // just exit already
quit_signal=1;
printf("Will quit at next camera frame (repeat to kill now)\n");
}
#endif
#define SSTR( x ) static_cast< std::ostringstream & >( \
( std::ostringstream() << std::dec << x ) ).str()
using namespace AL;
using namespace cv;
using namespace std;
QString randString(int len)
{
QString str;
str.resize(len);
for (int s = 0; s < len ; ++s)
str[s] = QChar('A' + char(qrand() % ('Z' - 'A')));
return str;
}
//Class Implementation
VideoThread::VideoThread() : isConnected(false),
isRecording(false),
savePictureLockFlag(false),
screenshotCounter(0),
firstScreenshot(true),
isWebcam(false),
functionSelected("Nenhuma")
{
#ifdef __unix__
signal(SIGINT,quit_signal_handler); // listen for ctrl-C
#endif
}
VideoThread::~VideoThread()
{
}
void VideoThread::connectVideo(bool connect, const QString &naoIP)
{
if(connect){
if(isConnected)
{
return;
}
isAlive = true;
storeVideo = false;
strIP = naoIP.toUtf8().constData();
if(naoIP == "0")
{
isWebcam = true;
cap = new cv::VideoCapture;
cap->set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap->set(CV_CAP_PROP_FRAME_HEIGHT, 480);
if(!cap->open(0))
{
QString message;
message = "Cannot open Webcam.";
emit statusMessage(message);
emit connection(false);
return;
}
}
else
{
isWebcam = false;
try
{
camProxy = new ALVideoDeviceProxy(strIP);
record = new ALVideoRecorderProxy(strIP);
}
catch(const AL::ALError &e)
{
QString message;
message = "Cannot connect to robot.";
emit statusMessage(message);
emit connection(false);
return;
}
record->setFrameRate(30);
record->setResolution(kVGA);
record->setColorSpace(kBGRColorSpace);
QString rndStr = randString(14);
clientName = rndStr.toUtf8().constData();
AL::ALValue cameraIndexes, cameraResolution ,cameraColorSpaces;
cameraIndexes.arraySetSize(2);
cameraResolution.arraySetSize(2);
cameraColorSpaces.arraySetSize(2);
cameraIndexes[0] = kTopCamera;
cameraIndexes[1] = kBottomCamera;
cameraResolution[0] = AL::kQVGA;
cameraResolution[1] = AL::kQVGA;
cameraColorSpaces[0] = AL::kBGRColorSpace;
cameraColorSpaces[1] = AL::kBGRColorSpace;
clientName = camProxy->subscribeCameras(clientName, cameraIndexes, cameraResolution, cameraColorSpaces, 30);
/**Change Camera */
//cout << "Change Camera. " << clientName << endl;
//camProxy->setParam(kCameraSelectID,0); //CHANGE TO THE BOTTOM CAMERA (0 - TOP, 1 - BOTTOM)
record->setCameraID(0);
cameraID = "top";
}
imgHeader = Mat(cv::Size(640, 480), CV_8UC3);
imgSafe = Mat(cv::Size(640, 480), CV_8UC3);
isConnected = true;
emit connection(true);
emit startLoop();
}
else{
//Essa parte ainda não funciona :(
isConnected = false;
emit connection(false);
cout << "desconectando" << endl;
if(isWebcam){
cap->release();
isWebcam = false;
cout<< "fechou =" << cap->isOpened() << endl;
delete(cap);
}
}
}
void VideoThread::videoLoop()
{
if(isConnected){
if(!isWebcam)
{
std::cout << camProxy->getActiveCameras(clientName) << std::endl;
ALValue imgAlValues = camProxy->getImagesRemote(clientName);
<<<<<<< HEAD
//Captar as duas img para imgHead e imgBody
camProxy->setCameraParameter("imgHead",kCameraSelectID, 1);
ALValue imgHeadValue = camProxy->getImageRemote(clientName);
camProxy->setCameraParameter("imgBody",kCameraSelectID, 0);
ALValue imgBodyValue = camProxy->getImageRemote(clientName);
if(img.getSize() < 7 || imgHeadValue.getSize() < 7 || imgBodyValue.getSize() < 7)
=======
if(imgAlValues[0].getSize() < 7 || imgAlValues[1].getSize() < 7)
>>>>>>> temp
{
QString message;
message = "Fail to Get Frame.";
emit statusMessage(message);
return;
}
//Captar as duas img para imgHead e imgBody
<<<<<<< HEAD
imgHead.data = (uchar*) imgHeadValue[6].GetBinary();
imgBody.data = (uchar*) imgBodyValue[6].GetBinary();
imgHeader.data = (uchar*) img[6].GetBinary();
=======
cv::Mat inputTop, inputBot;
inputTop = cv::Mat(cv::Size(320,240),CV_8UC3);
inputTop.data = (uchar*) imgAlValues[0][6].GetBinary();
inputBot = cv::Mat(cv::Size(320,240),CV_8UC3);
inputBot.data = (uchar*) imgAlValues[1][6].GetBinary();
inputBot.copyTo(imgBody);
inputTop.copyTo(imgHead);
>>>>>>> temp
//imgHeader.data = (uchar*) imgAlValues[0][6].GetBinary();
QImage image((uchar*)inputTop.data, inputTop.cols, inputTop.rows, inputTop.step, QImage::Format_RGB888);
imgContainer.image = image.rgbSwapped();
imagePipe.save(imgContainer);
perception2Frednator(functionSelected, vectorSelection);
camProxy->releaseImages(clientName);
}
else
{
(*cap) >> imgHeader;
if (quit_signal) exit(0); // exit cleanly on interrupt
QImage image((uchar*)imgHeader.data, imgHeader.cols, imgHeader.rows, imgHeader.step, QImage::Format_RGB888);
imgContainer.image = image.rgbSwapped();
imagePipe.save(imgContainer);
perception2Frednator(functionSelected, vectorSelection);
}
}
else
{
emit sendFrame();
}
}
//função para fazer a vectorSelectionInterface com o vectorSelectionInterface
cv::Mat VideoThread::vectorSelectionInterface(QComboBox *vectorSelection, PerceptionData *visionData, QString *functionName){
int vectorSelectionCounter = 0;
//preenche o comboBox
if(lastFunctionName != functionName){
while(vectorSelection->count() < visionData->debugImages[functionName->toStdString()].size()){
vectorSelection->insertItem(vectorSelectionCounter, QString::number(vectorSelectionCounter));
vectorSelectionCounter++;
}
}
//Imagem de interesse é a selecionada
if(!vectorSelection->currentText().isEmpty()){
if(vectorSelection->currentText().toInt() < visionData->debugImages[functionName->toStdString()].size()){ // se o valor selecionado estiver dentro do vetor
<<<<<<< HEAD
return visionData->debugImages[functionName->toStdString()][vectorSelection->currentText().toInt()];
}
else{
return visionData->debugImages[functionName->toStdString()][0];
}
=======
return visionData->debugImages[functionName->toStdString()][vectorSelection->currentText().toInt()];
}
else{
return visionData->debugImages[functionName->toStdString()][0];
}
>>>>>>> temp
} else{
return visionData->debugImages[functionName->toStdString()][0];
}
lastFunctionName = *functionName;
}
void VideoThread::perception2Frednator(QString functionName, QComboBox* vectorSelection){
<<<<<<< HEAD
cv::Mat cvMatImg;
=======
cv::Mat returnImg, srcImg;
>>>>>>> temp
if(functionSelected == "Nenhuma"){
emit sendFrame();
return;
}
else if(functionName == functionSelected){
if(isWebcam){
if(functionSelected == "ballCandidate"){
emit sendFrame();
return;
}
<<<<<<< HEAD
if(functionSelected == "ballDetector"){
ballDetector.run(imgHeader, imgHeader, &visionData);
cvMatImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
//Funciona
if(functionSelected == "ellipseDetector"){
ellipseDetector.run(imgHeader, imgHeader, &visionData);//roda a classe e atualiza a visionData
cvMatImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
//Funciona
if(functionSelected == "fieldDetector"){
fieldDetector.run(imgHeader, imgHeader, &visionData);//roda a classe e atualiza a visionData
cvMatImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
//Funciona
if(functionSelected == "goalDetector"){
goalDetector.run(imgHeader, imgHeader, &visionData); //roda a classe e atualiza a visionData
cvMatImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
//Funciona
if(functionSelected == "lineDetector"){
lineDetector.run(imgHeader, imgHeader, &visionData); //roda a classe e atualiza a visionData
cvMatImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
//Funciona
if(functionSelected == "yellowDetector"){
yellowDetector.run(imgHeader, imgHeader, &visionData); //roda a classe e atualiza a visionData
cvMatImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
=======
if(functionSelected == "ballDetector"){
ballDetector.run(imgHead, imgHead, &visionData);
returnImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
//Funciona
if(functionSelected == "ellipseDetector"){
ellipseDetector.run(imgHead, imgHead, &visionData);//roda a classe e atualiza a visionData
returnImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
//Funciona
if(functionSelected == "fieldDetector"){
fieldDetector.run(imgHead, imgHead, &visionData);//roda a classe e atualiza a visionData
returnImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
//Funciona
if(functionSelected == "goalDetector"){
goalDetector.run(imgHead, imgHead, &visionData); //roda a classe e atualiza a visionData
returnImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
//Funciona
if(functionSelected == "lineDetector"){
lineDetector.run(imgHead, imgHead, &visionData); //roda a classe e atualiza a visionData
returnImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
>>>>>>> temp
}
}
else if(!isWebcam){
//Se estiver vindo do robo, tem 2 cameras
if(functionSelected == "ballCandidate"){
emit sendFrame();
return;
}
if(functionSelected == "ballDetector"){
ballDetector.run(imgBody, imgHead, &visionData);
cvMatImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
<<<<<<< HEAD
//Funciona
if(functionSelected == "ellipseDetector"){
ellipseDetector.run(imgHead, imgHead, &visionData);//roda a classe e atualiza a visionData
cvMatImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
//Funciona
if(functionSelected == "fieldDetector"){
fieldDetector.run(imgHead, imgBody, &visionData);//roda a classe e atualiza a visionData
cvMatImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
=======
//Funciona
if(functionSelected == "yellowDetector"){
yellowDetector.run(imgHead, imgHead, &visionData); //roda a classe e atualiza a visionData
returnImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
>>>>>>> temp
//Funciona
if(functionSelected == "goalDetector"){
goalDetector.run(imgHead, imgHead, &visionData); //roda a classe e atualiza a visionData
cvMatImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
//Funciona
if(functionSelected == "lineDetector"){
lineDetector.run(imgHead, imgHead, &visionData); //roda a classe e atualiza a visionData
cvMatImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
//Funciona
if(functionSelected == "yellowDetector"){
yellowDetector.run(imgHead, imgHead, &visionData); //roda a classe e atualiza a visionData
cvMatImg = this->vectorSelectionInterface(vectorSelection, &visionData, &functionName);
}
}
int w = imgHead.cols;
int h = imgHead.rows;
QImage qImg((uchar*)returnImg.data, returnImg.cols, returnImg.rows, returnImg.step, QImage::Format_RGB888);
QRgb pixel;
//dstreturnImg.size() = imgHead.size();
//resize(returnImg, returnImg, imgHead.size(), 0, 0, INTER_AREA);
/*for(int i=0;i<w;i++)
{
for(int j=0;j<h;j++)
{
int gray = (int)returnImg.at<unsigned char>(j, i);
pixel = qRgb(gray,gray,gray);
qImg.setPixel(i,j,pixel);
}
}*/
imgContainer.image = qImg.rgbSwapped();;
imagePipe.save(imgContainer);
emit sendFrame();
}
}
void VideoThread::changeCamera(const int &camera)
{
if(isWebcam)
{
return;
}
if(!isConnected)
{
QString message;
message = "Not Connected.";
emit statusMessage(message);
return;
}
if(isRecording)
{
QString message;
message = "Cannot change camera while recording a video.";
emit statusMessage(message);
return;
}
cout << "change camera" << endl;
if(camera)
cameraID = "bot";
else
cameraID = "top";
QMutex mutex;
mutex.lock();
camProxy->setParam(kCameraSelectID,camera); //CHANGE TO THE BOTTOM CAMERA (0 - TOP, 1 - BOTTOM)
record->setCameraID(camera);
mutex.unlock();
}
void VideoThread::stopThread()
{
cout << "stoping thread" << endl;
if(!isConnected)
{
return;
}
if(!isWebcam)
{
camProxy->unsubscribe(clientName);
}
else
{
delete(cap);
}
emit terminateThread();
}
void VideoThread::startRecording(QString fileName)
{
if(isWebcam)
{
return;
}
if(!isConnected)
{
QString message;
message = "Not Connected.";
emit statusMessage(message);
return;
}
string file = fileName.toUtf8().constData();
file = file + ".avi";
cout << file << endl;
record->startRecording("/home/nao", file);
copyExec = "scp nao@" + strIP + ":/home/nao/" + file + " ./../recordings \n ssh nao@" + strIP + " 'rm /home/nao/" + file + "'";
cout << copyExec << endl;
system("mkdir -p ../recordings");
isRecording = true;
}
void VideoThread::stopRecording()
{
if(isWebcam)
{
return;
}
if(!isRecording)
{
QString message;
message = "Not recording. Fail to save video.";
emit statusMessage(message);
return;
}
record->stopRecording();
if(system(copyExec.c_str()) == 0)
{
QString message;
message = "File saved successfully.";
emit statusMessage(message);
}
else
{
QString message;
message = "Fail to transfer file.";
emit statusMessage(message);
}
isRecording = false;
}
void VideoThread::savePicture()
{
cout << "test!" << endl;
if(!isConnected)
{
QString message;
message = "Not Connected.";
emit statusMessage(message);
return;
}
if(firstScreenshot)
{
system("mkdir -p ../screenshots");
firstScreenshot = false;
}
QDateTime current = QDateTime::currentDateTime();
uint unixtime = current.toTime_t();
QDateTime timestamp;
timestamp.setTime_t(unixtime);
QString tmpString = timestamp.toString("yyyy-MM-dd_hh:mm:ss");
string str_timestamp = tmpString.toUtf8().constData();
string pictureFileName;
pictureFileName = "../screenshots/camera_" + cameraID + "_screenshot_" + str_timestamp + ".jpg";
cout << "Screenshot!" << endl;
Mat imgcv(cv::Size(640, 480), CV_8UC3);
if(isWebcam)
{
(*cap) >> imgcv;
}
else
{
ALValue img = camProxy->getImageRemote(clientName);
imgcv.data = (uchar*) img[6].GetBinary();
}
imwrite(pictureFileName, imgcv);
if(!isWebcam)
{
camProxy->releaseImage(clientName);
}
}
void VideoThread::functionChanged(QString functionToChange, QComboBox* vectorSelection){
functionSelected = functionToChange;
this->vectorSelection = vectorSelection;
this->vectorSelection->clear();
}