def test_identity(self): im = Image.open("/u/konolige/vslam/data/indoor1/left-%04d.ppm" % 1000) kp = [(x-16, y-16) for (x,y) in fast.fast(im.tostring(), im.size[0], im.size[1], 150, 40)] cl1 = calonder.classifier() cl1.train(im.tostring(), im.size[0], im.size[1], kp) cl1.write('unittest.tree') def testclassifier(kp, im, cl): ma = calonder.BruteForceMatcher() sigs = [] for (x,y) in kp: patch = im.crop((x,y,x+32,y+32)) sig = cl.getSignature(patch.tostring(), patch.size[0], patch.size[1]) #print ["%3f" % x for x in sig.dump()] sigs.append(sig) ma.addSignature(sig) for (i,(x,y)) in enumerate(kp): patch = im.crop((x,y,x+32,y+32)) sig = cl.getSignature(patch.tostring(), patch.size[0], patch.size[1]) (index, distance) = ma.findMatch(sig) self.assert_(i == index) testclassifier(kp, im, cl1) del cl1 # Now make another classifier, and read it from the file above cl2 = calonder.classifier() cl2.read('unittest.tree') testclassifier(kp, im, cl2)
def __init__(self, *args): self.cl = calonder.classifier() #self.cl.setThreshold(0.0) search = [ '/u/prdata/calonder_trees/current.rtc', '/u/jamesb/current.rtc', None ] for filename in search: if not filename: print "Failed to find current.rtc in", search assert 0 if os.access(filename, os.R_OK): self.cl.read(filename) break DescriptorScheme.__init__(self, *args)
def match_runs(reference_files, query_files): print "loading vocabulary tree..." vt = place_recognition.load("/u/mihelich/images/holidays/holidays.tree") cl = calonder.classifier() cl.read('/u/prdata/calonder_trees/current.rtc') print "adding database images..." for i in reference_files[2]: # Add image to vocabulary tree name = reference_files[0] % i img = Image.open(name) desc, kp = get_features(img, cl) vt.add(img, desc) print '%s has %d feature points' % (name, len(kp)) # Save coordinates and descriptors file = open(reference_files[1] % (i, "key"), "w") for (pt, d) in zip(kp, desc): # Format: x y [d...] file.write("%i %i %s\n" % (pt[0], pt[1], str(list(d)))) print "getting top-k views for query images..." N = len(reference_files[2]) k = 32 M = [] for i in query_files[2]: # Get indexes of best N matching views from reference run name = query_files[0] % i img = Image.open(name) desc, kp = get_features(img, cl) print '%s has %d feature points' % (name, len(kp)) scores = vt.topN(img, desc, N) M.append(scores) top_n = sorted(enumerate(scores), key=operator.itemgetter(1))[:k] # Save filenames of matching views file = open(query_files[1] % (i, "match"), "w") for (index, score) in top_n: file.write("%s %f\n" % (reference_files[0] % reference_files[2][index], score)) return M
def __init__(self): self.cl = calonder.classifier() #self.cl.setThreshold(0.0) self.cl.read('/u/prdata/calonder_trees/land50.trees') self.ma = calonder.BruteForceMatcher()
print 'opening images...' outlet_train = [ Image.open(outlet_files % i).convert('L') for i in range(416, 427) ] no_out_train = [ Image.open(no_out_files % i).convert('L') for i in range(416, 427) ] #outlet_test = [ Image.open(outlet_files % i).convert('L') for i in range(372, 416) + range(427, 471) ] #no_out_test = [ Image.open(no_out_files % i).convert('L') for i in range(372, 416) + range(427, 471) ] print 'loading trees...' vt = place_recognition.load('/u/mihelich/images/holidays/holidays_b5d4.tree') #vt = place_recognition.load('/u/mihelich/images/holidays/holidays.tree') #vt = place_recognition.vocabularytree() #vt.build(outlet_train, 5, 4, False) cl = calonder.classifier() cl.read('/u/prdata/calonder_trees/current.rtc') def kp_d(frame): fkp = fast.fast(frame.tostring(), frame.size[0], frame.size[1], 10, 15) fkp = [(x, y, r) for (x, y, r) in fkp if (16 <= x and 16 <= y and x <= frame.size[0] - 16 and y <= frame.size[1] - 16)] fkp = fast.nonmax(fkp) #damn this is slow kp = [(x, y) for ( x, y, r) in sorted(fkp, key=lambda x: (x[2], x[0], x[1]), reverse=True)[:200] ] print 'kp_d gave %d points' % len(kp)
def test_identity(self): im = Image.open("f0-left.png") kp = [(x-16, y-16) for (x,y,r) in fast.fast(im.tostring(), im.size[0], im.size[1], 55, 0) if (x > 32) and (y > 32) and (x < (640-32)) and (y < (480-32))] print "keypoints", len(kp) dim = 176 # actual dimension will be min(176, |kp|) cl1 = calonder.classifier() if 0: cl1.train(im.tostring(), im.size[0], im.size[1], kp, 50, 10, 100, dim, 0) else: # CMakefile.txt downloads current.rtc before this test runs filename = 'current.rtc' cl1.read(filename) dim = cl1.dimension() if 0: for i in range(1000000): print i sigs = cl1.getSignatures(im.size, im.tostring(), kp) #for (x,y) in kp: # patch = im.crop((x,y,x+32,y+32)) # sig = cl1.getSignature(patch.tostring(), patch.size[0], patch.size[1]) return def testclassifier(kp, im, cl): ma = calonder.BruteForceMatcher(dim) sigs = [] kp = kp[:] for (x,y) in kp: print x,y patch = im.crop((x,y,x+32,y+32)) sig = cl.getSignature(patch.tostring(), patch.size[0], patch.size[1]) sigs.append(sig) ma.addSignature(sig) #print cl.getSignatures(im, kp) for (i,(x,y)) in enumerate(kp): patch = im.crop((x,y,x+32,y+32)) sig = cl.getSignature(patch.tostring(), patch.size[0], patch.size[1]) (index, distance) = ma.findMatch(sig) print "i = %d, match = %u, distance = %.3f" % (i, index, distance) self.assert_(i == index) testclassifier(kp, im, cl1) print "done" print "Writing to unittest.tree... ", cl1.write('unittest.tree') print "done" del cl1 # Now make another classifier, and read it from the file above cl2 = calonder.classifier() print "Reading classifier... ", cl2.read('unittest.tree') print "done" testclassifier(kp, im, cl2) f = open('sigs.pickle', 'w') for (x,y) in kp[:3]: patch = im.crop((x,y,x+32,y+32)) sig = cl2.getSignature(patch.tostring(), patch.size[0], patch.size[1]) pickle.dump(sig, f) print "saved", sig f.close() f = open('sigs.pickle') for i in range(3): print "loaded", pickle.load(f)