-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualization.m
274 lines (207 loc) · 10.8 KB
/
Visualization.m
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
function Visualization(dataDir,filename)
%%% IMPORTANT NOTE:
% For the TackLabels_PHOW.dat File, you need to remove the first
% line "ID,LABELS" in the dat file manually before using it here.
%%%
video = strcat(dataDir, '\', filename, '.avi' );
dataFile = strcat(dataDir, '\', filename, ' - TackLabels_PHOW.dat');
tracksFile = strcat(dataDir,'\', filename, ' - tracks.txt');
videoFileReader = vision.VideoFileReader(video);
%VARIABLE: tracksList -> matrix extracted from tracks text file
tracksList = load(tracksFile);
fid = fopen(dataFile);
trackData = textscan(fid, '%d%s', 'delimiter', ',');
fclose(fid);
%fid= fopen(tracksFile);
%==========================================================%
% Displaying Box on Video %
%==========================================================%
%b=dlmread(tracksFile,' ',[0 1 iterator 5]);
%disp(b);
videoInfo = info(videoFileReader);
videoPlayer = vision.VideoPlayer('Position',[400 0 videoInfo.VideoSize+30]);
%VARIABLE: iterator-> a helper variable in counting video frames
iterator = 0;
lineArrayTracker = 1;
%Cell matrix for lines of tracks
IDCount = 0;
maximumCountID = max(tracksList); % get the maximum for each column
maximumCountID = maximumCountID(:,1); % Column is 6, row is the highest no. of ID for cell size.
sizeOfArray = maximumCountID; % an upper limit for the array size.
arrayOfLine = zeros(sizeOfArray,2); % Declare outside loop because this must retain info for few frames.
counterForDiffObj = size(tracksList); % Getting the total number of lines in the textfile
counterForDiffObj = counterForDiffObj(1,1);
arrayOfDiffObj = zeros(maximumCountID,1);
arrayCount = 1;
% The loop below is to help in creating cell rows.
for i=1:counterForDiffObj
if(i==1)
previousPointer = tracksList(1,1);
IDCount = IDCount + 1;
end
if(i>1)
currentPointer = tracksList(i,1);
if(currentPointer == previousPointer)
IDCount = IDCount + 1;
else
if(IDCount == 0)
IDCount = IDCount + 1;
elseif(arrayCount ~= 1)
IDCount = IDCount + 1; % Correction to line 50 Counting-Cow's problem
end
arrayOfDiffObj(arrayCount,1) = IDCount;
arrayCount = arrayCount + 1;
IDCount = 0;
end
previousPointer = currentPointer;
end
if(counterForDiffObj == i)
IDCount = IDCount + 1;
arrayOfDiffObj(arrayCount,1) = IDCount;
end
end
matrixForLine = mat2cell(tracksList(:,[3 4 5 6]),arrayOfDiffObj,[4]);
% initialize Color cell array
matrixCellColor = cell(maximumCountID,1);
for a=1 : maximumCountID
initialColor = {'white'};
matrixCellColor(a,1) = initialColor;
end
% initialize singleRowMatrixCell
singleRowMatrixCell = cell(maximumCountID,1);
for a=1:maximumCountID
tempRow = [0 0 0 0];
tempRow = mat2cell(tempRow,1,4);
singleRowMatrixCell(a,:) = tempRow;
end
% initialize currentObject
currentObjectCell = cell(maximumCountID,1);
for a=1:maximumCountID
initialCurrentObject = {'Cars'};
currentObjectCell(a,1) = initialCurrentObject;
currentObjectCellConcurrent = currentObjectCell;
end
% initialize deleteLineArray
deleteLineArray = arrayOfDiffObj;
while ~isDone(videoFileReader)
% Extract next frame
videoFrame = step(videoFileReader);
% STEP 1 %
% Find the object in the specific Frame %
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
%VARIABLE: boundObjects -> determine the existence of object in a
% specifc frame. Output is a matrix. of detected Frames.
boundObjects = tracksList(tracksList(:,2)==iterator,:);
% STEP 2 %
% Extract Position of Bounding Box %
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
%VARIABLE: flag -> return the total number of records inside
% boundObjects.
%VARIABLE: multipleObjectFlag -> helps to draw multiple
% bounding box appearing in same frame.
%VARIABLE: rowInBoundObjects -> return the rows in boundObjects
% matrix
flag = size(boundObjects,1);
positionOfBox = [0 0 0 0];
% Determining Objects
objectType = trackData(:,2);
objectID = trackData(:,1);
objectID = cell2mat(objectID);
objectType = objectType{1};
flagLabel = size(objectID,1); % to calculate array's size of objectid
% Multiple Object Bounding
arrayOfPosition = zeros(sizeOfArray,4); %initialize 100 empty arrays
for rowInBoundObjects = 1:flag
trackID = boundObjects(rowInBoundObjects,1);
trackInFrame = boundObjects(rowInBoundObjects,2); % is here to do checking for concurrency.
x = boundObjects(rowInBoundObjects,3);
y = boundObjects(rowInBoundObjects,4);
width = boundObjects(rowInBoundObjects,5);
height = boundObjects(rowInBoundObjects,6);
positionOfBox = [x y width height];
arrayOfPosition(rowInBoundObjects,:) = positionOfBox(1,:); % Putting the position into the empty array
% Determining the object Type( For Bounding Box Titie)
for currentObjectID = 1:flagLabel
if(trackID == currentObjectID)
currentObjectCell(trackID,1)= objectType(currentObjectID); % used in line color operation
currentObjectCellConcurrent(rowInBoundObjects,1) = objectType(currentObjectID); % to be inserted into insertObjectAnnotation
end
end
% Operation to find draw line of tracks
matrixForLineDouble = matrixForLine{boundObjects(rowInBoundObjects,1),:}; % to select the row to be processed.
% Determine the middle point
middlePointX = matrixForLineDouble(:,1) + matrixForLineDouble(:,3)/2;
middlePointY = matrixForLineDouble(:,2) + matrixForLineDouble(:,4)/2;
matrixForLineDouble(:,1) = middlePointX;
matrixForLineDouble(:,2) = middlePointY;
% Middle point part ends
matrixForLineDoubleXY = matrixForLineDouble(:,[1 2]); % to select x and y pos only
A = (size(matrixForLineDoubleXY));
A = A(1,1);
singleRowMatrix = zeros(A*2);
singleRowMatrix = singleRowMatrix(1,:);
for w=1:flag
% Putting the matrix into one single row
counterForYPos = 0;
counterForRow = 1;
for i=1:A*2
if(mod(i,2)==0)
singleRowMatrix(1,i) = matrixForLineDoubleXY(counterForYPos,2);
counterForRow = counterForRow + 1;
else
singleRowMatrix(1,i) = matrixForLineDoubleXY(counterForRow,1);
counterForYPos = counterForYPos + 1;
end
end
if(A==1)
singleRowMatrix = [0 0 0 0];
end
end
if(A>1)
singleRowMatrixConverted = mat2cell(singleRowMatrix,1,A*2);
singleRowMatrixCell(trackID,:) = singleRowMatrixConverted;
end
% end of operation
% Determining Color for each object
currentObjectHelper = char(currentObjectCell{trackID,1});
lineColor = {'white'};
if(strcmp(currentObjectHelper,'Cars'))
lineColor = {'blue'};
elseif(strcmp(currentObjectHelper,'Humans'))
lineColor = {'yellow'};
elseif(strcmp(currentObjectHelper,'GOP'))
lineColor = {'red'};
elseif(strcmp(currentObjectHelper,'Bicycle'))
lineColor = {'green'};
else
lineColor = {'black'};
end
matrixCellColor(trackID,1) = lineColor;
% object Color ends
% Operation to determine whether the line still will be
% used or not
deleteLineArray(trackID,1) = deleteLineArray(trackID,1) - 1;
if(deleteLineArray(trackID,1)==0)
singleRowMatrixCell(trackID,1) = tempRow;
end
% Operation ends
tracksLine = insertShape(videoFrame-videoFrame,'line',singleRowMatrixCell,'color',matrixCellColor);
videoOut = insertObjectAnnotation(videoFrame,'rectangle',arrayOfPosition,currentObjectCellConcurrent);
if(rowInBoundObjects==flag) % This Line is to avoid iterator moving ahead of videoframes when multiple objects bounded.
step(videoPlayer,videoOut+tracksLine);
iterator = iterator + 1;
end
end
% If no frames detected
if(flag==0)
% tracksLine = insertShape(videoFrame-videoFrame,'line',[0 0 0 0],'color','yellow');
videoOut = insertObjectAnnotation(videoFrame,'rectangle',arrayOfPosition,'object');
step(videoPlayer,videoOut);
iterator = iterator + 1;
end
% Print frame number
% videoOut = insertText(videoOut,[3 3],iterator,'AnchorPoint','LeftTop');
pause(0.15);
clear arrayOfPosition; % Clear the array after each frame
end
end