初めに
STTやTTSの学習等をする際に文字データが必要になるのですが、音声だけある場合もよくあるので文字お越しが必要になります。
今回はreazon-research/reazonspeech-nemo-v2を使ってフォルダ内の音声の文字起こしを行っていきます
精度及び速度はwhisperよりもいいとのことです
(公式サイトより引用)
デモ
最終的に以下が出力されます
CSVは以下のように出力されます
ファイル名,テキスト内容,音声の長さ test1.wav,test1,4.4 test1.wav,test2,6.7
Jsonは以下のように出力されます
[ { "ファイル名": "test1.wav", "テキスト内容": "test1", "音声の長さ": 4.4 }, { "ファイル名": "test2.wav", "テキスト内容": "test2", "音声の長さ": 6.7 } ]
コードはGistにも置いてあります
gistb5854648aed368c0ce1586b73b36ea66
開発環境
- Windows 11
- cuda 12.1
準備
まずは必要なライブラリを入れていきます
pip install Cython
# インストール
git clone https://github.com/reazon-research/ReazonSpeech
pip install ReazonSpeech/pkg/nemo-asr
またWindowsの場合は、ffmpegをGitHubからダウンロードをします。
(linuxの場合は、 sudo apt install ffmpeg
で大丈夫です)
また展開後のパスを環境変数に追加します。
文字お越し及びファイル保存
以下のスクリプトでフォルダ内の音声ファイルの文字起こし及びCSVとjsonファイルに保存していきます
import glob import os from reazonspeech.nemo.asr import load_model, transcribe, audio_from_path import csv import json import librosa model = load_model(device='cuda') # 対象のディレクトリパス directory_path = 'root path' # 指定されたディレクトリ以下のすべてのwavファイルのパスを取得 wav_files = glob.glob(os.path.join(directory_path + "\\train", '**', '*.mp3'), recursive=True) # CSVファイルとJSONファイルのパスを指定 csv_file_path = f'{directory_path}\\transcription_results.csv' json_file_path = f'{directory_path}\\transcription_results.json' # 結果を格納するためのリスト results = [] # 全体のファイル数を取得 total_files = len(wav_files) # CSVファイルに保存 with open(csv_file_path, mode='w', newline='', encoding='utf-8') as csv_file: writer = csv.writer(csv_file) writer.writerow(['ファイル名', 'テキスト内容', '音声の長さ']) for index, file_path in enumerate(wav_files, start=1): # ローカルの音声ファイルを読み込む audio = audio_from_path(file_path) # 音声認識を適用する ret = transcribe(model, audio).text # 音声の長さを取得 duration = librosa.get_duration(filename=file_path) # ファイルパスからファイル名のみを抽出 file_name = os.path.basename(file_path) print(f"{index}/{total_files}") # 結果をリストに追加 results.append({'ファイル名': file_name, 'テキスト内容': ret, '音声の長さ': duration}) # CSVに書き込む writer.writerow([file_name, ret, duration]) # JSONファイルに保存 with open(json_file_path, 'w', encoding='utf-8') as json_file: json.dump(results, json_file, ensure_ascii=False, indent=4)
エラー対応
CUDAがenabledになっていない場合
以下のようなエラーが出た場合は、cudaのverとtool kitのverを合わせます
raise AssertionError("Torch not compiled with CUDA enabled") AssertionError: Torch not compiled with CUDA enabled
以下でcuda toolkitのverを確認します
nvcc --version
以下のようなログが表示されます
nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2023 NVIDIA Corporation Built on Wed_Nov_22_10:30:42_Pacific_Standard_Time_2023 Cuda compilation tools, release 12.3, V12.3.107 Build cuda_12.3.r12.3/compiler.33567101_0
このときにpytorchが12.1対応でcuda toolkitが12.3なので、以下のcuda toolkit公式から12.1をダウンロードしてインストールをします
ReazonSpeech/pkg/nemo-asr
のインストールが失敗する
インストールする際に以下のようなエラーが出る場合についてです。
ModuleNotFoundError: No module named 'Cython' [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error
この場合、新しく仮想環境を作って以下を再度実行します
git clone https://github.com/reazon-research/ReazonSpeech pip install ReazonSpeech/pkg/nemo-asr
またこれでも解決しない場合は、torchを再インストールします
pip uninstall torch torchvision torchaudio
cuda 12.1にあるtorchをインストールします
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121