reconnaissance_audio
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| reconnaissance_audio [2024/05/23 15:57] – créée joseph | reconnaissance_audio [2024/05/23 15:58] (Version actuelle) – joseph | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| Test: | Test: | ||
| - | import numpy as np, scipy.signal, | + | |
| - | spectrogram = lambda x: 20 * np.log10(np.abs(scipy.signal.stft(x, | + | spectrogram = lambda x: 20 * np.log10(np.abs(scipy.signal.stft(x, |
| - | A = 0 | + | A = 0 |
| - | def add_constellations(s, | + | def add_constellations(s, |
| - | global A | + | global A |
| - | global_peaks = [] | + | global_peaks = [] |
| - | for t in range(s.shape[1]): | + | for t in range(s.shape[1]): |
| - | peaks, prop = scipy.signal.find_peaks(s[:, | + | peaks, prop = scipy.signal.find_peaks(s[:, |
| - | global_peaks += [(t, f) for _, f in sorted(zip(prop[" | + | global_peaks += [(t, f) for _, f in sorted(zip(prop[" |
| - | for i1, (t1, f1) in enumerate(global_peaks): | + | for i1, (t1, f1) in enumerate(global_peaks): |
| - | for (t2, f2) in global_peaks[i1 + 1:i1 + 20]: | + | for (t2, f2) in global_peaks[i1 + 1:i1 + 20]: |
| - | delta_t = t2 - t1 | + | delta_t = t2 - t1 |
| - | if delta_t <= 1 or delta_t >= 4: | + | if delta_t <= 1 or delta_t >= 4: |
| - | continue | + | continue |
| - | h = f1 + (f2 << 8) + (delta_t << 16) | + | h = f1 + (f2 << 8) + (delta_t << 16) |
| - | hash_table[h].append((t1, | + | hash_table[h].append((t1, |
| - | A += 1 | + | A += 1 |
| - | def create_library(path, | + | def create_library(path, |
| - | hash_table, songs = collections.defaultdict(list), | + | hash_table, songs = collections.defaultdict(list), |
| - | for i, f in enumerate(glob.glob(path)): | + | for i, f in enumerate(glob.glob(path)): |
| - | print(f" | + | print(f" |
| - | songs.append(os.path.basename(f)) | + | songs.append(os.path.basename(f)) |
| - | p = subprocess.Popen([FFMPEG_PATH, | + | p = subprocess.Popen([FFMPEG_PATH, |
| - | x = np.frombuffer(p.communicate()[0], | + | x = np.frombuffer(p.communicate()[0], |
| - | s = spectrogram(x)[: | + | s = spectrogram(x)[: |
| - | add_constellations(s, | + | add_constellations(s, |
| - | if i == 200: | + | if i == 200: |
| - | break | + | break |
| - | with open(db_filename, | + | with open(db_filename, |
| - | pickle.dump({" | + | pickle.dump({" |
| - | print(" | + | print(" |
| - | def load_library(db_filename=" | + | def load_library(db_filename=" |
| - | return pickle.load(open(db_filename, | + | return pickle.load(open(db_filename, |
| - | def recognize(f, | + | def recognize(f, |
| - | sr, x = scipy.io.wavfile.read(f) | + | sr, x = scipy.io.wavfile.read(f) |
| - | s = spectrogram(x) | + | s = spectrogram(x) |
| - | recording_hash_table = collections.defaultdict(list) | + | recording_hash_table = collections.defaultdict(list) |
| - | matches_per_song = collections.defaultdict(list) | + | matches_per_song = collections.defaultdict(list) |
| - | scores = collections.defaultdict(int) | + | scores = collections.defaultdict(int) |
| - | add_constellations(s, | + | add_constellations(s, |
| - | for h, T in recording_hash_table.items(): | + | for h, T in recording_hash_table.items(): |
| - | for t1 in T: | + | for t1 in T: |
| - | for (t0, song_id) in db[" | + | for (t0, song_id) in db[" |
| - | matches_per_song[song_id].append((h, | + | matches_per_song[song_id].append((h, |
| - | for song_id, matches in matches_per_song.items(): | + | for song_id, matches in matches_per_song.items(): |
| - | song_scores_by_offset = collections.defaultdict(int) | + | song_scores_by_offset = collections.defaultdict(int) |
| - | for h, t1, t0 in matches: | + | for h, t1, t0 in matches: |
| - | song_scores_by_offset[t0 - t1] += 1 | + | song_scores_by_offset[t0 - t1] += 1 |
| - | scores[song_id] = max(song_scores_by_offset.items(), | + | scores[song_id] = max(song_scores_by_offset.items(), |
| - | scores = sorted(scores.items(), | + | scores = sorted(scores.items(), |
| - | print(f, db[" | + | print(f, db[" |
| - | # create_library(r" | + | # create_library(r" |
| - | db = load_library(" | + | db = load_library(" |
| - | recognize(" | + | recognize(" |
reconnaissance_audio.1716479868.txt.gz · Dernière modification : de joseph
