DAY 7 - Text To Audio And Audio To Text Using Python Script

 Text To Audio And Audio To Text Using Python Script

#Text To Audio using Python Script
##############################################

1. Install gTTS using below command:

Open command prompt and move to path "C:\Users\Rakesh.Ranjan\AppData\Local\Programs\Python\Python37-32\Scripts>" and then execute below command to install gTTS

pip install gTTS

2. >>> import sys

   Using the command to know path variables
   >>> sys.path
   Audio file will be saved at one of these paths

3.
#module for text to speech conversion
from gtts import gTTS

mytext = 'Welcome to India, Mr Mike !'

#command to convert text to audio
myobj = gTTS(text=mytext, lang=language, slow=False)

#saving the file in mp3 format
myobj.save("WelcomeFile.mp3")

4. Steps to find the file location

import os.path
from os import path

>>> path.exists("WelcomeFile.mp3")
True

>>> path.realpath("WelcomeFile.mp3")
'C:\\Users\\Rakesh.Ranjan\\AppData\\Local\\Programs\\Python\\Python37-32\\WelcomeFile.mp3'
 
#Audio To Text using Python Script
################################################

1. Install SpeechRecognition

#Speech to Text Conversion
pip install SpeechRecognition

#Notably, the PyAudio package is needed for capturing microphone input.

2. Steps to convert audio to text
   a. download .wav file from "http://www.voiptroubleshooter.com/open_speech/india.html"

   b. Transfer the audio file at path - "C:\Users\Rakesh.Ranjan\AppData\Local\Programs\Python\Python37-32" because file will be picked from this path by default.

      Othewise open a python file from desktop and execute it in python IDLE. That's way, next file will be picked from Desktop by default. That's my personal experience. 

   c. Now run below mentioned commands to convert audio to text
import speech_recognition as sr

#Creating a Recognizer instance
r = sr.Recognizer()

hindi_audio = sr.AudioFile("OSR_in_000_0062_16k.wav")

#The context manager opens the file and reads its contents, storing the data in an AudioFile instance called source

>>> with hindi_audio as source:

#Using record() to Capture Data From a File
#the record() method records the data from the entire file into an AudioData instance
r.adjust_for_ambient_noise(source)
audio = r.record(source)

#audio1 = r.record(source, duration=4) #0 to 4 sec
#audio2 = r.record(source, duration=4) #4 to 8 sec

#Each Recognizer instance has seven methods for recognizing speech from an audio source using various APIs.
#recognize_google(): Google Web Speech API is one of them
#Of the seven, only recognize_sphinx() works offline with the CMU Sphinx engine. The other six all require an internet connection.
#Even with a valid API key, you’ll be limited to only 50 requests per day, and there is no way to raise this quota.

text = r.recognize_google(audio)

>>> text
'Keshav Prakhand Mein Char ladkiyan Hain Kai log Kumar ko Pasand Karte Hain Tumhare khargosh Ka Rang Safed hai aapki Gaya cal se yahan hai cal ka khana Sulekha ki lalkar banaegi
Akshay ki Kheer Garam Ho Gayi Main Kal Khwab Mein Ek Khubsurat gadi Dekhen yah Khabar sunkar Raghu Khushi Se Pagal Ho Gaya School Ki Ladai Mein Kai Jawan Ghayal Hue Kai ke aage
Gori chal rahi hai Kai Salon pahle Sita Ki Agni Pariksha hui thi aapki Soch Mein Kaun Sa Ghoda Shamil Karen aapki Dua Se Sab khairiyat Hai is varsh jansankhya ki Jayanti Mein Kai
log chhut Gaye Mausam badalne per Mere Ko Main Dar shuru ho jata hai gumnam Logon Ko gairon se Upkar ki Asha Nahin Karni chahie'


#RR #Day7 #Python #TextToAudio #AudioToText #OpenSpeechRepo #HappyLearning #WeLearnEveryday

Comments

Popular posts from this blog

Day 32 - Python Script to track Available COVID-19 Vaccine Slots for 18+ in India

DAY 1 - Steps to prepare your windows laptop for Python Programming

Day 26 - Call Power BI REST APIs to get POWER BI REPORT Details