I am working on a student id recognition using CNN and i manage to recognize every single digit but how can i combine every digit into one single output.
i want the output to be
student id number: 164335
below is my code
%% feed image
myFolder = 'D:\CNN test\segmentedImages1';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.png');
theFiles = dir(filePattern);
for k = 2 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
I = imread(fullFileName);
subplot(3, 4, k);
imshow(I); % Display image.
drawnow; % Force display to update immediately.
label = classify(net,I);
title([' Recognized Digit is ' char(label)])
end
%% Displaying Detected Text
fprintf('%s\n',label)
but my code right now only printing 5

9 Kommentare

label = classify(net,I);
overwrites the variable label each time through the loop; you need to allocate space for each to be recognized/stored and index into that array.
filePattern = fullfile(myFolder, '*.png');
theFiles = dir(filePattern);
label=cellstr(numel(theFiles)); % allocate for the number expected
for k = 2 : length(theFiles)
...
label(k)=cellstr(classify(net,I)); % save each as a cellstr()
end
One Q? -- why the loop over k beginning with 2 instead of 1? This will miss the first file in the folder with the desired "*.png" file extension -- is that intended or an oversight? Files are returned in a sorted order by dir(), but it isn't documented that this is guaranteed behavior.
In this case, this approach of file name reading could be better:
filePattern = fullfile(myFolder, '*.png');
theFiles = dir(filePattern);
Fnames = {theFiles.name};
for k = 2 : length(Fnames)
...
label(k)=...
end
@dpb the k begin with 2 because i want to ignore the first file in the folder btw i will give your script a try.
@dpb @Sulaymon Eshkabilov i have try both of the script given but can i know why my output in the command promt become like this:
5 ( why does the number 5 is here when it should output number 1 )
1
6
4
3
3
5
and how can i give the ouput horizontally (164335) instead vertically (as shown above)
it give this ouput
student id: 5
student id: 1
student id: 6
student id: 4
student id: 3
student id: 3
student id: 5
instead of student id: 164335
Walter Roberson
Walter Roberson am 21 Jun. 2021
Files are returned in a sorted order by dir(),
For some definition of sorted.. the sort order can depend upon the language settings of the person who created the file system!
@Walter Roberson alright thank you for the advice
But right now after changing the script this error keep on showing Unrecognized function or variable 'net' when i try to run it on my GUI but work fine on my matlab script
can i know why?
when i search on matlab it stated that These errors usually indicate that MATLAB cannot find a particular variable or MATLAB program file in the current directory or on the search path.
but the file is in the same directory
This is my GUI script
% --- Executes on button press in Digit_recognition.
function Digit_recognition_Callback(hObject, eventdata, handles)
% hObject handle to Digit_recognition (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
myFolder = 'D:\CNN test\segmentedImages1';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.png');
theFiles = dir(filePattern);
for k = 2 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
I = imread(fullFileName);
%subplot(3, 4, k);
%imshow(I); % Display image.
drawnow; % Force display to update immediately.
%label(k) = classify(net,I);
label(k)=cellstr(classify(net,I));
title([' Recognized Digit is ' char(label(k))])
end
Perimeter = fprintf( 'student id: %s\n',label);
handles.text1.String = Perimeter;

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 21 Jun. 2021

0 Stimmen

but the file is in the same directory
Are you indicating that you have a file net.m in the same directory, which is a function that will return a Network suitable for use by classify() ?
but work fine on my matlab script
You loaded (or assigned to) a variable named net in your base workspace. The base workspace is not automatically searched when a function is invoked.
You can force the base workspace to be looked in... but when you shut down for the evening and come back tomorrow, the base workspace will have been cleared and you will be out of sorts.
You should be loading the net from a .mat file at the time that the GUI starts, and making it available within your GUI; see http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F

5 Kommentare

Are you indicating that you have a file net.m in the same directory, which is a function that will return a Network suitable for use by classify() ?
Below is the script that i have save in the same directory with the name of handrecogtrain.m and the net variables was succesfuly to be call in the different script. But in GUI it does not.
But for the GUI i have tried to save the script below as a .mat instead of .m and I load the .mat file to my GUI outside of the loop and now the error im getting from my GUI is
Error using classify (line 123)
Requires at least three arguments.
Error in GUI1st>Digit_recognition_Callback (line 206)
label(k)=cellstr(classify(net,I));
Can i know why?
%CNN program
digitDatasetPath='D:\CNN test\mnisdigitdataset';
digitimages=imageDatastore(digitDatasetPath,'IncludeSubfolders',true,'LabelSource','foldernames');
numTrainFiles=750; %numTrainFiles=0.75 (75%)
[TrainImages,TestImages]=splitEachLabel(digitimages,numTrainFiles,'randomize');
%Building CNN
layers=[
imageInputLayer([28 28 1],'Name','Input')
convolution2dLayer(3,8,'Padding','same','Name','Conv_1')
batchNormalizationLayer('Name','BN_1')
reluLayer('Name','Relu_1')
maxPooling2dLayer(2,'Stride',2,'Name','MaxPool_1')
convolution2dLayer(3,16,'Padding','same','Name','Conv_2')
batchNormalizationLayer('Name','BN_2')
reluLayer('Name','Relu_2')
maxPooling2dLayer(2,'Stride',2,'Name','MaxPool_2')
convolution2dLayer(3,32,'Padding','same','Name','Conv_3')
batchNormalizationLayer('Name','BN_3')
reluLayer('Name','Relu_3')
maxPooling2dLayer(2,'Stride',2,'Name','Maxpool_3')
convolution2dLayer(3,64,'Padding','same','Name','Conv_4')
batchNormalizationLayer('Name','BN_4')
reluLayer('Name','Relu_4')
fullyConnectedLayer(10,'Name','FC')
softmaxLayer('Name','Softmax');
classificationLayer('Name','Output Classification');
];
%Igraph = layerGraph(layers);
%plot(Igraph); %Plotting Network Structure
%-----------------------------------Training Options-----------------
options = trainingOptions('sgdm','InitialLearnRate',0.01,'MaxEpochs',4,'Shuffle','every-epoch','ValidationData',TestImages,'ValidationFrequency',30,'Verbose',false,'Plots','training-progress');
net = trainNetwork(TrainImages,layers,options); %Network Training
Ypred = classify(net,TestImages); %Recognizing Digits
YValidation = TestImages.Labels; %Getting Labels
accuracy = sum(Ypred == YValidation)/numel(YValidation); %Finding %age accuracy
Walter Roberson
Walter Roberson am 21 Jun. 2021
You should be save()'ing the net variable to a .mat file, and then in your Digit_recognition_OpenFcn in Digit_recognition.m you should be load()'ing the net variable and storing it for internal use as I described earlier.
Oh okay now i get it after reading the link you gave me.
Thank you so much Sir
Your help is much appreciated.
And now how can i output the iteration into static text in gui like this
student id: 164335
it seem that when i use this script to output my result in static text it shows the value 108 and not 164335 can i know why, because at my command promt it shows
Now reading D:\CNN test\segmentedImages1\image2.png
Now reading D:\CNN test\segmentedImages1\image3.png
Now reading D:\CNN test\segmentedImages1\image4.png
Now reading D:\CNN test\segmentedImages1\image5.png
Now reading D:\CNN test\segmentedImages1\image6.png
Now reading D:\CNN test\segmentedImages1\image7.png
student id: <undefined>
student id: 8
student id: 6
student id: 4
student id: 3
student id: 3
student id: 5
Perimeter = fprintf( 'student id: %s\n',label);
The output of fprintf is the number of items transfered, not the content of the string. Use sprintf() to get the content.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by