I tried creating one more infinite loop outside the classification loop for that if isfile statement but still the code execution stops if the file is not detected.
Running a program in loop if file exists
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi all,
I am creating a program in MATLAB which applies an image classification algorithm on an image I give. The image is selected by LabVIEW software and placed in a folder. I have programmed MATLAB in such a way that it reads this exact image for prediction and after prediction, store the results in 2 text files. Now what I am trying to do is the code should run in infinite loop and if the image is deleted, the MATLAB should set the value in text files to blank i.e., "". But when I delete the image, the code execution stops with an error stating imread cannot find an image. Attaching the code here. I do not want the code execution to stop regardless of presence or absence of the image.
k=0;
while true
load('D:\modelpath\lymphnodenet.mat');
classNames = trainedNetwork_1.Layers(end).ClassNames;
if isfile('D:\imagepath\image.BMP')
I = imread('D:\imagepath\image.bmp');
I = imresize(I,[224,224]);
[label,scores] = classify(trainedNetwork_1,I);
subplot(1,2,1)
imshow(I)
title(string(label) + ", " + num2str(100*scores(classNames == label),3) + "%");
[~,idx] = sort(scores,'descend');
idx = idx(2:-1:1);
classNamesTop = trainedNetwork_1.Layers(end).ClassNames(idx);
scoresTop = scores(idx);
subplot(1,2,2)
barh(scoresTop)
xlim([0 1])
title('Top 5 Predictions')
xlabel('Probability')
yticklabels(classNamesTop)
writematrix(label,'D:\resultpath\result.txt')
percent=100*scores(classNames == label);
writematrix(percent,'D:\resultpath\percent.txt')
else
k=0;
writematrix("",'D:\resultpath\result.txt')
writematrix("",'D:\resultpath\percent.txt')
k=k+1;
end
end
Réponses (1)
Pratyush Swain
le 16 Mai 2024
Hi Shreyas,
To ensure your MATLAB code continues running in an infinite loop, even when the image file is deleted, you need to handle the scenario where "imread" throws an error in a better manner.
Here is an modified workflow of your implementation that includes a "try-catch" block:
while true
load('D:\modelpath\lymphnodenet.mat');
classNames = trainedNetwork_1.Layers(end).ClassNames;
if isfile('D:\imagepath\image.BMP')
try
I = imread('D:\imagepath\image.bmp');
I = imresize(I,[224,224]);
[label,scores] = classify(trainedNetwork_1,I);
subplot(1,2,1)
imshow(I)
title(string(label) + ", " + num2str(100*scores(classNames == label),3) + "%");
[~,idx] = sort(scores,'descend');
idx = idx(2:-1:1);
classNamesTop = trainedNetwork_1.Layers(end).ClassNames(idx);
scoresTop = scores(idx);
subplot(1,2,2)
barh(scoresTop)
xlim([0 1])
title('Top 5 Predictions')
xlabel('Probability')
yticklabels(classNamesTop)
writematrix(label,'D:\resultpath\result.txt')
percent=100*scores(classNames == label);
writematrix(percent,'D:\resultpath\percent.txt')
catch
% If imread fails, write blank to the text files
writematrix("",'D:\resultpath\result.txt')
writematrix("",'D:\resultpath\percent.txt')
end
else
% If the file does not exist, write blank to the text files
writematrix("",'D:\resultpath\result.txt')
writematrix("",'D:\resultpath\percent.txt')
end
pause(1);
end
Please refer to the above example workflow and try to include try-catch in your implementation. For more information please refer to https://www.mathworks.com/help/matlab/ref/try.html
Hope this helps your usecase.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!