Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Scripts to convert Text-to-speech #214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions System-Automation-Scripts/Text-to-speech/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Convert text to speech ##

- This script is written in Python and can be used to convert text to speech.
- The following packages are used :
- gtts , Google-Text-To-Speech module, a wrapper around python to implement the text-to-speech API.
- pytesseract, Optical Recognition Module, to convert (text) characters from images to text.
- wikipedia, a Python wrapper to access basic wikipedia content about any given topic.
- Pillow, an image-processing library in Python.
- All these packages can be installed using ** pip **.
- An mp3 file will be created on running the script based on the option chosen

## Working ##

![Image](/images/working.PNG)

- 1, To convert text and convert it to speech
- 2, To convert the contents of a text file to speech
- 3, convert the text in an image to speech
- 4, convert information about a topic from wikipedia into speech
- According to the option chosen, the required input files must be placed in the same folder
- A file named text_content.mp3 will be generated.

51 changes: 51 additions & 0 deletions System-Automation-Scripts/Text-to-speech/text-to-speech.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

#Imports and dependencies
#gtts, stands for the Google-Text-To-Speech module that is used to convert text to speech
from gtts import gTTS
import os

#These packages are used for OCR(Optical character recognition)
import pytesseract
from PIL import Image

#This module is a wrapper around python, basic information about a topic can be obtained
import wikipedia

#This script can be used to convert text to speech, either from a text file or when a user enters text
#Text can be read from images using the Optical recognition framework built for Python

print("Option 1, enter text and convert it to speech \n")
print("Option 2, convert the contents of a text file to speech \n")
print("Option 3, convert the text in an image to speech \n")
print("Option 4, convert information about a topic from wikipedia into speech \n")


#Conversion will be done to the English language
language = "en"

def convert_text_to_speech(option):

text = ""
if option == 1:
text = input("Enter the text, that has to be converted to speech ")

elif option == 2:
file_name = input("Enter the name of the text file, that has to be converted to speech ")
with open(file_name , "r") as handle:
text = handle.read().replace("\n" , "")

elif option == 3:
image_path = input("Enter the path of the image that has to be read and converted to speech ")
text = pytesseract.image_to_string(Image.open(image_path).replace("\n" , ""))

elif option == 4:
wikipedia = input("Enter the topic about which information is to be obtained ")
text = wikipedia.summary(wikipedia)

speech = gTTS(text = text, lang = language, slow = True)
speech.save("text_content.mp3")


if __name__ == "__main__":
option = int(input("Enter the option "))
convert_text_to_speech(option)