swdq commited on
Commit
2f42654
·
verified ·
1 Parent(s): a2098f8

Upload asr.py

Browse files
Files changed (1) hide show
  1. asr.py +61 -37
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, # 繰り返しのn-gramサイズ
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", # CUDA(GPU)を使って処理
22
- torch_dtype=torch.float16, # 精度をfloat16に設定(メモリ効率向上)
23
- chunk_length_s=30.0, # 30秒単位で音声を分割
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
- start_index = 0
35
  if os.path.exists(csv_file):
36
  with open(csv_file, mode="r", encoding="utf-8") as file:
37
- reader = csv.reader(file)
38
- next(reader, None) # ヘッダーをスキップ
39
- start_index = sum(1 for _ in reader) # 行数をカウント
40
 
41
- # CSVファイルを開く(追記モード)
 
42
  with open(csv_file, mode="a", newline="", encoding="utf-8") as file:
43
  writer = csv.writer(file)
44
 
45
- # ヘッダーが存在しない場合のみ書き込む
46
- if start_index == 0:
47
- writer.writerow(["True", "ASR"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- # トレーニングデータを一括でスキップして処理を再開
50
- for index, example in islice(enumerate(dataset["train"]), start_index, None):
51
- # ASR パイプラインを使って音声データを文字起こし
52
- result = pipe(example["ogg"]["array"], generate_kwargs=generate_kwargs)
53
 
54
- # 認識結果(文字起こし)を表示
55
- true_text = example["txt"]
56
- asr_result = result["text"]
 
 
57
 
58
- # 結果をCSVファイルに書き込み
59
- writer.writerow([true_text, asr_result])
60
- print("True:", true_text)
61
- print("ASR :", asr_result)
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} に保存されました。")