how to convert voice to text using google.cloud.speech python. I want to convert audio.raw file data to text using SpeechClient. In client.py file read each 332 characters of audio data and send it via socket. In server.py receive 332 character from socket and convert it to text using SpeechClient.
here is my sample code
client.py
# coding:utf-8
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
from contextlib import closing
import socket
import time
import sys
import os
import struct
import logging
def send():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
with closing(sock):
with open('audio.raw', 'rb') as f:
packets = iter(lambda: f.read(332), b'')
for packet in packets:
try:
sock.sendto(packet, ('192.168.10.64', 12345))
time.sleep(0.02)
except Exception as e:
print(e)
return
if __name__ == '__main__':
print('==================Testing=======================')
send()
print('===============Finished Test===============')
server.py
# coding:utf-8
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
from contextlib import closing
import socket
import time
import sys
import os
import struct
import logging
def socket_open():
print('=========================================')
client = speech.SpeechClient()
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code='en-US')
streaming_config = types.StreamingRecognitionConfig(
config=config)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
with closing(sock):
sock.bind(('192.168.10.64', 12345))
print('socket ok')
first_packet = None
while True:
try:
first_packet = sock.recv(332)
stream = [first_packet]
requests = (types.StreamingRecognizeRequest(audio_content=chunk)
for chunk in stream)
responses = client.streaming_recognize(
streaming_config, requests)
for response in responses:
for result in response.results:
print('Finished: {}'.format(result.is_final))
print('Stability: {}'.format(result.stability))
alternatives = result.alternatives
for alternative in alternatives:
print('Confidence: {}'.format(
alternative.confidence))
print(u'Transcript: {}'.format(
alternative.transcript))
sock.settimeout(300)
except socket.timeout:
print('socket timeout error')
break
except socket.error:
print('socket error occured')
break
if __name__ == '__main__':
print('==================Open Socket=======================')
socket_open()
print('===============Finished Socket===============')
I got output responses is empty.