How to read image one by one from folder and make prediction save it in CSV format

4 views (last 30 days)
hammad younas
hammad younas on 30 Nov 2021
Commented: hammad younas on 1 Dec 2021
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);

Answers (2)

Hiro
Hiro on 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.
  1 Comment
hammad younas
hammad younas on 30 Nov 2021
@Hiro i am using the following code it work but it read ony png extenstion i want to read all files in folder.
and process one image after 1 second
path_directory='C:\Users\ASUS\Documents\MATLAB\Examples\R2021b\phased\ModClassificationOfRadarAndCommSignalsExample\Dataset\Dataset'; % 'Folder name'
original_files=dir([path_directory '/*.png']);
% original_files=dir( fullfile(path_directory ,['*' ext]) );
for k=1:length(original_files)
filename=[path_directory '/' original_files(k).name];
I=imread(filename);
% 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)
% ----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);
end

Sign in to comment.


Walter Roberson
Walter Roberson on 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 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by