When silence is detected AcceptWaveform()
returns True and you can retrieve the result with Result()
. If it returns False you can retrieve a partial result with PartialResult()
. The FinalResult()
means the stream is ended, buffers are flushed and you retrieve the remaining result which could be silence.
What you could do is
import json
text = []
with open(audio_file, "rb") as audio:
while True:
data = audio.read(4000)
if len(data) == 0:
break
# if silence detected save result
if recognizer.AcceptWaveform(data):
text.append(json.loads(rec.Result())["text"])
text.append(json.loads(rec.FinalResult())["text"])
and you get a list of sentences.