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

4 次查看(过去 30 天)
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);

回答(2 个)

Hiro Yoshino
Hiro Yoshino 2021-11-30
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 个评论
hammad younas
hammad younas 2021-11-30
@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

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2021-11-30
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 个评论
Walter Roberson
Walter Roberson 2021-11-30
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;

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by