File size: 2,739 Bytes
d4035c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function ADMINrandomfilenames(HOMEANNOTATIONS, HOMEIMAGES, annotationfolder)
%
% ADMINrandomfilenames(HOMEANNOTATIONS, HOMEIMAGES, annotationfolder)

root = 'sun_a'; % this is the string that will be part of the first characters of the image name.
Nchar = 15;

if nargin == 1
    HOMEIMAGES = HOMEANNOTATIONS;
end

% create list of folders
if nargin == 3
    Folder = {annotationfolder};
else
    Folder = [{''} folderlist(HOMEIMAGES)];
end


for n = 1:length(Folder)
    f = Folder{n};
    
    if ~isempty(fullfile(HOMEIMAGES, f))
        files = dir(fullfile(HOMEIMAGES, f, '*.jpg'));
        
        Nfiles = length(files);
        for i = 1:Nfiles
            % check if file already starts with the root, then ignore
            
            oldimgname = files(i).name;
            oldanoname = strrep(oldimgname, '.jpg', '.xml');
            
            % generate new name
            newname = lower([root char([65+fix(26*rand(1,Nchar))])]);
            newimgname = [newname '.jpg'];
            newanoname = [newname '.xml'];
            
            % rename image
            src = fullfile(HOMEIMAGES, f, oldimgname);
            dest = fullfile(HOMEIMAGES, f, newimgname);
            if 0
                cmd = sprintf('mv "%s" %s', src, dest);
            else
                cmd = sprintf('rename "%s" %s', src, newimgname);
            end
            disp(cmd)
            system(cmd);
            
            % check if annotation exists.
            src = fullfile(HOMEANNOTATIONS, f, oldanoname);
            if exist(src, 'file')
                % rename file
                dest = fullfile(HOMEANNOTATIONS, f, newanoname);
                if exist(dest, 'file')
                    error('Duplicate image detected')
                end
                
                if 0
                    cmd = sprintf('mv "%s" %s', src, dest);
                else
                    cmd = sprintf('rename "%s" %s', src, newanoname);
                end
                disp(cmd)
                system(cmd);
                
                % load annotation
                xml = loadxml(dest);
                
                % change inside annotation file
                ii = strfind(xml, '<filename>');
                jj = strfind(xml, '</filename>');
                xml = [xml(1:ii+9) newimgname xml(jj:end)];
                
                % save annotation
                fid = fopen(dest,'w');
                fprintf(fid, xml');
                fclose(fid);
            end
        end
    end
end
                    

function xml = loadxml(filename)
[fid, message] = fopen(filename,'r');
if fid == -1; error(message); end
xml = fread(fid, 'uint8=>char');
fclose(fid);
xml = xml';