def find_matches(track, model):
    track_id = str(track)
    # Extract keypoints
    fp = fingerprint.from_file(
        track.path, model.settings['sr'], track_id, model.settings
    )

    # Find (approximate) nearest neighbors
    distances, indices = ann.find_neighbors(
        model.matcher,
        fp.descriptors,
        algorithm=model.settings['algorithm'],
        k=2
    )

    # Build match  objects
    logger.info('Building match objects')
    matches = []
    for i, distance in enumerate(distances):
        matches.append(Match(
            fp.keypoints[i],
            model.keypoints[indices[i][0]],
            distance[0],
            distance[1]
        ))
    return matches, fp.spectrogram, fp.keypoints
Exemplo n.º 2
0
def find_matches(track, model):
    track_id = str(track)
    # Extract keypoints
    fp = fingerprint.from_file(track.path, model.settings['sr'], track_id,
                               model.settings)

    # Find (approximate) nearest neighbors
    distances, indices = ann.find_neighbors(
        model.matcher,
        fp.descriptors,
        algorithm=model.settings['algorithm'],
        k=20)

    # Build match  objects
    logger.info('Building match objects')
    matches = []
    for i, distance in enumerate(distances):
        neighbors = [
            Neighbor(model.keypoints[index], dist)
            for index, dist in zip(indices[i], distance)
        ]
        matches.append(Match(
            fp.keypoints[i],
            neighbors,
        ))
    return matches, fp.spectrogram, fp.keypoints
def train_keypoints(tracks, hop_length, octave_bins=24, n_octaves=7,
                    fmin=50, sr=22050, algorithm='lshf', dedupe=False,
                    save=None, **kwargs):
    settings = locals().copy()
    for key in settings['kwargs']:
        settings[key] = settings['kwargs'][key]
    del settings['kwargs']
    del settings['tracks']
    logger.info('Settings: {}'.format(settings))
    spectrograms = {}
    keypoints = []
    descriptors = []
    for track in tracks:
        track_id = str(track)
        fp = fingerprint.from_file(track.path, sr, track_id, settings)
        if dedupe:
            # Remove duplicate keypoints
            # (important for ratio thresholding if source track
            # has exact repeated segments)
            fp.remove_similar_keypoints()

        if save:
            path = save_spectrogram(fp.spectrogram, track_id, save)
            spectrograms[track_id] = path
        keypoints.extend(fp.keypoints)
        descriptors.extend(fp.descriptors)
    descriptors = np.vstack(descriptors)
    matcher = ann.train_matcher(descriptors, algorithm=algorithm)
    model = Model(matcher, keypoints, settings)
    model.spectrograms = spectrograms
    if save:
        save_model(model, save)
    return model
Exemplo n.º 4
0
def train_keypoints(tracks,
                    hop_length,
                    octave_bins=24,
                    n_octaves=7,
                    fmin=50,
                    sr=22050,
                    algorithm='lshf',
                    dedupe=False,
                    save=None,
                    save_spec=False,
                    **kwargs):
    settings = locals().copy()
    for key in settings['kwargs']:
        settings[key] = settings['kwargs'][key]
    del settings['kwargs']
    del settings['tracks']
    logger.info('Settings: {}'.format(settings))
    spectrograms = {}
    keypoints = []
    descriptors = []
    model_tracks = {}
    for track in tracks:
        track_id = str(track)
        model_tracks[track_id] = track
        fp = fingerprint.from_file(track.path, sr, track_id, settings)
        if dedupe:
            # Remove duplicate keypoints
            # (important for ratio thresholding if source track
            # has exact repeated segments)
            fp.remove_similar_keypoints()

        if save_spec:
            path = save_spectrogram(fp.spectrogram, track_id, save)
            spectrograms[track_id] = path
        keypoints.extend(fp.keypoints)
        descriptors.extend(fp.descriptors)
    descriptors = np.vstack(descriptors)
    matcher = ann.train_matcher(descriptors, algorithm=algorithm)
    descriptors = None
    model = Model(matcher, keypoints, settings, tracks=model_tracks)
    model.spectrograms = spectrograms
    if save:
        save_model(model, save)
    return model