How to copy 30K images from folder to another according to the names in train and test text listes

clear,clc;
path = addpath(genpath('D:\DOCTORAT 2019\MATLAB Codes\datasets\NUS-WIDE-OBJECT\image list'));
DIR = 'D:\Dataset\NUSWIDE-Obj\imagesnus30k';%Source file
D_dir = 'D:\Dataset\NUSWIDE-Obj\Images30K';%Destination file (Sould be 31-subfolder=Nbre of classes)
class = {'bear','birds','boats','book','cars','cat','computer','coral','cow','dog','elk','fish','flags','flowers','fox','horses','leaf','plane','rocks','sand','sign','statue','sun','tiger','tower','toy','train','tree','vehicle','whales','zebra'}; %31 classes
tr = textread('Train.txt','%s');
ts = textread('Test.txt','%s');
imgs = vertcat(tr,ts);
for i =1:size(imgs,1)
disp(i)
% img = imread([ DIR,'\\',string(imgs(i,1))]);
imgname = (strcat(DIR, '\',string(imgs(i,1))));
img_files{i} = char(imgname);
fullFileName = fullfile(DIR, imgname);
%imwrite(img,D_dir,'jpg')
% figure(1),imshow(img);
%pause();
% imds = imageDatastore(img_files{i});
% img = readimage(imds,I);
% [img,info] = readimage(imds,2);
end

댓글 수: 3

What is your question?
It is a bad design to add folders to Matlab's PATH only to import files. Insert only folder, which contain M-files to the PATH and import files using absolute path names.
The readers can help you, if you explain, why you have commented the part of the code.
Sorry, i have corrected the question.
The code is juste to have a visual description of the subfolders (classes).
What is a "visual description" and a "class"?

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

 채택된 답변

With some guessing:
source = 'D:\Dataset\NUSWIDE-Obj\imagesnus30k';
dest = 'D:\Dataset\NUSWIDE-Obj\Images30K';
tr = textread('Train.txt', '%s');
ts = textread('Test.txt', '%s');
list = [tr; ts];
for k = 1:numel(list)
name = list{k};
copyfile(fullfile(source, name), fullfile(dest, name));
end

댓글 수: 11

It didn't work and report an error because of the variable "name" in "copyfile(fullfile(source, name), fullfile(dest, name));"
I don't have a subfolders in the source file(One folder) but i have to create 31 subfolders in the destination file according to the train and test texts.
Please could you consider this point in your code?
If you get an error message, it would be useful to post a copy. How could I fix an error without knowing the message?
I do not know, which destination folder have to be chosen for the specific files. I do not know the contents of the txt files also. Therefore I do no see a chance to implement details.
source = 'D:\cvprw15\data\Flickr';
dest = 'D:\cvprw15\data\NUS-WIDE';
tr = textread('Train.txt', '%s');
ts = textread('Test.txt', '%s');
list = [tr; ts];
for k = 1:numel(list)
name = list{k};
copyfile(fullfile(source, name), fullfile(dest, name));
end
----------------------------------------------------------------
Error using copyfile
Le chemin d’accès spécifié est introuvable.
Error in Matlab_help_NUSWIDE (line 8)
copyfile(fullfile(source, name), fullfile(dest, name));
The error message is clear: The path does not exist. You can check this:
for k = 1:numel(list)
name = list{k};
src = fullfile(source, name);
try
copyfile(src, fullfile(dest, name));
catch ME
sr
if ~isfile(src)
error('File name not existing: %s', src)
else
rethrow(ME);
end
end
end
I think because i have a subfolders(image classes) in the source file but i don't have in the distination, am i right?
The error message is clear: The specified file does not exist. The destination is not the problem.
"i have a subfolders(image classes)" - so insert this subfolder into the source. Maybe:
src = fullfile(source, 'YourSubFolder', name);
There is no chance that I can guesse these details.
By the way, please do not post code as screen shots, but as text. This is more useful and easier to read.
I have tried to create the first subfolder(actor)in the destination folder manually and the code worked and copied some images.How to automate the creation of subfolders before copying images?
Please see the error report after adding 'YourSubFolder' to the src
Error using Matlab_help_NUSWIDEObj (line 16)
File name not existing: D:\Dataset\NUSWIDE-Obj\Flickr\YourSubFolder\actor\0211_2233188435.jpg
"How to automate the creation of subfolders before copying images?"
"Please see the error report after adding 'YourSubFolder' to the src"
Jan used the text 'YourSubFolder' as a placeholder for you to replace with the actual name of your subfolder. The text 'YourSubFolder' tells you where you need to put the name of your subfolder.
So you need to replace 'YourSubFolder' with whatever your subfolder is called. Probably via a variable.
Because as Jan said, we cannot guess the details of your folder structure. Only you know this.
I still don't know how to extract the name of subfolder; for example for k=1, we pick the name 'actor\0211_2233188435.jpg' from source folder; how to extract the name of the first subfoder "actor" in the distination folder before copying images (only the source folder contains subfolders)
@riad didou: It would be much easier to post a working answer, if you provide a small set of input data. It is hard to guess, how the subfolder name has to be obtained, if you do not explain, what the inputs of your function is.
With some bold guessing:
for k = 1:numel(list)
name = list{k};
subfolder = fileparts(name);
src = fullfile(source, name);
destfolder = fullfile(dest, subfolder);
if ~isfolder(destfolder)
mkdir(destfolder);
end
try
copyfile(src, fullfile(dest, name));
catch ME
sr
if ~isfile(src)
error('File name not existing: %s', src)
else
rethrow(ME);
end
end
end
WOW..it works now, thank you so much

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

추가 답변 (1개)

Use copyfile to copy a file to a new location. You could probably use arrayfun to more quickly add the full file path to the file names output from the dir function. Or, you could probably pass a function to arrayfun that does the full file path creation and the copyfile all in one.

댓글 수: 2

I dindn't understand; could you give me an example to illustrate how it works
You can type "doc copyfile" and "doc arrayfun" in MATLAB to bring up the documentation and examples of using both functions.

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

카테고리

도움말 센터File Exchange에서 Images에 대해 자세히 알아보기

제품

질문:

2022년 11월 21일

댓글:

2022년 11월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by