This Python code implements a simple voice recognition and translation system. Here’s a breakdown of what each part of the code does:
- Library Used:
speech_recognition
- Function:
voice_to_text()
- This function listens for audio input from the microphone.
- It uses Google’s speech recognition service to convert the spoken audio (in Persian) into text.
- If successful, it returns the recognized text; if it fails to understand the audio or if there's a request error, it handles those exceptions by printing error messages.
- Library Used:
googletrans
- Function:
translate_text(text, dest_language='en')
- This function takes a string of text and translates it into the specified destination language (default is English).
- It uses the Google Translate API to perform the translation and returns the translated text.
- Library Used:
gtts
(Google Text-to-Speech) - Function:
text_to_speech(text)
- This function converts the provided text into speech.
- It saves the audio as an MP3 file and plays it using the default media player on the system (the command is specific to Windows).
- Function:
read_words(text)
- This function splits the translated text into individual words and uses the
text_to_speech
function to read each word aloud one by one.
- This function splits the translated text into individual words and uses the
- Function:
main()
- This function orchestrates the entire process:
- It calls
voice_to_text()
to get the user's spoken input. - It prints the recognized text.
- It translates the recognized text into English using
translate_text()
. - It prints the translated text.
- It converts the translated text to speech and reads each word separately.
- It calls
- This function orchestrates the entire process:
- The
if __name__ == "__main__":
block ensures thatmain()
is called when the script is run directly.
In summary, this code captures spoken input in Persian, translates it to English, and then converts the translated text back into speech, allowing for a seamless interaction between voice and text in different languages.