Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

python 3.x - Trouble Integrating Speech Recognition into PysimpleGUI + Search Bot

I've recently tried my hand at a simple search engine using TTS. However, I tried integrating Speech to text for the search field in Pysimple GUI and I have hit a road block.

I'm able to get the pysimpleGUI to recognize the words that I say, and search the results.However, the search engine mechanism doesn't recognize my speech to text as the value that I input and only gives me back the first letter that I used in my speech to text.

For example, I said "What is the weather like today" and it returned me back the definition of the letter "W"

These are the packages that I used:

import wolframalpha
import wikipedia
import speech_recognition as sr
r = sr.Recognizer()
m = sr.Microphone()
import PySimpleGUI as sg
import pyttsx3

All the stuff inside Pysimple GUI text window.

layout = [  [sg.Text('Welcome Back Sir')],
            [sg.Text('How can I be of assistance'), sg.InputText()],
            [sg.ReadButton('Speak'), sg.Button('Ok'), sg.Button('Cancel')]]

window = sg.Window('Pybot', layout)
engine = pyttsx3.init()

Event Loop to process "events" and get the "values" of the inputs

while True:
    event, values = window.read()
    if event in (None, 'Cancel'):
        break
    if event == 'Speak':
        with m as source:
            r.adjust_for_ambient_noise(source)
            audio = r.listen(source)
            values = r.recognize_google(audio, language='en-US')
            print(values)
    try:
        wiki_res = wikipedia.summary(values[0], sentences=2)
        wolfram_res = next(client.query(values[0]).results).text
        engine.say(wolfram_res)
        sg.PopupNonBlocking("Wolfram Result: "+wolfram_res,"Wikipedia Result: "+wiki_res)
    except wikipedia.exceptions.DisambiguationError:
        wolfram_res = next(client.query(values[0]).results).text
        engine.say(wolfram_res)
        sg.PopupNonBlocking(wolfram_res)
    except wikipedia.exceptions.PageError:
        wolfram_res = next(client.query(values[0]).results).text
        engine.say(wolfram_res)
        sg.PopupNonBlocking(wolfram_res)
    except:
        wiki_res = wikipedia.summary(values[0], sentences=2)
        engine.say(wiki_res)
        sg.PopupNonBlocking(wiki_res)

    engine.runAndWait()

    print (values[0])

window.close()
question from:https://stackoverflow.com/questions/65660829/trouble-integrating-speech-recognition-into-pysimplegui-search-bot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You use same variable values, so values[0] is w.


while True:
    event, values = window.read()
...
            values = r.recognize_google(audio, language='en-US')
...

revised code

enter image description here

while True:
    event, values = window.read()
    if event in (None, 'Cancel'):
        break
    # print(event, values)
    if event == 'Speak':
        with m as source:
            r.adjust_for_ambient_noise(source)
            audio = r.listen(source)
            value = r.recognize_google(audio, language='en-US')
            print(value)
            window[0].update(value)
            window.write_event_value('Ok', '')
    elif event == 'Ok':
        if values[0] == '':
            continue
        try:
            wiki_res = wikipedia.summary(values[0], sentences=2)
            wolfram_res = next(client.query(values[0]).results).text
            engine.say(wolfram_res)
            sg.PopupNonBlocking("Wolfram Result: "+wolfram_res,"Wikipedia Result: "+wiki_res)
        except wikipedia.exceptions.DisambiguationError:
            wolfram_res = next(client.query(values[0]).results).text
            engine.say(wolfram_res)
            sg.PopupNonBlocking(wolfram_res)
        except wikipedia.exceptions.PageError:
            wolfram_res = next(client.query(values[0]).results).text
            engine.say(wolfram_res)
            sg.PopupNonBlocking(wolfram_res)
        except:
            wiki_res = wikipedia.summary(values[0], sentences=2)
            engine.say(wiki_res)
            sg.PopupNonBlocking(wiki_res)

        engine.runAndWait()

window.close()

It will block when audio processing, maybe you can try multi-threaded programming, otherwise GUI will no response sometime.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...