Skip to content

Commit 3e55c94

Browse files
author
Brook Roberts
committed
Basic loading/saving of unionfind, but not full solution
1 parent d0d1458 commit 3e55c94

3 files changed

Lines changed: 41 additions & 6 deletions

File tree

opensfm/commands/create_tracks.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ def run(self, args):
1919
data = dataset.DataSet(args.dataset)
2020
images = data.images()
2121

22+
try:
23+
graph = data.load_tracks_graph()
24+
tracks, processed_images = matching.tracks_and_images(graph)
25+
except IOError:
26+
graph = None
27+
tracks = None
28+
processed_images = []
29+
30+
remaining_images = set(images) - set(processed_images)
31+
2232
# Read local features
2333
logging.info('reading features')
2434
features = {}
@@ -30,7 +40,7 @@ def run(self, args):
3040

3141
# Read matches
3242
matches = {}
33-
for im1 in images:
43+
for im1 in remaining_images:
3444
try:
3545
im1_matches = data.load_matches(im1)
3646
except IOError:
@@ -39,7 +49,7 @@ def run(self, args):
3949
matches[im1, im2] = im1_matches[im2]
4050

4151
tracks_graph = matching.create_tracks_graph(features, colors, matches,
42-
data.config)
52+
data.config, data)
4353
data.save_tracks_graph(tracks_graph)
4454

4555
end = time.time()

opensfm/dataset.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import json
55
import errno
6-
import pickle
6+
import cPickle as pickle
77
import gzip
88
import numpy as np
99
import networkx as nx
@@ -329,6 +329,19 @@ def find_matches(self, im1, im2):
329329
return im2_matches[im1][:, [1, 0]]
330330
return []
331331

332+
def __unionfind_file(self, filename=None):
333+
"""Return path of unionfind file"""
334+
return os.path.join(self.data_path, filename or 'unionfind.pkl')
335+
336+
def load_unionfind_file(self, filename=None):
337+
"""Return unionfind of tracks"""
338+
with open(self.__unionfind_file(filename)) as fin:
339+
return load_unionfind_file(fin)
340+
341+
def save_unionfind_file(self, unionfind, filename=None):
342+
with open(self.__unionfind_file(filename), 'w') as fout:
343+
save_unionfind_file(fout, unionfind)
344+
332345
def __tracks_graph_file(self, filename=None):
333346
"""Return path of tracks file"""
334347
return os.path.join(self.data_path, filename or 'tracks.csv')
@@ -501,3 +514,9 @@ def save_tracks_graph(fileobj, graph):
501514
r, g, b = data['feature_color']
502515
fileobj.write('%s\t%s\t%d\t%g\t%g\t%g\t%g\t%g\n' % (
503516
str(image), str(track), fid, x, y, r, g, b))
517+
518+
def load_unionfind_file(fileobj):
519+
return pickle.load(fileobj)
520+
521+
def save_unionfind_file(fileobj, unionfind):
522+
pickle.dump(unionfind, fileobj, protocol=pickle.HIGHEST_PROTOCOL)

opensfm/matching.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,14 @@ def good_track(track, min_length):
142142
return False
143143
return True
144144

145-
146-
def create_tracks_graph(features, colors, matches, config):
145+
def create_tracks_graph(features, colors, matches, config, data):
147146
logger.debug('Merging features onto tracks')
148-
uf = UnionFind()
147+
148+
try:
149+
uf = data.load_unionfind_file()
150+
except IOError:
151+
uf = UnionFind()
152+
149153
for im1, im2 in matches:
150154
for f1, f2 in matches[im1, im2]:
151155
uf.union((im1, f1), (im2, f2))
@@ -158,6 +162,8 @@ def create_tracks_graph(features, colors, matches, config):
158162
else:
159163
sets[p] = [i]
160164

165+
data.save_unionfind_file(uf)
166+
161167
tracks = [t for t in sets.values() if good_track(t, config.get('min_track_length', 2))]
162168
logger.debug('Good tracks: {}'.format(len(tracks)))
163169

0 commit comments

Comments
 (0)