Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
tylin committed Mar 24, 2015
2 parents 90ad613 + ef0f4bb commit affc7f0
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 0 deletions.
56 changes: 56 additions & 0 deletions coco/convert_to_pascalformat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/python -tt

# python code to convert coco val2014 JSON file to PASCAL XML.

import sys

def main():
if len(sys.argv) != 3:
print 'usage: python convert_to_pascalformat.py coco_dataDir coco_dataType'
print 'for example: python convert_to_pascalformat.py \'./\' \'val2014\''
sys.exit(1)

dataDir = sys.argv[1]
dataType = sys.argv[2]

from pycocotools.coco import COCO
import os

annFile='%s/annotations/instances_%s.json'%(dataDir,dataType)

coco=COCO(annFile)
cats = coco.loadCats(coco.getCatIds())
nms=[cat['name'] for cat in cats]

imgIds = coco.getImgIds()

directory = './annotations_pascalformat/'
if not os.path.exists(directory):
os.makedirs(directory)

for n in xrange(len(imgIds)):
img = coco.loadImgs(imgIds[n])[0]
annIds = coco.getAnnIds(imgIds=img['id'], iscrowd=None)
anns = coco.loadAnns(annIds)

xml = '<annotation>\n<folder>\nCOCO2014pascalformat\n</folder>\n<filename>\n'
xml += img['file_name'] + '\n</filename>\n<source>\n<database>\nCOCO2014pascalformat\n</database>\n</source>\n<size>\n'
xml += '<width>\n' + str(img['width']) + '\n</width>\n' + '<height>\n' + str(img['height']) + '\n</height>\n'
xml += '<depth>\n3\n</depth>\n</size>\n<segmented>\n0\n</segmented>\n'

for i in xrange(len(anns)):
bbox = anns[i]['bbox']
xml += '<object>\n<name>\n' + str(anns[i]['category_id']) + '\n</name>\n'
xml += '<bndbox>\n<xmin>\n' + str(int(round(bbox[0]))) + '\n</xmin>\n'
xml += '<ymin>\n' + str(int(round(bbox[1]))) + '\n</ymin>\n'
xml += '<xmax>\n' + str(int(round(bbox[0] + bbox[2]))) + '\n</xmax>\n'
xml += '<ymax>\n' + str(int(round(bbox[1] + bbox[3]))) + '\n</ymax>\n</bndbox>\n'
xml += '<truncated>\n0\n</truncated>\n<difficult>\n0\n</difficult>\n</object>\n'
xml += '</annotation>'
f_xml = open(directory + img['file_name'].split('.jpg')[0] + '.xml', 'w')
f_xml.write(xml)
f_xml.close()
print str(n) + ' out of ' + str(len(imgIds))

if __name__ == '__main__':
main()
138 changes: 138 additions & 0 deletions test/coco_dpm_test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
function [ output ] = coco_dpm_test( dpm_model, dpm_thresh, dataDir, dataType, imgIds, outputFile )
% coco_dpm_test is a wrapper of the DPM code running a trained dpm model
% on a set of images specified by their COCO ids.
% Detections are:
% - saved in the following JSON format:
% [{'image_id': int, 'cateogry_id': int, 'bbox': [x, y, w, h], 'score': float}]
% - returned in an output cell array
%
% USAGE:
% $ > load VOC2010/bicycle_final.mat
% $ > output = coco_dpm_test( model, -0.5, '../coco_dir', 'val2014', [388258], './bike_dpm_detections_coco.json' )
%
% INPUTS:
% - dpm_model - model trained with dpm
% - dpm_thresh - threshold for detection of the trained model
% - dataDir - path to the coco main directory
% - dataType - train, test or validation type of data
% - imgIds - array containing the COCO Ids of the images to use
% - outputFile - path to the output file
%
% OUTPUTS:
% - output - struct array containing an element for each detection
%
% Code written by Matteo Ruggero Ronchi, 2015.
% Licensed under the Simplified BSD License [see coco/license.txt]

output = struct();

%% set dpm model parameters
if nargin < 2
% if the threshold is not included we use the one in the model
dpm_thresh = dpm_model.thresh;
end

%% initialize COCO api for instance annotations
if nargin < 3
dataDir = '../';
end

if nargin < 4
dataType = 'test2014';
end

annFile=sprintf('%s/annotations/instances_%s.json',dataDir,dataType);
if(~exist('coco','var')), coco=CocoApi(annFile); end

%% recover Ids of images to use for testing
if nargin < 5
% if a list of IDs is not specified test on all the images
imgIds = coco.getImgIds();
end
nImages = length( imgIds );

%% set maximum detection variables
% maximum of 100 detections per image are allowed
maxDetections = 100;
counter = 0;

%% adjuste difference in category names between COCO and PASCAL
switch dpm_model.class
case 'aeroplane'
dpm_class = 'airplane';
case 'diningtable'
dpm_class = 'dining table';
case 'motorbike'
dpm_class = 'motorcycle';
case 'pottedplant'
dpm_class = 'potted plant';
case 'sofa'
dpm_class = 'couch';
case 'tvmonitor'
dpm_class = 'tv';
otherwise
dpm_class = dpm_model.class;
end
category_id = coco.getCatIds('catNms',dpm_class);

for jj = 1:nImages

%% load and display image
try
img_info = coco.loadImgs(imgIds(jj));
img = imread(sprintf('%s/images/%s/%s',dataDir,dataType,img_info.file_name));

%figure(1); imagesc(img); axis('image'); set(gca,'XTick',[],'YTick',[])
catch
error('Error in imgIds vector. Id [%d] not in COCO! Stopping Test.', imgIds(jj) );
end

%% run dpm code on the image
[ds, bs] = imgdetect(img, dpm_model, dpm_thresh);

if ~isempty(ds)
if dpm_model.type == model_types.MixStar
if isfield(dpm_model, 'bboxpred')
bboxpred = dpm_model.bboxpred;
[ds, bs] = clipboxes(img, ds, bs);
[ds, bs] = bboxpred_get(bboxpred, ds, reduceboxes(dpm_model, bs));
else
warning('no bounding box predictor found');
end
end
[ds, bs] = clipboxes(img, ds, bs);
I = nms(ds, 0.5);
ds = ds(I,:);
bs = bs(I,:);
end

%% put the detections in COCO format

nDetections = size(ds,1);
if (nDetections > maxDetections)
warning('Number of detections in image [%s/%d] exceeds maximum allowed (%d). Detections in excess will not be stored.', dataType, imgIds(jj), maxDetections);
nDetections = maxDetections;
end
for ii = 1:nDetections
counter = counter + 1;

output(counter).image_id = imgIds(jj);
output(counter).category_id = category_id;
output(counter).bbox = ds(ii,1:4);
output(counter).score = ds(ii,5);
end

end

%% save detections on filesystem in JSON format and return output structure
if nargin < 6
outputFile = strcat('./',dpm_class(~isspace(dpm_class)),'_dpm_detections_coco.json');
end

s = gason( output );

fid = fopen(outputFile, 'w');
fprintf(fid, '%s', s);
fclose(fid);


0 comments on commit affc7f0

Please sign in to comment.