首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python中使用Google的语言转换器

在Python中使用Google的语言转换器
EN

Stack Overflow用户
提问于 2022-10-20 16:55:17
回答 1查看 46关注 0票数 0

我使用了geeksforgeeks (https://www.geeksforgeeks.org/language-translator-using-google-api-in-python/)的这段代码,我试图运行它,它运行时没有任何错误,它打印出来:

代码语言:javascript
复制
Speak 'hello' to initiate the Translation !
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

但是当我说“你好”的时候,它就认不出来了,也不开始听翻译了。

我已经导入了所有的模块,尝试了更新其中的每一个模块,同时我也使用了macbook。

下面是代码:

代码语言:javascript
复制
import speech_recognition as spr
from googletrans import Translator
from gtts import gTTS
import os
 
 
# Creating Recogniser() class object
recog1 = spr.Recognizer()
 
# Creating microphone instance
mc = spr.Microphone()
 
 
# Capture Voice
with mc as source:
    print("Speak 'hello' to initiate the Translation !")
    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    recog1.adjust_for_ambient_noise(source, duration=0.2)
    audio = recog1.listen(source)
    MyText = recog1.recognize_google(audio)
    MyText = MyText.lower()
 
# Here initialising the recorder with
# hello, whatever after that hello it
# will recognise it.
if 'hello' in MyText:
     
    # Translator method for translation
    translator = Translator()
     
    # short form of english in which
    # you will speak
    from_lang = 'en'
     
    # In which we want to convert, short
    # form of hindi
    to_lang = 'hi'
     
    with mc as source:
         
        print("Speak a stentence...")
        recog1.adjust_for_ambient_noise(source, duration=0.2)
         
        # Storing the speech into audio variable
        audio = recog1.listen(source)
         
        # Using recognize.google() method to
        # convert audio into text
        get_sentence = recog1.recognize_google(audio)
 
        # Using try and except block to improve
        # its efficiency.
        try:
             
            # Printing Speech which need to
            # be translated.
            print("Phase to be Translated :"+ get_sentence)
 
            # Using translate() method which requires
            # three arguments, 1st the sentence which
            # needs to be translated 2nd source language
            # and 3rd to which we need to translate in
            text_to_translate = translator.translate(get_sentence,
                                                     src= from_lang,
                                                     dest= to_lang)
             
            # Storing the translated text in text
            # variable
            text = text_to_translate.text
 
            # Using Google-Text-to-Speech ie, gTTS() method
            # to speak the translated text into the
            # destination language which is stored in to_lang.
            # Also, we have given 3rd argument as False because
            # by default it speaks very slowly
            speak = gTTS(text=text, lang=to_lang, slow= False)
 
            # Using save() method to save the translated
            # speech in capture_voice.mp3
            speak.save("captured_voice.mp3")    
             
            # Using OS module to run the translated voice.
            os.system("start captured_voice.mp3")
 
        # Here we are using except block for UnknownValue
        # and Request Error and printing the same to
        # provide better service to the user.
        except spr.UnknownValueError:
            print("Unable to Understand the Input")
             
        except spr.RequestError as e:
            print("Unable to provide Required Output".format(e))
EN

回答 1

Stack Overflow用户

发布于 2022-12-02 03:30:56

代码语言:javascript
复制
from gtts import gTTS
from io import BytesIO
from pygame import mixer
import time

def speak():
    mp3_fp = BytesIO()
    tts = gTTS('KGF is a Great movie to watch', lang='en')
    tts.write_to_fp(mp3_fp)
    tts.save("Audio.mp3")
    return mp3_fp

mixer.init()
sound = speak()
sound.seek(0)
mixer.music.load(sound, "mp3")
mixer.music.play()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74143619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档