File size: 3,196 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 ADMINcreateDictionary(D, HOMEIMAGES)
% VISUAL OBJECT DICTIONARY
%
% Creates a visual index of all the objects in the database
%
% ADMINcreateDictionary(D, HOMEIMAGES)



homethumbnails = 'objectDictionary';

% List of objects
[names, counts, imagendx, objectndx, descriptionndx] = LMobjectnames(D);
[foo, ndx] = sort(counts, 'descend');
Nnames = length(names);
Ninstances = length(imagendx);

% Create index with global names and with local instances
fid = fopen('dictionary.js', 'w');
fprintf(fid, 'var d=new Array();\n')
fprintf(fid, 'var n=new Array();\n')
fprintf(fid, 'var x=new Array();\n')
fprintf(fid, 'var a=new Array();\n')
fprintf(fid, 'var p=new Array();\n')
fprintf(fid, 'var q=new Array();\n')
fprintf(fid, 'var Nnames=%d;\n', Nnames)
fprintf(fid, 'var Ninstances=%d;\n', Ninstances)
fprintf(fid, 'var nrows=%d;\n', Nnames)
fprintf(fid, 'var ncols=%d;\n', 1)
C = 0;
for i = 1:Nnames
    na = strrep(names{ndx(i)}, '''', '');
    fprintf(fid, 'd[%d]=''%s'';\n', i, na);          % strings, object names
    fprintf(fid, 'n[%d]=''%d'';\n', i, counts(ndx(i)));         % number of instances
    fprintf(fid, 'x[%d]=''%d'';\n', i, ndx(i)); % instance index
    fprintf(fid, 'a[%d]=%d;\n', i, C); % instance index
    C = C+counts(ndx(i));
end
m=0;
for i = 1:Nnames
    k = find(descriptionndx==ndx(i));
    for j= 1:length(k)
        m = m+1;
        o = objectndx(k(j));
        im = imagendx(k(j));
        %page = [D(im).annotation.folder '&image=' D(im).annotation.filename];
        %page = strrep(page, '.jpg', '');       
        fprintf(fid, 'p[%d]="%s";\n', m, D(im).annotation.folder);  % strings, object names
        fprintf(fid, 'q[%d]="%s";\n', m, strrep(D(im).annotation.filename, '.jpg', ''));  % strings, object names
    end
end
fclose(fid)



% Loop on D, loop on object
instancesCounts = zeros(Nnames,1);
problems = {''};
for i = 1:length(D)
    disp(i)
    k = find(imagendx==i);
    
    if ~isempty(k)
        o = objectndx(k);
        d = descriptionndx(k);
        
        % get thumbnail and extract object and mask
        try
            [annotation, img] = LMread(D, i, HOMEIMAGES);
            for n = 1:length(o)
                %[imgCrop, scaling] = LMobjectnormalizedcrop(img, annotation, o(n), 2, 64, 64);
                imgCrop = LMobjectcrop(img, annotation, o(n), 3);
                T = imresize(imgCrop, 64/max(size(imgCrop)), 'bilinear');
                [foo, T] = LMimpad('', T, [70 70], 255);
                
                %thumb = LMobjectThumbnail(D(i).annotation, img, o(n), [64 64]);
                
                
                %T = thumb(:,1:end/2-1,:);
                instancesCounts(d(n)) = instancesCounts(d(n))+1;
                folder = fullfile(homethumbnails, sprintf('obj_%d', d(n)));
                filename = fullfile(folder, sprintf('ins_%d.jpg', instancesCounts(d(n))));
                
                mkdir(folder)
                imwrite(T, filename, 'jpg', 'quality', 95)
                
                imshow(T)
                size(T)
                drawnow
            end
        catch
            a = lasterror;
            problems = [problems; a.message];
        end
    end
end