python - What would cause Google Cloud Speech API to return None in Operation.Results? -
what cause google cloud speech api return none in operation.results?
i using following code make call google speech api , following error return time time:
traceback (most recent call last): file "speech_api_test.py", line 151, in <module> run() file "speech_api_test.py", line 27, in run speech_api(audio_output_location, flac_file_output) file "speech_api_test.py", line 124, in speech_api result in operation.results: typeerror: 'nonetype' object not iterable just trying understand why happening, have looked through of speech api documentation here no results. nothing of error seems mentioned anywhere in stack exchange in relation google speech api, find. believed @ first error related limits restrict how long audio file 80 mins has been increased 180 mins. while still time time nonetype error on files close updated limit.
here code:
import os import time datetime import datetime #the 2 variables returned function #for cause lets assumed return follow audio_output_location = "gs://bucket/py/auto_flac_file/test_file.flac" flac_file_output = "/var/www/test/py/auto_flac_files/" # py directory directory links or "gcsfuses" folder on google vm def speech_api(audio_output_location, flac_file_output): print("starting speech_api...") google.cloud import speech client = speech.client() # input_flac_file must in form of gs://bucket/subbucket(s)/filename.flac input_flac_file = audio_output_location # setting start time run_time variable start_time = datetime.now() # starting api call sample = client.sample(source_uri = input_flac_file, encoding = speech.encoding.flac, sample_rate_hertz = 16000) operation = sample.long_running_recognize( language_code='en-us', max_alternatives=0,) retry_count = 100 while retry_count > 0 , not operation.complete: retry_count -= 1 time.sleep(10) operation.poll() # api call operation.complete true print("appending results txt file...") result in operation.results: alternative in result.alternatives: # auto naming file replaced .flac .txt new_file = os.path.basename(input_flac_file).replace("flac", "txt") # var output file txt placed test_location = "/var/www/test/py/text_files" # setting file location filename included in path new_file_location = os.path.join(test_location, new_file) # opening , appending transcription results .txt file text_file = open(new_file_location, "a") text_file.write(result.transcript) text_file.close() print("results appended txt file...") print("starting delete flac file...") # setting path auto deletion of flac file split_flac_audio = os.path.split(input_flac_file) # auto adding filename path file deletion delete_audio = os.path.join(flac_file_output, split_flac_audio[1]) # delecting file os.remove(delete_audio) print("flac file deleted...") # calculates run time. run_time = datetime.now() - start_time print("transciption complete!") print("runtime = %s" % (run_time)) if __name__ == "__main__": speech_api(audio_output_location, flac_file_output)
Comments
Post a Comment