<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anirudh Panthula on Medium]]></title>
        <description><![CDATA[Stories by Anirudh Panthula on Medium]]></description>
        <link>https://medium.com/@anirudhpanthula?source=rss-a6db73c7cec9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*DoUFX8s1Wc6tDu-R.</url>
            <title>Stories by Anirudh Panthula on Medium</title>
            <link>https://medium.com/@anirudhpanthula?source=rss-a6db73c7cec9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 11:45:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anirudhpanthula/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Convert Speech to Text in 5 lines of Code]]></title>
            <link>https://medium.com/customerinsights-ai/convert-speech-to-text-in-5-lines-of-code-f5c3884a8412?source=rss-a6db73c7cec9------2</link>
            <guid isPermaLink="false">https://medium.com/p/f5c3884a8412</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Anirudh Panthula]]></dc:creator>
            <pubDate>Wed, 27 Jan 2021 00:39:21 GMT</pubDate>
            <atom:updated>2021-01-27T00:39:21.674Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wzsfkizhqLxssd5XDKTuwg.jpeg" /></figure><p>With the popularity of speech-enabled products like Alexa, Siri, and even Fridges that can talk, speech-enabled devices are now an essential part of our lives. This popularity can be tracked down to open-source software. There are a lot of open source API&#39;s that have been published by tech giants like Google and Facebook. These companies have tons of data and this data has been used to build sophisticated models that can perform really well in a lot of scenarios.</p><p>In this blog, we will use one such open-source speech recognition software by google to convert your speech into text.</p><h3><strong>Step 1: Installing Libraries</strong></h3><p>SpeechRecognition — SpeechRecognition is a package that makes it easy to retrieve audio input. This package has built-in features, that retrieves audio input and process it in the right form for text conversion.</p><p>We will use SpeechRecognition as a wrapper to call, google web speech API this API is the core, that converts speech to text.</p><h4>Installing Speech Recognition</h4><p>$ pip install SpeechRecognition</p><p>Run this command in a shell, if you are using a mac os.</p><h4>Installing PyAudio</h4><p><a href="https://pypi.org/project/PyAudio/">Pyaudio</a> — This package allows you to access the microphone from your device.</p><h4>macOS</h4><p>First, install homebrew, if you already have this, move onto the next step:</p><pre>$ $/bin/bash -c &quot;$(curl -fsSL <a href="https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh">https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh</a>)&quot;</pre><p>Install PortAudio with homebrew</p><pre>$ brew install portaudio</pre><p>Install PyAudio with pip</p><pre>$ pip install pyaudio</pre><h4>Windows</h4><pre>$ pip install pyaudio</pre><h3>Step 2: Building the Script</h3><p>Now that, we have all the necessary libraries installed, let’s move onto building the python script. This program will take in a voice input from a microphone, use google speech API to convert voice into text. It will continue to do this on a loop until the user says the keywords to end the loop, here the keywords are “bye” and “goodbye”. When the user says any of these keywords, the program will break the loop.</p><h4>Import</h4><pre>import speech_recognition as sr # importing speech recognition api.</pre><h4>Create an empty string that takes in the converted text.</h4><pre>message = &quot;&quot;</pre><h4>Define a loop, that will start recording the user&#39;s voice</h4><pre>while True:</pre><pre>    r = sr.Recognizer()  # initialize recognizer.</pre><pre>    with sr.Microphone() as source:</pre><pre>        print(&quot;Speak Anything :&quot;)</pre><pre>        audio = r.listen(source)  # listen to the source.</pre><pre>        try:</pre><pre>            message = r.recognize_google(audio)  # use recognizer to convert audio to text</pre><pre>            print(&quot;You said : {}&quot;.format(message))</pre><p>Here, we are initializing speech recognizer to take in audio input from the microphone. Recognize_google will convert audio to text. Once, the text is created, it will be printed out as “you said: (text)”</p><h4>Loop Termination:</h4><pre>except:</pre><pre>        print(&quot;Sorry could not recognize your voice&quot;)  # in case of voice not recognized  clearly.</pre><pre>if message == &quot;bye&quot; or message == &quot;goodbye&quot;: # when the user says &quot;bye&quot; or &quot;goodbye&quot; the program terminates.</pre><pre>    break</pre><p>If the speech API does not recognize any words, except condition will be run and the message will be displayed. For terminating the loop, i.e to stop audio recording, Keywords will be used. Once the speech API, detects these words, the loop will be terminated.</p><h4>Bringing it all together, here is the complete python script:</h4><pre>import speech_recognition as sr # importing speech recognition api.</pre><pre>message = &quot;&quot;</pre><pre>while True:</pre><pre>    r = sr.Recognizer()  # initialize recognizer.</pre><pre>    with sr.Microphone() as source:  # we are using source as microphone but you can use audio files too.</pre><pre>        print(&quot;Speak Anything :&quot;)</pre><pre>        audio = r.listen(source)  # listen to the source.</pre><pre>        try:</pre><pre>            message = r.recognize_google(audio)  # use recognizer to convert audio to text part.</pre><pre>            print(&quot;You said : {}&quot;.format(message))</pre><pre>        except:</pre><pre>            print(&quot;Sorry could not recognize your voice&quot;)  # in case of voice not recognized  clearly.</pre><pre>    if message == &quot;bye&quot; or message == &quot;goodbye&quot;: # when the user says &quot;bye&quot; or &quot;goodbye&quot; the program terminates.</pre><pre>        break</pre><h3>Step 3: Testing and sample outputs</h3><p>I have tried saying: “how are you”, “sky is blue” and “goodbye”. Here is how the output from the script looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*9RCSgWwt_KafX7WafxZpXQ.png" /></figure><p>In this blog, we have run a python script that takes in audio input and converts it into text. The program also has the logic to terminate recording audio using keywords. All of this is done in 5 lines of code(almost).</p><p>Thanks for reading.</p><p>Github link for code: <a href="https://github.com/CIAI-RnD-Team/Speech_to_text_Google_API">https://github.com/CIAI-RnD-Team/Speech_to_text_Google_API</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f5c3884a8412" width="1" height="1" alt=""><hr><p><a href="https://medium.com/customerinsights-ai/convert-speech-to-text-in-5-lines-of-code-f5c3884a8412">Convert Speech to Text in 5 lines of Code</a> was originally published in <a href="https://medium.com/customerinsights-ai">CustomerInsights.AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>