def query2(querydir, querysurf, dbdir, mainOutputDir, nClosestCells, copytopmatch, params, copy_top_n_percell=0): lat, lon = info.getQuerySURFCoord(querysurf) closest_cells = util.getclosestcells(lat, lon, dbdir) assert dict(closest_cells)['37.8732916331,-122.268346029'] < 236 or dict(closest_cells)['37.8714489427,-122.272389514'] < 236 or dict(closest_cells)['37.8696062215,-122.273737308'] < 236 built = [ '37.8732916331,-122.268346029', '37.8714489427,-122.272389514', '37.8696062215,-122.273737308', ] closest_cells = filter(lambda c: c[0] in built, closest_cells) outputFilePaths = [] cells_in_range = [(cell, dist) for cell, dist in closest_cells[0:nClosestCells] if dist < cellradius + ambiguity+matchdistance] latquery, lonquery = info.getQuerySURFCoord(querysurf) if verbosity > 0: print "checking query: {0} \t against {1} \t cells".format(querysurf, len(cells_in_range)) for cell, dist in cells_in_range: latcell, loncell = cell.split(',') latcell = float(latcell) loncell = float(loncell) actualdist = info.distance(latquery, lonquery, latcell, loncell) if verbosity > 1: print "querying cell: {0}, distance: {1} with:{2}".format(cell, actualdist, querysurf) outputFilePath = os.path.join(mainOutputDir, querysurf + ',' + cell + ',' + str(actualdist) + ".res") outputFilePaths.append(outputFilePath) # start query query.run_parallel(dbdir, [c for c,d in cells_in_range], querydir, querysurf, outputFilePaths, params) # end query for cell, dist in cells_in_range: latcell, loncell = cell.split(',') latcell = float(latcell) loncell = float(loncell) actualdist = info.distance(latquery, lonquery, latcell, loncell) outputFilePath = os.path.join(mainOutputDir, querysurf + ',' + cell + ',' + str(actualdist) + ".res") if copy_top_n_percell > 0: outputDir = os.path.join(mainOutputDir, querysurf + ',' + cell + ',' + str(actualdist)) copy_topn_results(os.path.join(dbdir, cell), outputDir, outputFilePath, 4) # combined = combine_until_dup(outputFilePaths, 1000) combined = combine_topn_votes(outputFilePaths, float('inf')) # combined = filter_in_range(combined, querysurf) # write_scores(querysurf, combined, "/media/data/combined") [g, y, r, b, o] = check_topn_img(querysurf, combined, topnresults) if copytopmatch: match = g or y or r or b or o copy_top_match(querydir, querysurf.split('surf.npy')[0], combined, match) return [g, y, r, b, o]
def match(C, Q): if C.shuffle_cells: C._dbdir = None if C.override_cells: INFO('override cells') cells_in_range = [(c,0) for c in C.override_cells] else: # compute closest cells closest_cells = util.getclosestcells(Q.query_lat, Q.query_lon, C.dbdir) if C.restrict_cells: closest_cells = filter(lambda c: c[0] in C.restrict_cells, closest_cells) cells_in_range = [(cell, dist) for cell, dist in closest_cells[0:C.ncells] if dist < C.cellradius + C.ambiguity + C.matchdistance] INFO('Using %d cells' % len(cells_in_range)) if C.shuffle_cells: import reader sr = reader.get_reader('sift') supercell = sr.get_supercelldir( C.dbdir, [c for (c,d) in cells_in_range], C.overlap_method) C._dbdir = supercell if not cells_in_range: raise LocationOutOfRangeError # cache for fuzz runs if C.cacheEnable: key = derive_key(C, cells_in_range, Q.siftname) if key in cache: print 'cache hit' return cache[key] else: print 'cache miss' # compute output file paths for the cells cellpath = [c for c,d in cells_in_range] listofimages = [] if C.one_big_cell: INFO('Using 1 big cell (%d union)' % len(cells_in_range)) outputFilePaths = [os.path.join(C.matchdir, Q.siftname + ',' + getcellid(cellpath) + ".res")] #listofimages = lexiconrank.addImagetoList(listofimages, C.dbdir + cellpath) cellpath = [cellpath] else: outputFilePaths = [] for cell, dist in cells_in_range: if ',' in cell: latcell, loncell = cell.split(',') latcell = float(latcell) loncell = float(loncell) else: latcell, loncell = 0,0 actualdist = info.distance(Q.query_lat, Q.query_lon, latcell, loncell) outputFilePath = os.path.join(C.matchdir, Q.siftname + ',' + cell + ',' + str(actualdist) + ".res") outputFilePaths.append(outputFilePath) #listofimages = lexiconrank.addImagetoList(listofimages, C.dbdir + cell) # start query query.run_parallel(C, Q, cellpath, outputFilePaths, estimate_threads_avail()) #d, lexiconmatchedimg = lexiconrank.returnTopMatch_random(C.dbdump, listofimages, Q.jpgpath) # combine results if C.spatial_comb: comb_matches = corr.combine_spatial(outputFilePaths) else: print outputFilePaths comb_matches = corr.combine_matches(outputFilePaths) #geometric consistency reranking if C.disable_filter_step: imm = condense2(sorted(comb_matches.iteritems(), key=lambda x: len(x[1]), reverse=True)) rsc_ok = True else: imm, rsc_ok = rerank_ransac(comb_matches, C, Q) if C.weight_by_coverage: #print 1 ranked = weight_by_coverage(C, Q, imm) elif C.weight_by_distance: #print 2 ranked = weight_by_distance(C, Q, imm) else: #print 3 ranked = distance_sort(C, Q, imm) # top 1 stats = check_topn_img(C, Q, ranked, 1) # return statistics and top result matchedimg = ranked[0][0] matches = comb_matches[matchedimg + 'sift.txt'] if C.cacheEnable: cache[key] = (stats, matchedimg, matches, ranked) if C.match_callback: C.match_callback(C, Q, stats, matchedimg, ranked, cells_in_range, rsc_ok) # compute homography and draw images maybe if MultiprocessExecution.pool: MultiprocessExecution.pool.apply_async(compute_hom, [C.pickleable(), Q, ranked, comb_matches]) else: compute_hom(C, Q, ranked, comb_matches) ### Query Pose Estimation ### match = any(check_img(C, Q, ranked[0])) if (C.solve_pose and match and Q.name not in C.pose_remove) or C.pose_param['solve_bad']: #computePose.draw_dbimage(C, Q, matchedimg, match) if MultiprocessExecution.pool: MultiprocessExecution.pool.apply_async(computePose.estimate_pose, [C.pickleable(), Q, matchedimg, match]) else: computePose.estimate_pose(C, Q, matchedimg, match) # done return stats, matchedimg, matches, ranked
def query2(querydir, querysurf, dbdir, mainOutputDir, nClosestCells, copytopmatch, params, copy_top_n_percell=0): lat, lon = info.getQuerySURFCoord(querysurf) closest_cells = util.getclosestcells(lat, lon, dbdir) assert dict(closest_cells)['37.8732916331,-122.268346029'] < 236 or dict( closest_cells)['37.8714489427,-122.272389514'] < 236 or dict( closest_cells)['37.8696062215,-122.273737308'] < 236 built = [ '37.8732916331,-122.268346029', '37.8714489427,-122.272389514', '37.8696062215,-122.273737308', ] closest_cells = filter(lambda c: c[0] in built, closest_cells) outputFilePaths = [] cells_in_range = [(cell, dist) for cell, dist in closest_cells[0:nClosestCells] if dist < cellradius + ambiguity + matchdistance] latquery, lonquery = info.getQuerySURFCoord(querysurf) if verbosity > 0: print "checking query: {0} \t against {1} \t cells".format( querysurf, len(cells_in_range)) for cell, dist in cells_in_range: latcell, loncell = cell.split(',') latcell = float(latcell) loncell = float(loncell) actualdist = info.distance(latquery, lonquery, latcell, loncell) if verbosity > 1: print "querying cell: {0}, distance: {1} with:{2}".format( cell, actualdist, querysurf) outputFilePath = os.path.join( mainOutputDir, querysurf + ',' + cell + ',' + str(actualdist) + ".res") outputFilePaths.append(outputFilePath) # start query query.run_parallel(dbdir, [c for c, d in cells_in_range], querydir, querysurf, outputFilePaths, params) # end query for cell, dist in cells_in_range: latcell, loncell = cell.split(',') latcell = float(latcell) loncell = float(loncell) actualdist = info.distance(latquery, lonquery, latcell, loncell) outputFilePath = os.path.join( mainOutputDir, querysurf + ',' + cell + ',' + str(actualdist) + ".res") if copy_top_n_percell > 0: outputDir = os.path.join( mainOutputDir, querysurf + ',' + cell + ',' + str(actualdist)) copy_topn_results(os.path.join(dbdir, cell), outputDir, outputFilePath, 4) # combined = combine_until_dup(outputFilePaths, 1000) combined = combine_topn_votes(outputFilePaths, float('inf')) # combined = filter_in_range(combined, querysurf) # write_scores(querysurf, combined, "/media/data/combined") [g, y, r, b, o] = check_topn_img(querysurf, combined, topnresults) if copytopmatch: match = g or y or r or b or o copy_top_match(querydir, querysurf.split('surf.npy')[0], combined, match) return [g, y, r, b, o]