How to create variables dynamically (Speech_recognition)

Asked

Viewed 60 times

0

I need to transcribe a 21 minute audio using speech_recognition and recognize_google the problem is that the api does not allow any audio size, I do not know exactly which but it is greater than 30 seconds so I created the function:

with contextlib.closing(wave.open(fname, 'r')) as f:
    frames = f.getnframes()
    rate = f.getframerate()
    duration = frames / float(rate)
    splitaudio = duration / 30

blocos = int(round(splitaudio, 0) + 1)

That returns me 43 blocks.

That employee code to follow:

with sr.AudioFile(AUDIO_FILE) as source:
     audio1 = r.record(source, duration=30)
     audio2 = r.record(source, duration=30)
     audio3 = r.record(source, duration=30)
     audio4 = r.record(source, duration=30)
     audio5 = r.record(source, duration=30)
     audio6 = r.record(source, duration=30)
     audio7 = r.record(source, duration=30)
     audio8 = r.record(source, duration=30)
     #... até atingir o valor de blocos que tenho ... #

speech1 = ("Transcription: " + r.recognize_google(audio1, language='pt-br') + '\n')
speech2 = ("Transcription: " + r.recognize_google(audio2, language='pt-br') + '\n')
speech3 = ("Transcription: " + r.recognize_google(audio3, language='pt-br') + '\n')
speech4 = ("Transcription: " + r.recognize_google(audio4, language='pt-br') + '\n')
speech5 = ("Transcription: " + r.recognize_google(audio5, language='pt-br') + '\n')
speech6 = ("Transcription: " + r.recognize_google(audio6, language='pt-br') + '\n')
speech7= ("Transcription: " + r.recognize_google(audio7, language='pt-br') + '\n')
speech8 = ("Transcription: " + r.recognize_google(audio8, language='pt-br') + '\n')
#até completar

with open('frases/frases.csv', 'a') as writefile:
     writefile.write(speech1)
     writefile.write(speech2)
     writefile.write(speech3)
     writefile.write(speech4)
     writefile.write(speech5)
     writefile.write(speech6)
     writefile.write(speech7)
     #... até completar

However I need to keep creating many variables and this code would only be useful for this audio, any idea how to improve this code creating these variables dynamically ? Or some other approach not to be necessary to type so much and make the code cleaner ?

****** EDIT ******

In that part :

with sr.AudioFile(AUDIO_FILE) as source:
         audio1 = r.record(source, duration=30)
         audio2 = r.record(source, duration=30)
         ...

The audio1 variable records the first 30 seconds of audio, the audio2 variable from 30 to 60 and so on.

  • Study about Python lists.

  • By the question, I believe you intend to write in a file, so write the list values in the file.

  • When inside with : audio1 = r.record(source, Duration=30) records the first 30 seconds. audio2 = r.record(source, Duration=30) records from 30 to 60 and so on so I need to create these variables dynamically, if it weren’t for that I could simply . append as you might be suggesting.

2 answers

2


If you know the amount of blocks, make a repeat loop that stores the values in a list:

N = 43

audios = []

with sr.AudioFile(AUDIO_FILE) as source:
    for _ in range(N)
         audios.append( r.record(source, duration=30) )

This avoids having to set a variable for every 30s of audio.

  • Boy, your answer only came to me a minute after my post, if you hadn’t published it, the weird thing is it came to me 20 minutes ago.

  • @Sidon Page cache, maybe :D

  • True, almost sure!

0

From what I understand you know how many blocos of 30s are, if so, a way to do this would be, as suggested, to keep on a list, let’s say we know beforehand that there are 10 blocks:

audios = []
with sr.AudioFile(AUDIO_FILE) as source:
    for n in range(0,10)
        audios.append(r.record(source, duration=30)) 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.