update ws.py
This commit is contained in:
parent
e2dc3bad20
commit
911e158485
1 changed files with 50 additions and 26 deletions
76
ws.py
76
ws.py
|
|
@ -12,39 +12,61 @@ from google.cloud import speech
|
||||||
client = speech.SpeechClient()
|
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(
|
config = speech.RecognitionConfig(
|
||||||
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
|
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
|
||||||
sample_rate_hertz=16000,
|
sample_rate_hertz=44100,
|
||||||
language_code="en-US",
|
language_code="en-US",
|
||||||
)
|
)
|
||||||
|
|
||||||
streaming_config = speech.StreamingRecognitionConfig(config=config)
|
# Transcribe an audio file.
|
||||||
|
def transcribe(content):
|
||||||
|
input(content)
|
||||||
|
input(type(content))
|
||||||
|
|
||||||
def transcribe(stream):
|
content = speech.RecognitionAudio(content=content)
|
||||||
|
|
||||||
requests = (
|
|
||||||
speech.StreamingRecognizeRequest(audio_content=chunk) for chunk in stream
|
|
||||||
)
|
|
||||||
|
|
||||||
# Detects speech in the audio file
|
# Detects speech in the audio file
|
||||||
responses = client.streaming_recognize(
|
response = speechtotext_client.recognize(config=config, audio=content)
|
||||||
config=config,
|
input(f"Response: {response}")
|
||||||
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))
|
|
||||||
|
|
||||||
|
for result in response.results:
|
||||||
|
return result.alternatives[0].transcript
|
||||||
|
|
||||||
|
|
||||||
async def session(websocket, path):
|
async def session(websocket, path):
|
||||||
|
|
@ -58,10 +80,12 @@ async def session(websocket, path):
|
||||||
|
|
||||||
buf.append(chunk)
|
buf.append(chunk)
|
||||||
|
|
||||||
transcribe(buf)
|
input(f"Buffer sample:" {buf[0]})
|
||||||
|
audio = b"".join(buf)
|
||||||
|
|
||||||
|
transcribe(audio)
|
||||||
|
|
||||||
|
|
||||||
start_server = websockets.serve(session, "localhost", 8080)
|
start_server = websockets.serve(session, "localhost", 8080)
|
||||||
asyncio.get_event_loop().run_until_complete(start_server)
|
asyncio.get_event_loop().run_until_complete(start_server)
|
||||||
asyncio.get_event_loop().run_forever()
|
asyncio.get_event_loop().run_forever()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue