-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path012_people_following.cpp
executable file
·274 lines (227 loc) · 7.82 KB
/
012_people_following.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
/*
CODE SAMPLE # 011: Object Tracking
This code will grab the basic stereo panoramas (left and right images) and ALSO the Disparity panorama, execute tracking for objects on it and then display in an opencv window
>>>>>> Compile this code using the following command....
g++ 011_object_tracking.cpp /usr/src/tensorrt/bin/common/logger.o ../lib/libPAL.so ../lib/libPAL_CAMERA.so ../lib/libPAL_DEPTH_HQ.so ../lib/libPAL_DEPTH_128.so ../lib/libPAL_DE.so libPAL_Track.so `pkg-config --libs --cflags opencv` -g -o 011_object_tracking.out -I../include/ -I/usr/local/include/eigen3 -lv4l2 -lpthread -lcudart -L/usr/local/cuda/lib64 -lnvinfer -lnvvpi -lnvparsers -lnvinfer_plugin -lnvonnxparser -lmyelin -lnvrtc -lcudart -lcublas -lcudnn -lrt -ldl
>>>>>> Execute the binary file by typing the following command...
./011_object_tracking.out
>>>>>> KEYBOARD CONTROLS:
ESC key closes the window
Press v/V key to toggle the vertical flip of panorama
Press f/F to toggle filter rgb property.
Press d/D to toggle fast depth property
Press r/R to toggle near range property
*/
# include <stdio.h>
# include <opencv2/opencv.hpp>
# include <chrono>
# include <bits/stdc++.h>
# include "PAL.h"
using namespace cv;
using namespace std;
using namespace std::chrono;
namespace PAL
{
int RunTrack(cv::Mat& img, cv::Mat& depth, vector<vector<float>> &boxes,
vector<int> &ids, vector<float> &depthValues, vector<Scalar> &colours);
}
string getCmdOutput(string cmd)
{
string outputString;
FILE *outpStream;
const int MaxLineLen = 128;
char outpLine[MaxLineLen];
outpStream = popen(cmd.c_str(), "r");
if (outpStream) {
while (!feof(outpStream)) {
if (fgets(outpLine, MaxLineLen, outpStream) != NULL) {
outputString += outpLine;
}
}
pclose(outpStream);
}
return outputString;
}
int main(int argc, char *argv[])
{
namedWindow("Pal Object Tracking", WINDOW_NORMAL); // Create a window for display.
int width, height;
if (PAL::Init(width, height, -1) != PAL::SUCCESS) //Connect to the PAL camera
{
printf("Init failed\n");
return 1;
}
PAL::CameraProperties data;
PAL::Acknowledgement ack = PAL::LoadProperties("../Explorer/SavedPalProperties.txt", &data);
if(ack != PAL::SUCCESS)
{
printf("Error Loading settings\n");
}
PAL::CameraProperties prop;
unsigned int flag = PAL::MODE;
flag = flag | PAL::FD;
flag = flag | PAL::NR;
flag = flag | PAL::FILTER_SPOTS;
flag = flag | PAL::VERTICAL_FLIP;
flag = flag | PAL::CAMERA_HEIGHT;
prop.mode = PAL::Mode::TRACKING;
prop.fd = 1;
prop.nr = 0;
prop.filter_spots = 1;
prop.vertical_flip =0;
prop.camera_height = 60;
PAL::SetCameraProperties(&prop, &flag);
bool isDisparityNormalized = true;
bool followFlag = true, followFound = false;
int followID = 0;
int ftop,fleft,fwidth,fheight;
int prevPad = 0;
//width and height are the dimensions of each panorama.
//Each of the panoramas are displayed at quarter their original resolution.
//Since the left+right+disparity are vertically stacked, the window height should be thrice the quarter-height
resizeWindow("Pal Object Tracking", width / 4, (height / 4) * 3);
int key = ' ';
printf("Press ESC to close the window\n");
printf("Press v/V key to toggle the vertical flip of panorama\n");
printf("Press f/F to toggle filter rgb property.\n");
printf("Press d/D to toggle fast depth property\n");
printf("Press r/R to toggle near range property\n");
size_t currentResolution = 0;
bool flip = false;
bool filter_spots = true;
bool nr = false;
bool fd = true;
vector<vector<float>> boxes;
vector<int> ids;
vector<float> depthValues;
vector<Scalar> colours;
int num;
//27 = esc key. Run the loop until the ESC key is pressed
while (key != 27)
{
PAL::Image left, right, depth, disparity;
//PAL::GrabFrames(&left, &right, &depth);
PAL::GrabFrames(&left, &right);
//Convert PAL::Image to Mat
Mat img0 = Mat(left.rows, left.cols, CV_8UC3, left.Raw.u8_data);
Mat img;// = img0.clone();
if(followFlag && followFound)
{
if(prevPad-544<0)
{
int leftlen = prevPad-544;
int rightlen = prevPad+544;
//cout<<"Pan-Left::"<<leftlen<<"::"<<rightlen<<endl;
cv::Rect lroi(1120+leftlen,0,abs(leftlen), 384);
cv::Rect rroi(0,0,rightlen,384);
hconcat(img0(lroi),img0(rroi),img);
}
else if(prevPad+544>1120)
{
int leftlen = 1120-(prevPad-544);
int rightlen = (prevPad+544)-1120;
//cout<<"Pan-Right::"<<leftlen<<"::"<<rightlen<<endl;
cv::Rect lroi(1120-leftlen,0,abs(leftlen), 384);
cv::Rect rroi(0,0,rightlen,384);
hconcat(img0(lroi),img0(rroi),img);
}
else
{
int leftStart = prevPad-544;
int rightEnd = prevPad + 544;
//cout<<"Within-Limits::"<<leftStart<<"::"<<rightEnd<<endl;
cv::Rect roi(leftStart, 0, 1088, 384);
img = img0(roi);
}
}
else
{
cv::Rect roi(16, 0, 1088, 384);
img = img0(roi);
prevPad = 560;
}
//Mat d = Mat(depth.rows, depth.cols, CV_32FC1, depth.Raw.f32_data);
Mat d = cv::Mat::zeros(cv::Size(1, 1), CV_32FC1);
num = PAL::RunTrack(img, d, boxes, ids, depthValues, colours);
for (int i = 0; i < boxes.size(); i++)
{
//putText(img, format("ID=%d , Depth=%.2fm", ids[i], depthValues[i]/100), Point(boxes[i][0], boxes[i][1]+25+boxes[i][3]),
// 0, 0.6, Scalar(0, 0, 255), 1, LINE_AA);
putText(img, format("ID=%d", ids[i]), Point(boxes[i][0], boxes[i][1]+25+boxes[i][3]), 0, 0.6, Scalar(0, 0, 255), 1, LINE_AA);
//putText(img, format("ID=%d , Depth=%.2fm", ids[i], depthValues[i]/100), Point(boxes[i][0], boxes[i][1] - 5),
//0, 0.6, Scalar(0, 0, 255), 1, LINE_AA);
rectangle(img, Rect(boxes[i][0], boxes[i][1], boxes[i][2], boxes[i][3]), colours[i], 2);
}
putText(img, format("num: %d", num), Point(0, 30), 0, 0.6, Scalar(0, 0, 255), 2, LINE_AA);
if(followFlag)
{
for (int i=0; i<boxes.size(); i++)
{
if(ids[i]==followID)
{
fleft=boxes[i][0];
ftop=boxes[i][1];
fwidth=boxes[i][2];
fheight=boxes[i][3];
//cout<<"T:"<<ftop<<"L:"<<fleft<<"W:"<<fwidth<<"H:"<<fheight<<endl;
followFound=true;
//cout<<"PrevPad::"<<prevPad<<endl;
prevPad = prevPad - 544 + fleft + fwidth/2;
prevPad = prevPad%1120;
//cout<<"PrevPad::"<<prevPad<<endl;
break;
}
}
}
//Display the final output image
imshow("Pal Object Tracking", img);
boxes.clear();
ids.clear();
depthValues.clear();
colours.clear();
//Wait for the keypress - with a timeout of 1 ms
key = waitKey(1) & 255;
if (key == 'i' || key == 'I')
{
string zenityCmd = "zenity --entry --text \"Enter ID to Track\" --title \"PAL People Following\" --entry-text=\"\"";
string output = getCmdOutput(zenityCmd);
followID = stoi(output);
}
if (key == 'v' || key == 'V')
{
PAL::CameraProperties prop;
flip = !flip;
prop.vertical_flip = flip;
unsigned int flags = PAL::VERTICAL_FLIP;
PAL::SetCameraProperties(&prop, &flags);
}
if (key == 'f' || key == 'F')
{
PAL::CameraProperties prop;
filter_spots = !filter_spots;
prop.filter_spots = filter_spots;
unsigned int flags = PAL::FILTER_SPOTS;
PAL::SetCameraProperties(&prop, &flags);
}
if(key == 'd' || key == 'D')
{
PAL::CameraProperties prop;
fd = !fd;
prop.fd = fd;
unsigned int flags = PAL::FD;
PAL::SetCameraProperties(&prop, &flags);
}
if(key == 'r' || key == 'R')
{
PAL::CameraProperties prop;
nr = !nr;
prop.nr = nr;
unsigned int flags = PAL::NR;
PAL::SetCameraProperties(&prop, &flags);
}
}
printf("exiting the application\n");
//CloseTrack();
PAL::Destroy();
return 0;
}