fix script
Browse files- sbtal_riksdag_asr.py +15 -8
sbtal_riksdag_asr.py
CHANGED
|
@@ -16,14 +16,16 @@
|
|
| 16 |
|
| 17 |
# Lint as: python3
|
| 18 |
"""Datasets loader to create the Riksdag data"""
|
|
|
|
| 19 |
from pathlib import Path
|
| 20 |
from pydub import AudioSegment
|
|
|
|
| 21 |
|
| 22 |
import datasets
|
| 23 |
from datasets.tasks import AutomaticSpeechRecognition
|
| 24 |
from datasets.features import Audio
|
| 25 |
|
| 26 |
-
ALIGNMENTS = Path("alignments")
|
| 27 |
TMP = Path("/tmp")
|
| 28 |
parameters=["-ac", "1", "-acodec", "pcm_s16le", "-ar", "16000"]
|
| 29 |
|
|
@@ -37,6 +39,7 @@ class RDDataset(datasets.GeneratorBasedBuilder):
|
|
| 37 |
def _info(self):
|
| 38 |
features = datasets.Features(
|
| 39 |
{
|
|
|
|
| 40 |
"audio": datasets.Audio(sampling_rate=16_000),
|
| 41 |
"text": datasets.Value("string"),
|
| 42 |
}
|
|
@@ -62,13 +65,13 @@ class RDDataset(datasets.GeneratorBasedBuilder):
|
|
| 62 |
]
|
| 63 |
|
| 64 |
def _generate_examples(self, split):
|
| 65 |
-
for
|
| 66 |
-
|
| 67 |
-
with open(str(
|
| 68 |
for line in alignment.readlines():
|
| 69 |
if line.startswith("FILE"):
|
| 70 |
continue
|
| 71 |
-
parts = line.split("\t")
|
| 72 |
if parts[3] == "MISALIGNED":
|
| 73 |
continue
|
| 74 |
vidid = parts[0]
|
|
@@ -76,21 +79,25 @@ class RDDataset(datasets.GeneratorBasedBuilder):
|
|
| 76 |
if Path(temp_wav).exists():
|
| 77 |
audio = AudioSegment.from_wav(temp_wav)
|
| 78 |
else:
|
| 79 |
-
video_file = Path("/sbtal/riksdag-video
|
| 80 |
if video_file.exists():
|
| 81 |
vid_as = AudioSegment.from_file(str(video_file), "mp4")
|
| 82 |
vid_as.export(temp_wav, format="wav", parameters=parameters)
|
| 83 |
audio = AudioSegment.from_wav(temp_wav)
|
| 84 |
else:
|
| 85 |
continue
|
|
|
|
| 86 |
start = int(float(parts[1]) * 1000)
|
| 87 |
end = int(float(parts[2]) * 1000)
|
| 88 |
text = parts[4]
|
| 89 |
-
|
|
|
|
| 90 |
"id": vidid,
|
| 91 |
"audio": {
|
| 92 |
-
"array": audio[start:end],
|
| 93 |
"sampling_rate": 16_000
|
| 94 |
},
|
| 95 |
"text": text
|
| 96 |
}
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Lint as: python3
|
| 18 |
"""Datasets loader to create the Riksdag data"""
|
| 19 |
+
# This script is full of local paths; sorry about that
|
| 20 |
from pathlib import Path
|
| 21 |
from pydub import AudioSegment
|
| 22 |
+
import numpy as np
|
| 23 |
|
| 24 |
import datasets
|
| 25 |
from datasets.tasks import AutomaticSpeechRecognition
|
| 26 |
from datasets.features import Audio
|
| 27 |
|
| 28 |
+
ALIGNMENTS = Path("/home/joregan/sbtal_riksdag_asr/alignments")
|
| 29 |
TMP = Path("/tmp")
|
| 30 |
parameters=["-ac", "1", "-acodec", "pcm_s16le", "-ar", "16000"]
|
| 31 |
|
|
|
|
| 39 |
def _info(self):
|
| 40 |
features = datasets.Features(
|
| 41 |
{
|
| 42 |
+
"id": datasets.Value("string"),
|
| 43 |
"audio": datasets.Audio(sampling_rate=16_000),
|
| 44 |
"text": datasets.Value("string"),
|
| 45 |
}
|
|
|
|
| 65 |
]
|
| 66 |
|
| 67 |
def _generate_examples(self, split):
|
| 68 |
+
for afile in ALIGNMENTS.glob("*"):
|
| 69 |
+
temp_wav = ""
|
| 70 |
+
with open(str(afile)) as alignment:
|
| 71 |
for line in alignment.readlines():
|
| 72 |
if line.startswith("FILE"):
|
| 73 |
continue
|
| 74 |
+
parts = line.strip().split("\t")
|
| 75 |
if parts[3] == "MISALIGNED":
|
| 76 |
continue
|
| 77 |
vidid = parts[0]
|
|
|
|
| 79 |
if Path(temp_wav).exists():
|
| 80 |
audio = AudioSegment.from_wav(temp_wav)
|
| 81 |
else:
|
| 82 |
+
video_file = Path("/sbtal/riksdag-video") / f"{parts[0]}_480p.mp4"
|
| 83 |
if video_file.exists():
|
| 84 |
vid_as = AudioSegment.from_file(str(video_file), "mp4")
|
| 85 |
vid_as.export(temp_wav, format="wav", parameters=parameters)
|
| 86 |
audio = AudioSegment.from_wav(temp_wav)
|
| 87 |
else:
|
| 88 |
continue
|
| 89 |
+
print(parts)
|
| 90 |
start = int(float(parts[1]) * 1000)
|
| 91 |
end = int(float(parts[2]) * 1000)
|
| 92 |
text = parts[4]
|
| 93 |
+
piece_id = f"{vidid}_{start}_{end}"
|
| 94 |
+
yield piece_id, {
|
| 95 |
"id": vidid,
|
| 96 |
"audio": {
|
| 97 |
+
"array": np.array(audio[start:end].get_array_of_samples()),
|
| 98 |
"sampling_rate": 16_000
|
| 99 |
},
|
| 100 |
"text": text
|
| 101 |
}
|
| 102 |
+
if temp_wav != "":
|
| 103 |
+
Path.unlink(temp_wav)
|