因为最近使用labelImg软件标注训练图片,我把标记好的图片和标注文件放置在一个文件夹下,由于有多批次图片标注,每标注一批放在一个文件夹下,最终放置的文件夹如下:
现在我需要把这些文件夹下的图片和标注文件集中到两个文件ImSet(用于放置所有的标注图片),AnotSet(用于放置所有的标注文件)
为此我写了一个Matlab程序,主要函数是copyfile,好了现在直接上Matlab程序吧
%2018/09/07 by DQ
clc;
clear;
close all;
DisperalMainFolder='H:\WorkFile\SecondPeroidAnotBackup';
ConcentrateFolder='C:\Users\Administrator\Desktop\SecondPeroidUnifySet';
OldFileSet=dir(ConcentrateFolder);
OldFileSet(1:2)=[];
if ~isempty(OldFileSet)
disp('the folder exists other files');
return
end
ImFolderPath=fullfile(ConcentrateFolder,'ImSet');
if ~exist(ImFolderPath,'dir')
mkdir(ImFolderPath);
end
AnotFolderPath=fullfile(ConcentrateFolder,'AnotSet');
if ~exist(AnotFolderPath,'dir')
mkdir(AnotFolderPath);
end
FolderSet=dir(DisperalMainFolder);
FolderNum=length(FolderSet);
for i=3:FolderNum
FolderName=FolderSet(i).name;
FolderPath=fullfile(DisperalMainFolder,FolderName);
XmlFileSet=dir(strcat(FolderPath,'\*.xml'));
XmlFileNum=length(XmlFileSet);
fprintf('%s XmlFileNum=%d\n',FolderName,XmlFileNum);
%%%%%%%start%%%%%%%%%%
for k=1:XmlFileNum
XmlFileName=XmlFileSet(k).name;
XmlFilePath=fullfile(FolderPath,XmlFileName);
copyfile(XmlFilePath,AnotFolderPath);%复制标注文件到指定的文件夹
ImName=strcat(XmlFileName(1:end-4),'.jpg');
ImPath=fullfile(FolderPath,ImName);
copyfile(ImPath,ImFolderPath);%复制图片到指定的文件夹
end
%%%%%%%end%%%%%%%%%%
% %%上述start和end之间的程序有一种更简洁的方式
% XmlFileS=strcat(FolderPath,'\*.xml');%复制文件夹FolderPath下所有后缀名为.xml的文件
% copyfile(XmlFileS,AnotFolderPath);
% ImFileS=strcat(FolderPath,'\*.jpg');%复制文件夹FolderPath下所有后缀名为.jpg的文件
% copyfile(ImFileS,ImFolderPath);
end