python - 当语音识别结果不是预期时打印消息

标签 python python-3.4 voice-recognition

我使用语音识别并检查颜色列表,如果我说的不在列表中,那么它会显示“未找到颜色”,如果找到它会显示“找到颜色”我只希望它显示每个留言一次。 我遇到的问题是如何让“未找到颜色”消息正确显示。

# speech recognition

import speech_recognition as speech

#a lot of variables used for loops and conditions
voice = speech.Recognizer()
condition = False
condition2 = False
condition3 = False
boolean = False
counter = 0
counter2 = 0

while condition == False:
#with microphone input as the input
with speech.Microphone() as source:
    print("Say something!")

    #variable = the input from the microphone
    audio = voice.listen(source)
    voiceRecog = voice.recognize_google(audio)
    try:
        #print the word that was heard
        print("\nYou said: " + voiceRecog)
    except speech.UnknownValueError:


        #if couldnt recognise then print this msg
        print("Sorry, we couldn't make that out. Try again!")

    except speech.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))


    #list of colours  for comparing   
    colours = ["blue", "red", "Green", "Orange", "yellow", "pink", "purple", "black"]

    #loop to check the word spoken against each word in the list
    for i in range(len(colours)):



        #so that if colour found it doesnt claim colour not found
        if boolean == False:
            condition2 = False

        #increase counters by 1 and change booleans to True
        #if spoken word matches any words in the list then output
        if colours[i] == voiceRecog:

            boolean = True
            condition2 = True
            condition3 = True
            counter += 1
            counter2 += 1

            print("\nColour Found!\n")

        #if user says "quit" then all booleans = True (exit all loops)
        elif voiceRecog == "quit":

            boolean = True
            condition = True
            condition2 = True
            condition3 = True

        #if none of other conditions check value of i and of con2
        else:
            #if end of list is reached and con2 = False then con3 = False
            if (i + 1) == len(colours) and condition2 == False:
                condition3 = False
                print(end = "")
    #if con3 = False then counter increase and print 'colour not found'
    if condition3 == False:            
        print("\nColour not found!\n\n")
        counter += 1

#once loop exited by saying quit, print attempts and successful attempts
print("\nYou checked for", counter, "colours and had", counter2, "successful attempts")

上面是我的代码,下面是一个场景。

Say something!

You said: blue

Colour Found!

Say something!

You said: Green

Colour Found!

Say something!

You said: Dave Dave

Say something!

You said: quit

You checked for 2 colours and had 2 successful attempts

应该有 3 次尝试,但有 2 次成功。

但如果我反过来做:

Say something!

You said: Dave Dave

Colour not found!

Say something!

You said: Steve Steve

Colour not found!

Say something!

You said: red

Colour Found!

Say something!

You said: black

Colour Found!

Say something!

You said: quit

You checked for 4 colours and had 2 successful attempts

我知道它这样做的原因,但我想不出解决办法。

最佳答案

# speech recognition

import speech_recognition as speech

#a lot of variables used for loops and conditions
voice = speech.Recognizer()
# Maintain status of each attempt. You can just use two variables successful_attempts and total_attempts also but status list might help you in maintaining more things like a list of tuples (color, result) etc
status = []
# successful_attempts, total_attempts = 0, 0

while True:
    #with microphone input as the input
    with speech.Microphone() as source:
        print("Say something!")

        #variable = the input from the microphone
        audio = voice.listen(source)
        voiceRecog = voice.recognize_google(audio)
        try:
            #print the word that was heard
            print("\nYou said: " + voiceRecog)
        except speech.UnknownValueError:


            #if couldnt recognise then print this msg
            print("Sorry, we couldn't make that out. Try again!")

        except speech.RequestError as e:
            print("Could not request results from Google Speech Recognition service; {0}".format(e))


        #list of colours  for comparing   
        colours = ["blue", "red", "Green", "Orange", "yellow", "pink", "purple", "black"]

        if "quit" in voiceRecog:
            break
        elif voiceRecog in colours:
            status.append(1)
            # successful_attempts += 1
            print("\nColour Found!\n")
        else:
            status.append(0)
            print("\nColour not found!\n\n")

        # total_attempts += 1

#once loop exited by saying quit, print attempts and successful attempts
print("\nYou checked for", len(status), "colours and had", sum(status), "successful attempts")
#print("\nYou checked for", total_attempts, "colours and had", successful_attempts, "successful attempts")

关于python - 当语音识别结果不是预期时打印消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40689097/

相关文章:

python - 将文件实例作为参数传递给 celery 任务引发 "ValueError: I/O operation on closed file"

python - heroku python3 : ImportError: No module named 'encodings'

android - TTS语音识别

android - 如何在 Android 上使用语音搜索和语音识别?

python - 在 jupyter notebook 中编辑代码缩进的快捷方式

python - 如何在 OS X 上使用 Python 中的 matplotlib 生成 Cocoa 可识别的图(最好是 Leopard)

python - 在数据框中移动/滴流更新

mysql - 无法使 mariadb 中的 select+update 事务自动执行

postgresql - Unicode解码错误: 'ascii' codec can't decode byte in position : ordinal not in range(128)

ios - 在应用程序中访问 iOS 语音识别