-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_recording.py
74 lines (48 loc) · 1.79 KB
/
make_recording.py
1
2
3
4
5
6
7
8
9
10
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import speech_recognition as sr
from gcloud import storage
from google.cloud import speech
import soundfile as sf
from oauth2client.service_account import ServiceAccountCredentials
import re
import io
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./speech-recog-363719-f9d84d991d98.json"
# r = sr.Recognizer()
# r.pause_threshold = 8000
# # getting a recording
# with sr.Microphone() as source:
# print("Say something!")
# audio = r.listen(source, timeout=2, phrase_time_limit=10)
# with open("./audio_files/evi.wav", "wb") as f:
# f.write(audio.get_wav_data(convert_rate=48000))
def speech_to_text(config, audio):
client = speech.SpeechClient()
response = client.recognize(config=config, audio=audio)
results = response.results
confidences = []
for i in results:
temp = re.findall(r'\d+', str(i.alternatives[0]))
confidences.append(float(".".join(temp)))
max_idx = confidences.index(max(confidences))
return response.results[max_idx].alternatives[0].transcript
def print_sentences(response):
for result in response.results:
best_alternative = result.alternatives[0]
transcript = best_alternative.transcript
confidence = best_alternative.confidence
print("-" * 80)
print(f"Transcript: {transcript}")
print(f"Confidence: {confidence:.0%}")
language_code = "en-US"
# Sample rate in Hertz of the audio data sent
sample_rate_hertz = 48000
# Encoding of audio data sent. This sample sets this explicitly.
# This field is optional for FLAC and WAV audio formats.
config = {
"language_code": language_code,
"sample_rate_hertz": sample_rate_hertz,
}
with io.open("./audio_files/evi.wav", "rb") as f:
content = f.read()
audio = {"content": content}
speech_to_text(config, audio)