File size: 2,932 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
95
96
97
98
99
100
101
function ADMINremovespaces(HOMEANNOTATIONS, HOMEIMAGES, annotationfolder)
%
% Removes non-alphabetic characters, spaces, %2025 and %20 from filenames and replaces them with '_'
% This function is to be used with caution as it might affect other users.
% 
% Those characteres can be introduced sometimes when reading images from
% the web.
%
% ADMINremovespaces(HOMEANNOTATIONS, HOMEIMAGES, annotationfolder)

% Rename images

% create list of folders
if nargin == 3
    Folder = {annotationfolder};
else
    folders = genpath(HOMEIMAGES);
    h = [findstr(folders,  pathsep)];
    h = [0 h];
    Nfolders = length(h)-1;
    for i = 1:Nfolders
        tmp = folders(h(i)+1:h(i+1)-1);
        tmp = strrep(tmp, HOMEIMAGES, ''); tmp = tmp(2:end);
        Folder{i} = tmp;
    end
end

for n = 1:length(Folder)
    annotationfolder = Folder{n};
    
    if ~isempty(HOMEIMAGES)
        files = dir(fullfile(HOMEIMAGES, annotationfolder, '*.jpg'));
        
        Nfiles = length(files);
        for i = 1:Nfiles
            filename = fullfile(HOMEIMAGES, annotationfolder, files(i).name);
            
            % rename image file
            src = filename;
            dest = removecaracters(files(i).name);
            cmd = sprintf('rename "%s" %s', src, dest);
            disp(cmd)
            system(cmd)
        end
    end
    
    % Rename annotations and replace %2025 and %20 and spaces by '_' from annotation.filename
    if ~isempty(HOMEANNOTATIONS)
        files = dir(fullfile(HOMEANNOTATIONS, annotationfolder, '*.xml'));
        
        Nfiles = length(files);
        for i = 1:Nfiles
            filename = fullfile(HOMEANNOTATIONS, annotationfolder, files(i).name);
            
            % rename image file indexed inside the file
            [fid, message] = fopen(filename,'r');
            if fid == -1; error(message); end
            xml = fread(fid, 'uint8=>char');
            fclose(fid);
            xml = xml';
            
            ii = strfind(xml, '<filename>');
            jj = strfind(xml, '</filename>');
            
            if ~isempty(i)
                tmp = removecaracters(xml(ii:jj));
                xml = [xml(1:ii-1) tmp xml(jj+1:end)];
                
                % save annotation
                fid = fopen(filename,'w');
                fprintf(fid, xml');
                fclose(fid);
                
                
                % rename annotation file
                src = filename;
                dest = removecaracters(files(i).name);
                cmd = sprintf('rename "%s" %s', src, dest);
                disp(cmd)
                system(cmd)
            else
                disp(filename)
            end
        end
    end
end


function out = removecaracters(in)
out = lower(in);

out = strrep(out, ' ', '_');
out = strrep(out, '%2520', '_');
out = strrep(out, '%20', '_');

out = regexprep(out, '\W', '_');