function [D, unmatched] = LMaddtags(D, tagsfile) % % [D, unmatched] = LMaddtags(D, 'tags.txt'); % % Reemplaces object names with the names in the list tags.txt % % This new field contains a tag that can be used to unify labelme % descriptions into more consistent labels. [Tag, Descriptions] = loadtags(tagsfile); Ntags = length(Tag); % 1) Create list of labelme descriptions [labelmeDescriptions, counts, imagendx, objectndx, descriptionndx] = LMobjectnames(D, 'name'); % 2) Find tag for each description and make list of unmatched descriptions ndxtag = zeros(length(labelmeDescriptions),1); for i = 1:length(labelmeDescriptions) for k = 1:Ntags j = strmatch(lower(labelmeDescriptions{i}), Descriptions{k}, 'exact'); if ~isempty(j) ndxtag(i) = k; break end end end % Create list of unmatched descriptions and sort them by counts unmatched = labelmeDescriptions(ndxtag==0); [cc,jj] = sort(counts(ndxtag==0), 'descend'); unmatched = unmatched(jj); % 3) Add tag field to matched objects for i = find(ndxtag>0)' j = find(descriptionndx==i); for k = 1:length(j) D(imagendx(j(k))).annotation.object(objectndx(j(k))).tag = Tag{ndxtag(i)}; D(imagendx(j(k))).annotation.object(objectndx(j(k))).description = D(imagendx(j(k))).annotation.object(objectndx(j(k))).name; D(imagendx(j(k))).annotation.object(objectndx(j(k))).name = Tag{ndxtag(i)}; end end [labelmetags, counttags] = LMobjectnames(D, 'tag'); % Visualization if nargout == 0 figure loglog(sort(counts, 'descend'), 'r') hold on loglog(sort(counttags(2:end), 'descend'), 'g') xlabel('rank') ylabel('counts') axis('tight') title(sprintf('descriptions: %d, Ntags:%d, polygons: %d, with tags: %d', length(labelmeDescriptions), Ntags, sum(counts), sum(counttags(2:end)))) end