Hi, I am trying to put a box around the car and have it's confidence level placed on top of the box, how can I do that?

1 view (last 30 days)
I have tried insertObjectAnnotation but i am having error. Here are my codes :
clear
net = googlenet; % Load the neural net
net.Layers
% while true
labelType =''
[file,path]=uigetfile({'*.jpg;*.jpeg;*.bmp;*.png;*.tif'},'Choose an image');
s=[path,file];
I=imread(s);
% I = imread('car1.jpeg');
sz = net.Layers(1).InputSize ;
inputSize = net.Layers(1).InputSize
size(I)
I = imresize(I,inputSize(1:2));
figure
imshow(I)
[label, score] = classify(net, I); % Classify the picture
if (label == 'convertible')
labelType = 'Vehicle Recognised'
elseif (label == 'moped')
labelType = 'Vehicle Recognised'
elseif (label == 'tank')
labelType = 'Vehicle Recognised'
elseif (label == 'sports car')
labelType = 'Vehicle Recognised'
elseif (label == 'moving van')
labelType = 'Vehicle Recognised'
elseif (label == 'trailer truck')
labelType = 'Vehicle Recognised'
elseif (label == 'garbage truck')
labelType = 'Vehicle Recognised'
elseif (label == 'trailer truck')
labelType = 'Vehicle Recognised'
elseif (label == 'minivan')
labelType = 'Vehicle Recognised'
elseif (label == 'beach wagon')
labelType = 'Vehicle Recognised'
elseif (label == 'ambulance')
labelType = 'Vehicle Recognised'
elseif (label =='cab')
labelType = 'Vehicle recognised';
else
labelType = 'No Vehicle Recognised'
end
position = [23 373 60 66;35 185 77 81;77 107 59 26];
I = insertObjectAnnotation(I, 'rectangle', label, score);
imshow(I); % Show the picture
message = strcat(labelType,'-', char(label))
title({char(message),num2str(mac(score),3)}); % Show the label
I=imread(s);
BW = im2bw(I,0.2);
se = strel('rectangle', [2 20]);
BW_opened = imclose(BW,se);
figure, imshow(BW_opened,[])
s=regionprops(BW_opened,'Area','BoundingBox');
[hh,ii] = sort([s.Area],'descend');
out = imcrop(I,s(ii(2)).BoundingBox);
figure,imshow(out);
out_path = 'C:\Users\Student\Desktop\Vehicle number plate recognition\test images'; % Give path here
fullFileName = fullfile(out_path, 'test_image.jpg');
imwrite(out, fullFileName);
load imgfildata;
picture=imread(fullFileName);
[~,cc]=size(picture);
picture=imresize(picture,[300 500]);
if size(picture,3)==3
picture=rgb2gray(picture);
end
se=strel('rectangle',[5,5]);
a=imerode(picture,se);
figure,imshow(a);
b=imdilate(a,se);
threshold = graythresh(picture);
picture =~im2bw(picture,threshold);
percentageBlack = (1-nnz(picture)/numel(picture))*100
picture = bwareaopen(picture,50);
if (percentageBlack < 50)
picture = imcomplement(picture);
end
[L,Ne]=bwlabel(picture);
propied=regionprops(L,'BoundingBox');
hold on
pause(1)
for n=1:size(propied,1)
rectangle('Position',propied(n).BoundingBox,'EdgeColor','g','LineWidth',1)
end
hold off
figure
final_output=[];
t=[];
for n=1:Ne
[r,c] = find(L==n);
n1=picture(min(r):max(r),min(c):max(c));
n1=imresize(n1,[42,24]);
imshow(n1)
pause(0.2)
x=[ ];
totalLetters=size(imgfile,2);
for k=1:totalLetters
y=corr2(imgfile{1,k},n1);
x=[x y];
end
t=[t max(x)];
if max(x)>.45
z=find(x==max(x));
out=cell2mat(imgfile(2,z));
final_output=[final_output out];
end
end
file = fopen('number_Plate.txt', 'wt');
fprintf(file,'%s\n',final_output);
fclose(file);
winopen('number_Plate.txt')
drawnow;
%end
The error that i get is : | _Error using insertObjectAnnotation Expected input number 3, POSITION, to be one of these types:
numeric
Instead its type was categorical.
Error in insertObjectAnnotation
Error in insertObjectAnnotation
Error in GoogleNet (line 49) I = insertObjectAnnotation(I, 'rectangle', label, score);
>>_|

Answers (1)

Florian Morsch
Florian Morsch on 3 Aug 2018
Error using insertObjectAnnotation Expected input number 3, POSITION, to be one of these types:
numeric
Instead its type was categorical.
I = insertObjectAnnotation(I, 'rectangle', label, score);
Says all you need to know. From https://de.mathworks.com/help/vision/ref/insertobjectannotation.html you can see, that after 'rectangle' you need to specify where the rectangle should be. You are using your label as position, which cant work. It should look like this:
I = insertObjectAnnotation(I, 'rectangle', PositionWhereRectangleShouldBe, label, score);
From the link i posted above you can read that your position should look like this [x y width height], so something like
PositionWhereRectangleShouldBe = [100 100 50 50];
would insert a rectangle at the position X100 Y100 with width and height of 50 pixel. You already have a position-variable
position = [23 373 60 66;35 185 77 81;77 107 59 26];
directly above your insertObjectAnnotation, but you didnt use it.
  3 Comments
Florian Morsch
Florian Morsch on 6 Aug 2018
Again the error code says all you need. It expects "label" to be a cell , but the data type you are giving is categorical. As it seems you didnt bother to read the documentation in the link i gave, there is a example how the different input arguments should look like. Correct your data types for the input arguments, then the error should disappear.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!