Update audio2text/a2t.py
Browse files- audio2text/a2t.py +13 -6
audio2text/a2t.py
CHANGED
@@ -1,9 +1,10 @@
|
|
|
|
1 |
import numpy as np
|
2 |
-
|
3 |
from .init import pipe
|
4 |
|
5 |
TASK = "transcribe"
|
6 |
BATCH_SIZE = 16
|
|
|
7 |
|
8 |
class A2T:
|
9 |
def __init__(self, mic):
|
@@ -13,12 +14,17 @@ class A2T:
|
|
13 |
if inputs is None:
|
14 |
print("Inputs None")
|
15 |
|
16 |
-
transcribed_text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task, "language": "english"})
|
17 |
-
|
18 |
-
return transcribed_text["text"]
|
19 |
|
20 |
-
def __preprocces(self, raw: np.ndarray):
|
21 |
chunk = raw.astype(np.float32) / 32768.0
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
return chunk
|
23 |
|
24 |
def predict(self):
|
@@ -26,7 +32,8 @@ class A2T:
|
|
26 |
if self.mic is not None:
|
27 |
chunk = self.mic.get_array_of_samples()
|
28 |
chunk = np.array(chunk, dtype=np.int16)
|
29 |
-
|
|
|
30 |
print(f"audio : {audio} \n shape : {audio.shape} \n max : {np.max(audio)}")
|
31 |
else:
|
32 |
raise Exception("please provide audio")
|
|
|
1 |
+
import librosa
|
2 |
import numpy as np
|
|
|
3 |
from .init import pipe
|
4 |
|
5 |
TASK = "transcribe"
|
6 |
BATCH_SIZE = 16
|
7 |
+
LIMIT = 60
|
8 |
|
9 |
class A2T:
|
10 |
def __init__(self, mic):
|
|
|
14 |
if inputs is None:
|
15 |
print("Inputs None")
|
16 |
|
17 |
+
transcribed_text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task, "language": "english"})["text"]
|
18 |
+
return transcribed_text
|
|
|
19 |
|
20 |
+
def __preprocces(self, raw: np.ndarray, sampling_rate: int):
|
21 |
chunk = raw.astype(np.float32) / 32768.0
|
22 |
+
|
23 |
+
if sampling_rate > 16000:
|
24 |
+
chunk = librosa.resample(chunk, orig_sr=sampling_rate, target_sr=16000)
|
25 |
+
|
26 |
+
chunk = chunk[:16000*LIMIT]
|
27 |
+
|
28 |
return chunk
|
29 |
|
30 |
def predict(self):
|
|
|
32 |
if self.mic is not None:
|
33 |
chunk = self.mic.get_array_of_samples()
|
34 |
chunk = np.array(chunk, dtype=np.int16)
|
35 |
+
sampling_rate = self.mic.frame_rate
|
36 |
+
audio = self.__preprocces(raw=chunk, sampling_rate=sampling_rate)
|
37 |
print(f"audio : {audio} \n shape : {audio.shape} \n max : {np.max(audio)}")
|
38 |
else:
|
39 |
raise Exception("please provide audio")
|