Course Content

Project 4: Indian Railways Announcement Software | Python Tutorials For Absolute Beginners #124

In this tutorial, we will learn to program an “Indian Railway Announcement System” using python. I am using Virtual Studio Code IDE for this project.

Prerequisite: The prerequisite of this project is the basic knowledge of python.

For creating railway announcement software, we will be using a bunch of modules like pyaudio, pydub, and gTTS to process audio and get the announcing status of thousands of trains. By using PyAudio module, we can easily use Python to play and record audio on a variety of platforms. Pydub is a simple and well-designed Python module for audio manipulation and gTTS (which stands for Google Text-to-Speech) is a Python library and CLI tool to interface with Google Translate text-to-speech API.

We will cover the logic as well as coding in this Python Project. This tutorial is a part of python series for absolute beginners. If you have not watched my python tutorial, then click on the link below and start learning. This course will help you in learning Python from the beginner to the advanced level.

https://codewithharry.com/videos/python-tutorials-for-absolute-beginners-0

Practice makes a man perfect. You cannot learn to code by just watching the code implementation until you start to doing it yourself. So, keep trying to solve these new programming puzzles with codewithharry.

Click here to download media+other files

Code as described/written in the video

import os
import pandas as pd
from pydub import AudioSegment
from gtts import gTTS

# pip install pyaudio
# pip install pydub
# pip install pandas
# pip install gTTS


def textToSpeech(text, filename):
    mytext = str(text)
    language = 'hi'
    myobj = gTTS(text=mytext, lang=language, slow=False)
    myobj.save(filename)
    

# This function returns pydubs audio segment
def mergeAudios(audios):
    combined = AudioSegment.empty()
    for audio in audios:
        combined += AudioSegment.from_mp3(audio)
    return combined

def generateSkeleton():
    audio = AudioSegment.from_mp3('railway.mp3')

    # 1 - Generate kripya dheyan dijiye
    start = 88000
    finish = 90200
    audioProcessed = audio[start:finish]
    audioProcessed.export("1_hindi.mp3", format="mp3")

    # 2 is from-city

    # 3 - Generate se chalkar
    start = 91000
    finish = 92200
    audioProcessed = audio[start:finish]
    audioProcessed.export("3_hindi.mp3", format="mp3")

    # 4 is via-city

    # 5 - Generate ke raaste
    start = 94000
    finish = 95000
    audioProcessed = audio[start:finish]
    audioProcessed.export("5_hindi.mp3", format="mp3")

    # 6 is to-city

    # 7 - Generate ko jaane wali gaadi sakhya
    start = 96000
    finish = 98900
    audioProcessed = audio[start:finish]
    audioProcessed.export("7_hindi.mp3", format="mp3")

    # 8 is train no and name

    # 9 - Generate kuch hi samay mei platform sankhya
    start = 105500
    finish = 108200
    audioProcessed = audio[start:finish]
    audioProcessed.export("9_hindi.mp3", format="mp3")

    # 10 is platform number

    # 11 - Generate par aa rahi hai
    start = 109000
    finish = 112250
    audioProcessed = audio[start:finish]
    audioProcessed.export("11_hindi.mp3", format="mp3")

def generateAnnouncement(filename):
    df = pd.read_excel(filename)
    print(df)
    for index, item in df.iterrows():
        # 2 - Generate from-city
        textToSpeech(item['from'], '2_hindi.mp3')

        # 4 - Generate via-city
        textToSpeech(item['via'], '4_hindi.mp3')

        # 6 - Generate to-city
        textToSpeech(item['to'], '6_hindi.mp3')

        # 8 - Generate train no and name
        textToSpeech(item['train_no'] + " " + item['train_name'], '8_hindi.mp3')

        # 10 - Generate platform number
        textToSpeech(item['platform'], '10_hindi.mp3')

        audios = [f"{i}_hindi.mp3" for i in range(1,12)]

        announcement = mergeAudios(audios)
        announcement.export(f"announcement_{item['train_no']}_{index+1}.mp3", format="mp3")


if __name__ == "__main__":
    print("Generating Skeleton...")
    generateSkeleton()
    print("Now Generating Announcement...")
    generateAnnouncement("announce_hindi.xlsx")
    


Comments(4)

Karan1999 8 months ago
Sir maine ise kam se kam 5 baar dekha hai ye work nahi kar raha hai sir
harry 6 months, 1 week ago
Kya error aaya? Also do you know python?
vishu04 6 months ago
It's giving error... 
Traceback (most recent call last):
  File "C:\Users\VISHAL\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\base.py", line 4411, in get_value
    return libindex.get_value_at(s, key)
  File "pandas\_libs\index.pyx", line 44, in pandas._libs.index.get_value_at
  File "pandas\_libs\index.pyx", line 45, in pandas._libs.index.get_value_at
  File "pandas\_libs\util.pxd", line 98, in pandas._libs.util.get_value_at
  File "pandas\_libs\util.pxd", line 83, in pandas._libs.util.validate_indexer
TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "V:\Computer Science\Python\project\Indian Railway Announcement System\main.py", line 99, in <module>
    generateAnnouncement("announce_hindi.xlsx")
  File "V:\Computer Science\Python\project\Indian Railway Announcement System\main.py", line 81, in generateAnnouncement
    textToSpeech(item['to'], '6_hindi.mp3')
  File "C:\Users\VISHAL\AppData\Roaming\Python\Python37\site-packages\pandas\core\series.py", line 871, in __getitem__
    result = self.index.get_value(self, key)
  File "C:\Users\VISHAL\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\base.py", line 4419, in get_value
    raise e1
  File "C:\Users\VISHAL\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\base.py", line 4405, in get_value
    return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
  File "pandas\_libs\index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 90, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'to'
manan7 1 month, 2 weeks ago
Sir I am following your python course for a long time and was stuck and unable to find the announcement voice so can you post the voice Please

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here