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

2 ビュー (過去 30 日間)
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 件のコメント
riad didou
riad didou 2022 年 11 月 21 日
Sorry, i have corrected the question.
The code is juste to have a visual description of the subfolders (classes).
Jan
Jan 2022 年 11 月 21 日
編集済み: Jan 2022 年 11 月 21 日
What is a "visual description" and a "class"?

サインインしてコメントする。

採用された回答

Jan
Jan 2022 年 11 月 21 日
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 件のコメント
Jan
Jan 2022 年 11 月 26 日
@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
riad didou
riad didou 2022 年 11 月 26 日
WOW..it works now, thank you so much

サインインしてコメントする。

その他の回答 (1 件)

Benjamin Thompson
Benjamin Thompson 2022 年 11 月 21 日
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 件のコメント
riad didou
riad didou 2022 年 11 月 21 日
I dindn't understand; could you give me an example to illustrate how it works
Benjamin Thompson
Benjamin Thompson 2022 年 11 月 21 日
You can type "doc copyfile" and "doc arrayfun" in MATLAB to bring up the documentation and examples of using both functions.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by