Update audio2text/a2t.py
Browse files- audio2text/a2t.py +55 -39
audio2text/a2t.py
CHANGED
@@ -1,53 +1,69 @@
|
|
1 |
-
import librosa
|
2 |
import numpy as np
|
3 |
-
|
4 |
|
5 |
-
TASK = "transcribe"
|
6 |
-
BATCH_SIZE = 8
|
7 |
-
LIMIT = 60
|
8 |
-
SAMPLING_RATE = 16000
|
9 |
|
10 |
-
class A2T:
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
|
22 |
|
23 |
-
|
24 |
|
25 |
-
|
26 |
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
|
31 |
|
32 |
-
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
|
|
|
|
1 |
import numpy as np
|
2 |
+
import speech_recognition as sr
|
3 |
|
4 |
+
# TASK = "transcribe"
|
5 |
+
# BATCH_SIZE = 8
|
6 |
+
# LIMIT = 60
|
7 |
+
# SAMPLING_RATE = 16000
|
8 |
|
9 |
+
# class A2T:
|
10 |
+
# def __init__(self, mic):
|
11 |
+
# self.mic = mic
|
12 |
|
13 |
+
# def __transcribe(self, inputs, task: str = None):
|
14 |
+
# if inputs is None:
|
15 |
+
# print("Inputs None")
|
16 |
|
17 |
+
# transcribed_text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
18 |
+
# return transcribed_text
|
19 |
|
20 |
+
# def __preprocces(self, raw: np.ndarray, sampling_rate: int):
|
21 |
|
22 |
+
# chunk = raw.astype(np.float32, order='C') / 32768.0
|
23 |
|
24 |
+
# print(f"Chunk : {chunk} max chunk : {np.max(chunk)}")
|
25 |
|
26 |
+
# if len(chunk.shape) > 1:
|
27 |
+
# chunk = librosa.to_mono(chunk.T)
|
28 |
|
29 |
+
# chunk = chunk[:SAMPLING_RATE*LIMIT]
|
30 |
|
31 |
+
# return chunk
|
32 |
|
33 |
+
# def predict(self):
|
34 |
+
# try:
|
35 |
+
# if self.mic is not None:
|
36 |
+
# raw = self.mic.get_array_of_samples()
|
37 |
+
# chunk = np.array(raw, dtype=np.int16)
|
38 |
+
# sampling_rate = self.mic.frame_rate
|
39 |
+
# audio = self.__preprocces(raw=chunk, sampling_rate=sampling_rate)
|
40 |
+
# print(f"audio : {audio} \n shape : {audio.shape} \n max : {np.max(audio)} \n shape of chunk : {chunk.shape} \n sampling rate : {sampling_rate} \n max chunk : {np.max(chunk)} \n chunk : {chunk}")
|
41 |
+
# else:
|
42 |
+
# raise Exception("please provide audio")
|
43 |
+
|
44 |
+
# if isinstance(audio , np.ndarray):
|
45 |
+
# inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
|
46 |
+
# return self.__transcribe(inputs=inputs, task=TASK)
|
47 |
+
# else:
|
48 |
+
# raise Exception("Audio is not np array")
|
49 |
|
50 |
+
# except Exception as e:
|
51 |
+
# return f"Oops some kinda error : {e}"
|
52 |
+
|
53 |
+
|
54 |
+
class A2T:
|
55 |
+
def get_text(self):
|
56 |
+
# obtain audio from the microphone
|
57 |
+
r = sr.Recognizer()
|
58 |
+
with sr.Microphone() as source:
|
59 |
+
print("Say something!")
|
60 |
+
audio = r.listen(source)
|
61 |
+
|
62 |
+
# recognize speech using Sphinx
|
63 |
+
try:
|
64 |
+
return r.recognize_sphinx(audio)
|
65 |
+
except sr.UnknownValueError:
|
66 |
+
raise Exception("Sphinx could not understand audio")
|
67 |
+
except sr.RequestError as e:
|
68 |
+
raise Exception("Sphinx error; {0}".format(e))
|
69 |
|