How to read image one by one from folder and make prediction save it in CSV format
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I want to read image automatically from folder one by one and pass it to trained model one by one after some delay
I am implement the following code using GUI (push button) but it take input image from user and print the prediction
I want to make it automatically take all images from folder and processed one by one
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global ImageFile filepath
[filename, filepath] = uigetfile({'*.*';'*.jpg';'*.png';'*.bmp'},'Select Image File');
fullname = [filepath filename];
% ----now read that image (fullname)
I = imread(fullname);
I = repmat(I,[1 1 3]);
% ----now display that image (fullname)
%resizePos = get(handles.axes1,'Position');
%ImageFile = imresize(ImageFile, [resizePos(3) resizePos(3)]);
axes(handles.axes1);
%imagesc(ImageFile);
imshow(I)
% ----clear axes scale
axis off
net = load('11ClassesResnet.mat')
net=net.trainednet
inputSize = net.Layers(1).InputSize;
I = imresize(I,inputSize(1:2));
%-----------------------Test-------------------
YPred_Test = classify(net,I)
n = string(YPred_Test(1));
set(handles.edit1,'string',n);
0 commentaires
Réponses (2)
Hiro Yoshino
le 30 Nov 2021
I would use "imageDatastore".
This data format is dedicated for that kind of problem.
Use is quite straight foward: 1) point the folder that contains your images 2) read(imds) return a pointer to an image one by one
You can also extract the path information from this type of variable.
Walter Roberson
le 30 Nov 2021
net = load('11ClassesResnet.mat')
net=net.trainednet
inputSize = net.Layers(1).InputSize;
path_directory='C:\Users\ASUS\Documents\MATLAB\Examples\R2021b\phased\ModClassificationOfRadarAndCommSignalsExample\Dataset\Dataset'; % 'Folder name'
original_files = dir(path_directory);
original_files([original_files.isdir]) = []; %remove . and .. and subfolders
% original_files=dir( fullfile(path_directory ,['*' ext]) );
for k=1:length(original_files)
thisfile = original_files(k).name;
filename = fullfile(original_files(k).folder, thisfile);
try
I = imread(filename);
catch ME
fprintf('File "%s" could not be read as an image\n', thisfile);
continue;
end
% Image read is done
%%Image Operation as per your work
% process x
% ----now read that image (fullname)
%I = imread(fullname);
I = repmat(I,[1 1 3]);
% ----now display that image (fullname)
%resizePos = get(handles.axes1,'Position');
%ImageFile = imresize(ImageFile, [resizePos(3) resizePos(3)]);
axes(handles.axes1);
%imagesc(ImageFile);
imshow(I)
title(thisfile)
t
% ----clear axes scale
axis off
I = imresize(I,inputSize(1:2));
%-----------------------Test-------------------
YPred_Test = classify(net,I)
n = string(YPred_Test(1));
set(handles.edit1,'string',n);
drawnow();
end
9 commentaires
Walter Roberson
le 30 Nov 2021
I do not seem to find a copy of 11ClassResNet.mat anywhere.
The directory name you give appears to be associated with the example https://www.mathworks.com/help/phased/ug/modulation-classification-of-radar-and-communication-waveforms-using-deep-learning.html which is an example about signals not about images so I am not clear as to what you are doing.
Ah... the line
Prediction = n;
should be
Prediction(k) = n;
Voir également
Catégories
En savoir plus sur Image Preview and Device Configuration dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!