Friday, 5 February 2021

python speech recognition

#computer speaks: what I can do for you
#user talks to microphone: how are you today
#computer talks back: I am fine

PS C:\Users\bob\python-speech> python main.py
what can i do for you?
say something!
You said: how are you today
I am fine

------------------------
#main.py
import time
import speech_recognition as sr
#from gtts import gTTS
import pyglet
import subprocess
import os
from playsound import playsound
import pyttsx3

def speak(audioString):
    print(audioString)
    #tts = gTTS(text=audioString, lang='en')
    #tts.save("audio.mp3")

    """
    wmp = r"C:\Program Files (x86)\Windows Media Player\wmplayer.exe"
    media_file = os.path.abspath(os.path.relpath("audio.mp3"))
    p = subprocess.call([wmp, media_file])
    """

    #playsound("audio.mp3")
    engine = pyttsx3.init()
    engine.say(audioString)
    engine.runAndWait()

def recordAudio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("say something!")
        r.adjust_for_ambient_noise(source)
        audio = r.listen(source)

        try:
            data = r.recognize_google(audio)
            print("You said: " + data)

        except sr.UnknownValueError:
            return "Couldn't understand audio"

        except sr.RequestError as e:
            return "couldn't request results; {0}" + format(e)

        return data

speak("what can i do for you?")
#time.sleep(2)

data = recordAudio()

if "how are you" in data:
    speak("I am fine")

---------------------------
#power shell
pip install SpeechRecognition
#pip install gtts
#pip install pyglet
pip install pyttsx3

pip install PyAudio-0.2.11-cp38-cp38-win_amd64.whl

#logs
Processing c:\users\bob\python-speech\pyaudio-0.2.11-cp38-cp38-win_amd64.whl
Installing collected packages: PyAudio
Successfully installed PyAudio-0.2.11

reference:

pyaudio

text to sound

microphone to text

No comments:

Post a Comment