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
Hiro 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
hammad younas
hammad younas 2021년 12월 1일
bascially the code save the signal in image form. and trained the squeezenet model on it

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by