简体   繁体   中英

How to translate spoken transcript using google translation by Python

I'm new to coding and Python so forgive my lack of using the proper vocabulary. I'm working on a leisurely project where I am trying to translate text from 'speech recognition code'. I've found a way to using the Python googletrans module to translate pre-typed words, but am curious if there is a way to translate the transcript from my mic as source using the same method, thanks!

Here is the 'speech recognition code' I am using.

import speech_recognition as sr
r = sr.Recognizer()
mic = sr.Microphone()
with mic as source:
    r.adjust_for_ambient_noise(source)
    audio = r.listen(source)
    transcript = r.recognize_google(audio)
    print(transcript)

Thanks

Yes. In the example you have given there, you have created a variable, transcript , which contains a string which you should simply be able to pass to the translate function of googletrans Translator object. Unless either of these two modules are doing something very strange, this should work.

For example, if you wanted translate to German:

trans = googletrans.Translator()
trans.translate(transcript, dest='de')

(You'll also need to to import googletrans at the start).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM