add speech to text transcription

This commit is contained in:
riqo 2021-02-05 16:41:55 -06:00
commit 0ba02f2f09
10 changed files with 378 additions and 438 deletions

63
ws.py
View file

@ -11,59 +11,23 @@ from google.cloud import speech
# Instantiates a client
client = speech.SpeechClient()
# config = speech.RecognitionConfig(
# encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
# sample_rate_hertz=16000,
# language_code="en-US",
# )
# streaming_config = speech.StreamingRecognitionConfig(config=config)
# def transcribe(stream):
# requests = (
# speech.StreamingRecognizeRequest(audio_content=chunk) for chunk in stream
# )
# # Detects speech in the audio file
# responses = client.streaming_recognize(
# config=config,
# requests=requests
# )
# for response in responses:
# # Once the transcription has settled, the first result will contain the
# # is_final result. The other results will be for subsequent portions of
# # the audio.
# for result in response.results:
# print("Finished: {}".format(result.is_final))
# print("Stability: {}".format(result.stability))
# alternatives = result.alternatives
# # The alternatives are ordered from most likely to least.
# for alternative in alternatives:
# print("Confidence: {}".format(alternative.confidence))
# print(u"Transcript: {}".format(alternative.transcript))
speechtotext_client = speech.SpeechClient()
config = speech.RecognitionConfig(
audio_channel_count=1,
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=44100,
sample_rate_hertz=16000,
language_code="en-US",
)
# Transcribe an audio file.
def transcribe(content):
input(content)
input(type(content))
content = speech.RecognitionAudio(content=content)
print(len(content))
audio = speech.RecognitionAudio(content=content)
# Detects speech in the audio file
response = speechtotext_client.recognize(config=config, audio=content)
response = speechtotext_client.recognize(config=config, audio=audio)
input(f"Response: {response}")
for result in response.results:
@ -73,18 +37,21 @@ def transcribe(content):
async def session(websocket, path):
buf = []
while True:
chunk = await websocket.recv()
chunk = await websocket.recv()
input(type(chunk))
transcribe(chunk)
# while True:
# chunk = await websocket.recv_frame()
if chunk == "stop":
break
# if chunk == "stop":
# break
buf.append(chunk)
# buf.append(chunk)
input(f"Buffer sample: {buf[0]}")
audio = b"".join(buf)
# input(f"Buffer sample: {buf[0]}")
# audio = b"".join(buf)
transcribe(audio)
# transcribe(audio)
start_server = websockets.serve(session, "localhost", 8080)