Esempio n. 1
0
def sift_match(path1, path2, hop_length, abs_thresh=None, ratio_thresh=None, octave_bins=24, n_octaves=9, fmin=40, timeframe=20, cluster_size=1, sr=22050):
    # Extract keypoints
    kp1, desc1, img1 = sift_file(path1, hop_length, octave_bins=octave_bins, n_octaves=n_octaves, fmin=fmin, sr=sr)
    kp2, desc2, img2 = sift_file(path2, hop_length, octave_bins=octave_bins, n_octaves=n_octaves, fmin=fmin, sr=sr)

    # Plot keypoint images
    fig = plt.figure()
    ax1 = fig.add_subplot(2, 1, 1)
    plot_keypoint_image(img1, hop_length, octave_bins, fmin, path1, sr=sr)
    ax2 = fig.add_subplot(2, 1, 2)
    plot_keypoint_image(img2, hop_length, octave_bins, fmin, path2, sr=sr)
   # mng = plt.get_current_fig_manager()
   # mng.full_screen_toggle()

    # BFMatcher with default params
    #bf = cv2.BFMatcher()
    #matches = bf.knnMatch(desc1, desc2, k=2)
    distances, indices = ann.nearest_neighbors(desc1, desc2, k=2)

    # Build match  objects
    logger.info('Building match objects')
    matches = []
    for i, distance in enumerate(distances):
        matches.append(Match(kp1[i], kp2[indices[i][0]], distance[0], distance[1]))

    # Filter nearest neighbors
    logger.info('Filtering nearest neighbors down to actual matched samples')
    timeframe = int((sr/ hop_length) * timeframe)
    matches = filter_matches(matches, abs_thresh, ratio_thresh, timeframe, cluster_size)

    # Draw matching lines
    plot_matches(ax1, ax2, matches)
    plt.show(block=False)
    return matches
Esempio n. 2
0
def sift_match(
    path1,
    path2,
    hop_length,
    abs_thresh=None,
    ratio_thresh=None,
    octave_bins=24,
    n_octaves=9,
    fmin=40,
    timeframe=20,
    cluster_size=1,
    sr=22050,
):
    # Extract keypoints
    kp1, desc1, img1 = sift_file(path1,
                                 hop_length,
                                 octave_bins=octave_bins,
                                 n_octaves=n_octaves,
                                 fmin=fmin,
                                 sr=sr)
    kp2, desc2, img2 = sift_file(path2,
                                 hop_length,
                                 octave_bins=octave_bins,
                                 n_octaves=n_octaves,
                                 fmin=fmin,
                                 sr=sr)

    # Plot keypoint images
    fig = plt.figure()
    ax1 = fig.add_subplot(2, 1, 1)
    plot_keypoint_image(img1, hop_length, octave_bins, fmin, path1, sr=sr)
    ax2 = fig.add_subplot(2, 1, 2)
    plot_keypoint_image(img2, hop_length, octave_bins, fmin, path2, sr=sr)
    # mng = plt.get_current_fig_manager()
    # mng.full_screen_toggle()

    # BFMatcher with default params
    # bf = cv2.BFMatcher()
    # matches = bf.knnMatch(desc1, desc2, k=2)
    distances, indices = ann.nearest_neighbors(desc1, desc2, k=2)

    # Build match  objects
    logger.info("Building match objects")
    matches = []
    for i, distance in enumerate(distances):
        matches.append(
            Match(kp1[i], kp2[indices[i][0]], distance[0], distance[1]))

    # Filter nearest neighbors
    logger.info("Filtering nearest neighbors down to actual matched samples")
    timeframe = int((sr / hop_length) * timeframe)
    matches = filter_matches(matches, abs_thresh, ratio_thresh, timeframe,
                             cluster_size)

    # Draw matching lines
    plot_matches(ax1, ax2, matches)
    plt.show(block=False)
    return matches
Esempio n. 3
0
def match(path1, path2, hop_length, wn=0.4, num_peaks=5, thresh=0.001, plot=True):
    target_peaks = num_peaks + 2
    fig = plt.figure()
    ax1 = fig.add_subplot(2, 1, 1)
    S1, peaks1 = spectral_peaks(path1, hop_length, wn=wn, num_peaks=target_peaks, plot=plot)
    ax2 = fig.add_subplot(2, 1, 2)
    S2, peaks2 = spectral_peaks(path2, hop_length, wn=wn, num_peaks=num_peaks, plot=plot)
    mng = plt.get_current_fig_manager()
    mng.full_screen_toggle()

    logger.info("Finding peak distances...")
    peak_dists2 = np.diff(peaks2, axis=0)

    num_combos = len(list(itertools.combinations(range(target_peaks), num_peaks)))
    peak_dists1 = np.empty([num_peaks - 1, peaks1.shape[1] * (num_combos + 1)], dtype=int)
    for x, frame in enumerate(peaks1.T):
        start = x * (num_combos + 1)
        end = (x + 1) * (num_combos + 1) - 1
        peak_dists1[:, start:end] = np.array([np.diff(combo) for combo in itertools.combinations(frame, num_peaks)]).T

    logger.info("Finding nearest neighbors")
    distances, nearest_neighbors = ann.nearest_neighbors(peak_dists2.T, peak_dists1.T, k=1)
    # nearest_neighbors = []
    # for frame in peak_dists1:
    #     score = 0
    #     nn = None
    #     for i, yframe in enumerate(peak_dists2):
    #         count = count_matches(frame, yframe)
    #         if count > score:
    #             score = count
    #             nn = i
    #     nearest_neighbors.append({'score': score, 'index': nn})

    logger.info("Drawing lines between matches")
    for x, nn in enumerate(nearest_neighbors[:-1]):
        if distances[x] < thresh:
            if any(n in nearest_neighbors[x + 1 : x + (num_combos * 3)] for n in [nn + 1, nn + 2, nn + 3]):
                con = ConnectionPatch(
                    xyA=(x / num_combos, 0),
                    xyB=(nn, S2.shape[0]),
                    coordsA="data",
                    coordsB="data",
                    axesA=ax1,
                    axesB=ax2,
                    arrowstyle="<-",
                    linewidth=1,
                    zorder=999,
                )
                ax1.add_artist(con)
                ax2.set_zorder(-1)
    plt.show(block=False)
    return nearest_neighbors, distances
def match(path1, path2, hop_length, wn=0.4, num_peaks=5, thresh=0.001, plot=True):
    target_peaks = num_peaks + 2
    fig = plt.figure()
    ax1 = fig.add_subplot(2, 1, 1)
    S1, peaks1 = spectral_peaks(path1, hop_length, wn=wn, num_peaks=target_peaks, plot=plot)
    ax2 = fig.add_subplot(2, 1, 2)
    S2, peaks2 = spectral_peaks(path2, hop_length, wn=wn, num_peaks=num_peaks, plot=plot)
    mng = plt.get_current_fig_manager()
    mng.full_screen_toggle()

    logger.info('Finding peak distances...')
    peak_dists2 = np.diff(peaks2, axis=0)

    num_combos = len(list(itertools.combinations(range(target_peaks), num_peaks)))
    peak_dists1 = np.empty([num_peaks-1, peaks1.shape[1]*(num_combos+1)], dtype=int)
    for x, frame in enumerate(peaks1.T):
        start = x*(num_combos+1)
        end = (x+1)*(num_combos+1) - 1
        peak_dists1[:, start:end] = np.array([np.diff(combo) for combo in itertools.combinations(frame, num_peaks)]).T

    logger.info('Finding nearest neighbors')
    distances, nearest_neighbors = ann.nearest_neighbors(peak_dists2.T, peak_dists1.T, k=1)
   # nearest_neighbors = []
   # for frame in peak_dists1:
   #     score = 0
   #     nn = None
   #     for i, yframe in enumerate(peak_dists2):
   #         count = count_matches(frame, yframe)
   #         if count > score:
   #             score = count
   #             nn = i
   #     nearest_neighbors.append({'score': score, 'index': nn})

    logger.info('Drawing lines between matches')
    for x, nn in enumerate(nearest_neighbors[:-1]):
        if distances[x] < thresh:
            if any(n in nearest_neighbors[x+1:x+(num_combos*3)] for n in [nn+1, nn+2, nn+3]):
                con = ConnectionPatch(
                    xyA=(x/num_combos, 0), xyB=(nn, S2.shape[0]),
                    coordsA='data', coordsB='data',
                    axesA=ax1, axesB=ax2,
                    arrowstyle='<-', linewidth=1,
                    zorder=999
                )
                ax1.add_artist(con)
                ax2.set_zorder(-1)
    plt.show(block=False)
    return nearest_neighbors, distances