def characterizeFuzzy(querydir, dbdir, mainOutputDir, n, copytopmatch):
    start = time.time()
    if not os.path.exists(mainOutputDir):
        os.makedirs(mainOutputDir)
    if copytopmatch:
        if os.path.exists(resultsdir):
            shutil.rmtree(resultsdir)
        os.makedirs(resultsdir)
    files = util.getSiftFileNames(querydir)
    g_count = 0
    y_count = 0
    r_count = 0
    b_count = 0
    o_count = 0
    count = 0
    for queryfile in files:
        for newlat, newlon in skew_location(queryfile, ambiguity):
            closest_cells = util.getclosestcells(newlat, newlon, dbdir)
            count += 1
            [g, y, r, b, o] = query2(querydir, queryfile, dbdir, mainOutputDir, n, copytopmatch, closest_cells, copy_top_n_percell=5)
            if g:
                g_count += 1
                if verbosity > 0:
                    print "G match-g:{0} y:{1} r:{2} b:{3} o:{4} out of {5}".format(g_count, y_count, r_count, b_count, o_count, count)
            elif y:
                y_count += 1
                if verbosity > 0:
                    print "Y match-g:{0} y:{1} r:{2} b:{3} o:{4} out of {5}".format(g_count, y_count, r_count, b_count, o_count, count)
            elif r:
                r_count += 1
                if verbosity > 0:
                    print "R match-g:{0} y:{1} r:{2} b:{3} o:{4} out of {5}".format(g_count, y_count, r_count, b_count, o_count, count)
            elif b:
                b_count += 1
                if verbosity > 0:
                    print "B match-g:{0} y:{1} r:{2} b:{3} o:{4} out of {5}".format(g_count, y_count, r_count, b_count, o_count, count)
            elif o:
                o_count += 1
                if verbosity > 0:
                    print "O match-g:{0} y:{1} r:{2} b:{3} o:{4} out of {5}".format(g_count, y_count, r_count, b_count, o_count, count)
            else:
                if verbosity > 0:
                    print "No match-g:{0} y:{1} r:{2} b:{3} o:{4} out of {5}".format(g_count, y_count, r_count, b_count, o_count, count)
    end = time.time()
    elapsed = end - start
    if verbosity > 0:
        print "total time:{0}, avg time:{1}".format(elapsed, elapsed / count)
    total_count = g_count + y_count + r_count + b_count + o_count
    match_rate = float(total_count) / count
    print "g:{0} y:{1} r:{2} b:{3} o:{4} = {5}, out of {6}={7}".format(g_count, y_count, r_count, b_count, o_count, total_count, count, match_rate)
def weigh_cells(ranked_matches, dbdir, nClosestCells):
    weighted_matches = []
    for matchedimg, score in ranked_matches:
        lat = float(matchedimg.split(",")[0])
        lon = float(matchedimg.split(",")[1][0:-5])
        closest_cells = util.getclosestcells(lat, lon, dbdir)
        cells_in_range = [(cell, dist) for cell, dist in closest_cells[0:nClosestCells] if dist < cellradius]
        numoverlap = len(cells_in_range)
        if numoverlap == 3:
            weighted_matches.append((matchedimg, score * 4))
        elif numoverlap == 4:
            weighted_matches.append((matchedimg, score * 3))
#        else:
#            print "ERROR! weigh_cells has more overlap than it's supposed to: {0}, {1}".format(numoverlap, matchedimg)
    weighted_matches.sort(key=lambda x: x[1], reverse=True)
    return weighted_matches
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]
Esempio n. 4
0
def weigh_cells(ranked_matches, dbdir, nClosestCells):
    weighted_matches = []
    for matchedimg, score in ranked_matches:
        lat = float(matchedimg.split(",")[0])
        lon = float(matchedimg.split(",")[1][0:-5])
        closest_cells = util.getclosestcells(lat, lon, dbdir)
        cells_in_range = [(cell, dist)
                          for cell, dist in closest_cells[0:nClosestCells]
                          if dist < cellradius]
        numoverlap = len(cells_in_range)
        if numoverlap == 3:
            weighted_matches.append((matchedimg, score * 4))
        elif numoverlap == 4:
            weighted_matches.append((matchedimg, score * 3))


#        else:
#            print "ERROR! weigh_cells has more overlap than it's supposed to: {0}, {1}".format(numoverlap, matchedimg)
    weighted_matches.sort(key=lambda x: x[1], reverse=True)
    return weighted_matches
Esempio n. 5
0
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
Esempio n. 6
0
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]