Created
April 2, 2018 12:15
-
-
Save mzmmoazam/fc2fcf3d3fe187f1e38672a6d1f77280 to your computer and use it in GitHub Desktop.
This gist converts Turkish audio to English audio.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Algorithmia, speech_recognition as sr, pyttsx | |
# Algorthmia Api key and end point | |
client = Algorithmia.client('simCM+BqJzKSw/iMrQsR/OTRwVE1') | |
algo = client.algo('translation/GoogleTranslate/0.1.1') | |
# pyttsx engine config | |
speech_engine = pyttsx.init('sapi5') # see http://pyttsx.readthedocs.org/en/latest/engine.html#pyttsx.init | |
speech_engine.setProperty('rate', 150) | |
def speak(text): | |
''' | |
speaks the text(english) provided to it. | |
:param text: text provided in english -- str | |
:return: speaks using the speaker -- audio output | |
''' | |
speech_engine.say(text) | |
speech_engine.runAndWait() | |
def listen(): | |
''' | |
no arguments , records audio from the microphone and converts it to text. | |
:return: text as interpretted from the audio(input voice recieved) | |
''' | |
# obtain audio from the microphone | |
r = sr.Recognizer() | |
with sr.Microphone() as source: | |
print("Listening....say something...") | |
audio = r.listen(source) | |
print("... audio recorded ...") | |
# recognize speech using Google Speech Recognition | |
try: | |
# for testing purposes, we're just using the default API key | |
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")` | |
# instead of `r.recognize_google(audio)` | |
# for English | |
# print("Google Speech Recognition thinks you said in English: - " + r.recognize_google(audio, language="en-US")) | |
# for Turkish | |
resp = r.recognize_google(audio, language="tr-TR") | |
print("Google Speech Recognition thinks you said in Turkish: - " + resp) | |
return resp | |
except sr.UnknownValueError: | |
print("Google Speech Recognition could not understand audio") | |
except sr.RequestError as e: | |
print("Could not request results from Google Speech Recognition service; {0}".format(e)) | |
return None | |
def translate(text): | |
''' | |
translates text from english to turkish | |
:param text: text to be translated to English | |
:return: translated text | |
''' | |
input = { | |
"action": "translate", | |
"text": text | |
} | |
resp = algo.pipe(input).result["translation"] | |
return resp | |
def main(): | |
audio = listen() | |
resp = translate(text=audio) | |
# print(resp) | |
speak(resp) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment