Upload asr.py
Browse files
asr.py
CHANGED
@@ -1,64 +1,88 @@
|
|
1 |
import torch
|
2 |
import csv
|
3 |
-
from datasets import load_dataset
|
4 |
from transformers import pipeline
|
5 |
import os
|
|
|
6 |
from itertools import islice
|
7 |
|
|
|
|
|
|
|
8 |
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
9 |
|
10 |
-
# 音声認識のための generate_kwargs を定義
|
11 |
generate_kwargs = {
|
12 |
-
"language": "Japanese",
|
13 |
-
"no_repeat_ngram_size": 0,
|
14 |
-
"repetition_penalty": 1.0,
|
15 |
}
|
16 |
|
17 |
-
# "anime-whisper" モデルを使った ASR パイプラインの初期化
|
18 |
pipe = pipeline(
|
19 |
-
"automatic-speech-recognition",
|
20 |
-
model="litagin/anime-whisper",
|
21 |
-
device="cuda",
|
22 |
-
torch_dtype=torch.float16,
|
23 |
-
chunk_length_s=30.0,
|
24 |
-
batch_size=64,
|
25 |
)
|
26 |
|
27 |
-
# データセットをロード
|
28 |
-
dataset = load_dataset("litagin/Galgame_Speech_ASR_16kHz", streaming=True)
|
29 |
-
|
30 |
# CSVファイルの準備
|
31 |
-
csv_file = "speech_recognition_results.csv"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
if os.path.exists(csv_file):
|
36 |
with open(csv_file, mode="r", encoding="utf-8") as file:
|
37 |
-
reader = csv.
|
38 |
-
|
39 |
-
|
40 |
|
41 |
-
#
|
|
|
42 |
with open(csv_file, mode="a", newline="", encoding="utf-8") as file:
|
43 |
writer = csv.writer(file)
|
44 |
|
45 |
-
#
|
46 |
-
if
|
47 |
-
writer.writerow(["True", "ASR"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
# ASR パイプラインを使って音声データを文字起こし
|
52 |
-
result = pipe(example["ogg"]["array"], generate_kwargs=generate_kwargs)
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
-
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
print()
|
63 |
|
64 |
print(f"結果は {csv_file} に保存されました。")
|
|
|
1 |
import torch
|
2 |
import csv
|
|
|
3 |
from transformers import pipeline
|
4 |
import os
|
5 |
+
import librosa
|
6 |
from itertools import islice
|
7 |
|
8 |
+
OVER_SIZE_LIMIT = 200_000_000
|
9 |
+
|
10 |
+
csv.field_size_limit(OVER_SIZE_LIMIT)
|
11 |
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
12 |
|
|
|
13 |
generate_kwargs = {
|
14 |
+
"language": "Japanese",
|
15 |
+
"no_repeat_ngram_size": 0,
|
16 |
+
"repetition_penalty": 1.0,
|
17 |
}
|
18 |
|
|
|
19 |
pipe = pipeline(
|
20 |
+
"automatic-speech-recognition",
|
21 |
+
model="litagin/anime-whisper",
|
22 |
+
device="cuda",
|
23 |
+
torch_dtype=torch.float16,
|
24 |
+
chunk_length_s=30.0,
|
25 |
+
batch_size=64,
|
26 |
)
|
27 |
|
|
|
|
|
|
|
28 |
# CSVファイルの準備
|
29 |
+
csv_file = r"C:\Users\user\Pictures\speech_recognition_results.csv"
|
30 |
+
|
31 |
+
# transcript.csvからデータを読み込む
|
32 |
+
transcripts = {}
|
33 |
+
with open(r"C:\Users\user\Pictures\transcript.csv", mode="r", encoding="utf-8") as file:
|
34 |
+
reader = csv.DictReader(file)
|
35 |
+
for row in reader:
|
36 |
+
transcripts[row["filename"]] = row["transcript"]
|
37 |
+
|
38 |
+
audio_dir = r"C:\Users\user\Pictures\dataset_converted"
|
39 |
+
audio_files = os.listdir(audio_dir)
|
40 |
|
41 |
+
# CSV から既に処理されたファイル名を取得
|
42 |
+
processed_files = set()
|
43 |
if os.path.exists(csv_file):
|
44 |
with open(csv_file, mode="r", encoding="utf-8") as file:
|
45 |
+
reader = csv.DictReader(file)
|
46 |
+
for row in reader:
|
47 |
+
processed_files.add(row["Filename"])
|
48 |
|
49 |
+
# 処理を開始
|
50 |
+
batch_size = 256 # バッチサイズ
|
51 |
with open(csv_file, mode="a", newline="", encoding="utf-8") as file:
|
52 |
writer = csv.writer(file)
|
53 |
|
54 |
+
# ヘッダーがない場合は書き込む
|
55 |
+
if not processed_files:
|
56 |
+
writer.writerow(["Filename", "True", "ASR"])
|
57 |
+
|
58 |
+
# 未処理ファイルをフィルタリング
|
59 |
+
unprocessed_files = [
|
60 |
+
f for f in audio_files if f in transcripts and f not in processed_files
|
61 |
+
]
|
62 |
+
|
63 |
+
# バッチ処理
|
64 |
+
for i in range(0, len(unprocessed_files), batch_size):
|
65 |
+
batch_files = unprocessed_files[i : i + batch_size]
|
66 |
+
audio_paths = [os.path.join(audio_dir, f) for f in batch_files]
|
67 |
+
|
68 |
+
# 音声ファイルを読み込み
|
69 |
+
audios = []
|
70 |
+
for audio_path in audio_paths:
|
71 |
+
y, sr = librosa.load(audio_path, sr=16000)
|
72 |
+
audios.append(y)
|
73 |
|
74 |
+
# ASR パイプラインを使ってバッチ処理
|
75 |
+
results = pipe(audios, generate_kwargs=generate_kwargs)
|
|
|
|
|
76 |
|
77 |
+
# CSVに結果を保存
|
78 |
+
for audio_file, result in zip(batch_files, results):
|
79 |
+
asr_result = result["text"]
|
80 |
+
true_text = transcripts[audio_file]
|
81 |
+
writer.writerow([audio_file, true_text, asr_result])
|
82 |
|
83 |
+
print("Filename:", audio_file)
|
84 |
+
print("True:", true_text)
|
85 |
+
print("ASR :", asr_result)
|
86 |
+
print()
|
|
|
87 |
|
88 |
print(f"結果は {csv_file} に保存されました。")
|