Open AnnekaPetersen opened 3 years ago
Anneka @AnnekaPetersen a question: this pedestrians-only, is this for the general solution case, or is this one of many particular solution cases?
I presume this is one particular solution case, and that later, you might want to do the same for "Biker", or ("Car and Bus").
We could do this with Loop Control Statements!
TL;DR: Within a for
loop, the break
command terminates the loop, and the continue
command goes back to the top of the loop, continuing the loop with the next element.
So imagine we have a text variable named filter
. This variable could be an empty string ""
(meaning no filter) OR we could put "Pedestrian"
, or we could put "Car,Bus"
where the comma ,
is some arbitrary delimiter.
Helpful to know: In MatLab,
strlength("some string")
returns the length of "some string".strfind("Some long string", "long")
returns a vector of the occurrences of "long" in the long string that's searched.So our code would be
% filter could be an empty string
filter = "Pedestrian";
for k=1:length(S{1})
cell = S(1);
annotation = cell{1}(k);
annotation= char(annotation);
split = textscan(annotation, '%f%f%f%f%f%f%f%f%f%s');
id = split{1};
xmin = split{2};
ymin = split{3};
xmax = split{4};
ymax = split{5};
class = split{10};
if strlength(filter) > 0
% We have a filter!
% strfind() returns a vector of spots where the string is found.
% the vector will be length 0 if not found
if length(strfind(filter, class) = 0
% we don't match the filter so bail-out and don't process this line
continue
% our big `switch` statement goes here
end
I hope this makes sense 😄
Yes you assumed right, it is for one of many particular solution cases. It does make some sense to me and I will try this out, thank you!
Yeah @AnnekaPetersen the keys to the city here:
break
blows up the whole for
loopcontinue
blows up just the current iteration of the for
loop; back to the top with the next iteration on-deck.So here's the code, it's had some adjustments, and I tried adding the filter and if statements.
annotationfile = fopen('Bookstore Annotations Short.txt','r');
S = textscan(annotationfile,'%s','delimiter','\n');
fid=fopen('Bookstore Annotations Short.txt');
tline = fgetl(fid);
tlines = cell(1,1);
while ischar(tline)
tlines{end+1,1} = tline;
tline = fgetl(fid);
end
fclose(fid);
I=imread('Bookstore Reference1.jpg');
figure,imshow(I)
hold on
filter =();
for k=2:size(tlines)
cell = tlines{k,1};
annotation= char(cell);
split = textscan(annotation, '%f%f%f%f%f%f%f%f%f%s');
id = split{1};
xmin = split{2};
ymin = split{3};
xmax = split{4};
ymax = split{5};
display(split);
fprintf("displaying the box cordinates\n")
x = (xmin +((xmax-xmin)/2));
y = (ymin +((ymax-ymin)/2));
fprintf("%d\n",x);
fprintf("%d\n",y);
plot(x,y,'or','MarkerSize',2,'LineWidth',2);
class = split{10}{:};
fprintf("%s\n",class);
if strlength(filter) > 0
strfind(filter,class)
if length(strfind(filter,class))==0
end
end
switch class
case '"Biker"'
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','b','LineWidth',2);
case '"Pedestrian"'
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','r','LineWidth',2);
case '"Skater"'
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','m','LineWidth',2);
case '"Car"'
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','g','LineWidth',2);
case '"Cart"'
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','c','LineWidth',2);
case '"Bus"'
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','y','LineWidth',2)
otherwise
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','k','LineWidth',2)
end
end
However, all the trajectory line of the different types of traffic are still showing up. I have the pedestrians in red and bikers in blue, so on the picture I have red and blue lines showing up. The command window shows:
split =
1×10 cell array
Columns 1 through 9
{[12]} {[695]} {[274]} {[722]} {[316]} {[2531]} {[0]} {[0]} {[0]}
Column 10
{1×1 cell}
displaying the box cordinates
7.085000e+02
295
"Pedestrian"
ans =
[]
@AnnekaPetersen is this line a correct string assignment?
filter =();
Looks suspect. I would expect the following:
filter ="";
@AnnekaPetersen also I see a few things missing here:
continue
statement 😄 if strlength(filter) > 0
strfind(filter,class)
if length(strfind(filter,class))==0
end
end
@StevenBlack Yes, you are right. That was something I was playing around with, instead I have actually been using filter = "Pedestrian";
When using break
and continue
in this case, where should they be placed?
@AnnekaPetersen right where you want to blow the loop, or the current iteration.
if strlength(filter) > 0
if length(strfind(filter,class))==0
continue;
end
end
@AnnekaPetersen check the syntax for equaliy testing; I pulled the ==
out of thin air based on other languages I know.
@StevenBlack ==
is the correct way. Now with using continue;
all trajectories still show up but as the colour red.
@AnnekaPetersen Well the red makes sense because red is "Pedestrian".
The data file has over 361,000 records. There has to be tens of thousands, or hundreds of thousands, of "pedestrian" records, right?
Try this: make a much smaller data file, say under 50 records, and run that, and assess what you see based on a very limited and controlled dataset.
Hang in there, these are tricks everybody learns with time. In this case, 360,000+ records is a bit savage...
@StevenBlack Sorry I should have phrased that better. I am currently using a smaller file with only 12 individuals that are either a biker or pedestrian. Now running the code, I see that both biker and pedestrian trajectories still appear but now bikers are also labelled as red trajectories.
@AnnekaPetersen can you post latest code?
annotationfile = fopen('Bookstore Annotations Short.txt','r');
S = textscan(annotationfile,'%s','delimiter','\n');
fid=fopen('Bookstore Annotations Short.txt');
tline = fgetl(fid);
tlines = cell(1,1);
while ischar(tline)
tlines{end+1,1} = tline;
tline = fgetl(fid);
end
fclose(fid);
I=imread('Bookstore Reference1.jpg');
figure,imshow(I)
hold on
filter ="Pedestrians";
for k=2:size(tlines)
cell = tlines{k,1};
annotation= char(cell);
split = textscan(annotation, '%f%f%f%f%f%f%f%f%f%s');
id = split{1};
xmin = split{2};
ymin = split{3};
xmax = split{4};
ymax = split{5};
display(split);
fprintf("displaying the box cordinates\n")
x = (xmin +((xmax-xmin)/2));
y = (ymin +((ymax-ymin)/2));
fprintf("%d\n",x);
fprintf("%d\n",y);
plot(x,y,'or','MarkerSize',2,'LineWidth',2);
class = split{10}{:};
fprintf("%s\n",class);
if strlength(filter) > 0
strfind(filter,class);
if length(strfind(filter,class))==0
continue;
end
end
switch class
case '"Pedestrian"'
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','r','LineWidth',2);
case '"Biker"'
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','b','LineWidth',2);
case '"Skater"'
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','m','LineWidth',2);
case '"Car"'
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','g','LineWidth',2);
case '"Cart"'
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','c','LineWidth',2);
case '"Bus"'
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','y','LineWidth',2)
otherwise
plot(x,y,'or','MarkerSize',2,'MarkerEdgeColor','k','LineWidth',2)
end
end
@AnnekaPetersen Quick observation, the strfind(filter,class);
doesn't do anything, it's never assigned to anything so this strikes me as better
if strlength(filter) > 0
if length(strfind(filter,class))==0
continue;
end
end
Now try inserting a debug message to screen just before bailing, like this maybe:
if strlength(filter) > 0
strfind(filter,class);
if length(strfind(filter,class))==0
fprintf("Bailing out!\n");
continue;
end
end
Also I notice this line needs a semicolon ;
fprintf("displaying the box cordinates\n")
Bailing out!
for each bike.@StevenBlack Made the changes! Now I do see Bailing out!
in the command window when I run the code, and the biker trajectories still showing up in red.
What about this?
Reading this, no ;
after continue
. https://www.mathworks.com/help/matlab/ref/continue.html?searchHighlight=continue&s_tid=srchtitle
if strlength(filter) > 0
strfind(filter,class);
if length(strfind(filter,class))==0
fprintf("Bailing out!\n");
continue
fprintf("I should NEVER see this!\n");
end
end
@StevenBlack So I do see Bailing out!
but not I should NEVER see this
which makes sense, but I am confused in this helps to make the Biker trajectories disappear.
@AnnekaPetersen well we should never get to the switch
statement that triggers the plot()
commands if we bail for all non-Pedestrian records.
Is "cache" a thing here? It it possible (somehow) that you're seeing old plot()
output?
@StevenBlack I do not know for sure I did try to clear it using clear Single_Class_Code.m
, but I am also confused where the code specifically bails on the plot()
command for non-Pedestrians.
@AnnekaPetersen we have a for
loop with a switch
statement at the bottom, within the loop. The switch
statement executes various plot()
statements depending on the class
.
Above the switch
statement we have a conditional continue
call. A continue
stops code processing and sends us back to the top of the loop, at the next item in the for
.
So the continue
command should short-circuit the for
loop and so the switch
statement should never fire if the continue
fires.
@StevenBlack Okay that makes sense.
@StevenBlack Is it possible that a string array isn't defined?
@AnnekaPetersen anything is possible 😄
Sometimes we just have to smash at the code repeatedly until it works as intended. Don't be shy to sprinkle fprintf("We are here\n");
statements in your code, also don't be shy to echo the values of class
and filter
to screen to confirm the code is following a happy path. Those debug instances are easy to remove later.
You could try running a data file with just bike records and filter="Pedestrian";
just to distill things to the absolute singular case.
My objective is to view only the trajectories of the pedestrians or one class of traffic. I have been looking at this part of the code:
I have tried using % to comment out the other cases and the otherwise statement, as well as deleting the cases but have not had any luck so far. The other trajectories still show up.