使用语音识别促进多个答案

Promoting multiple answers using voice recognization

我正在开发一款基于语音识别的游戏,用户必须识别显示的图像并提供答案。我有一个问题,如果他们的第一个答案是错误的或显示的图像无法识别,我如何允许用户提供多个答案。我尝试使用 try: 和 multiple except 但我无法让它工作。

if i== 1:
    carImg = pygame.image.load(os.path.join(image_path,'tiger.jpg'))
    gameDisplay.blit(carImg,(130,0))
    pygame.display.update()
    r = sr.Recognizer()
    with sr.Microphone() as source:

        print ('Say Something!')
        audio = r.listen(source)

    try:
        text = r.recognize_google(audio)
    except:
        print('Did not get that try Again')
        text=''

    if text == 'tiger':
        print('good job') 
        pygame.mixer.Sound.play(right)
        pygame.mixer.music.stop()
    else:
        print('wrong')
        pygame.mixer.Sound.play(wrong)
        pygame.mixer.music.stop()
    time.sleep(7)
for i in range(1,3):
       
          
             if i== 1:
         
                   carImg = pygame.image.load(os.path.join(image_path,'tiger.jpg'))
                   gameDisplay.blit(carImg,(130,0))
                   pygame.display.update()
                   recognizer = sr.Recognizer()
                   microphone = sr.Microphone() 
      
                   for j in range(1,3):
                     text = recognize_speech_from_mic(recognizer, microphone)
                     print(text)
                     if text["transcription"]  == 'tiger':
                       print('good job') 
                       pygame.mixer.Sound.play(right)
                       pygame.mixer.music.stop()
                       break
                       
                       
                     else:
                            print('wrong')
                            pygame.mixer.Sound.play(wrong)
                            pygame.mixer.music.stop()
             time.sleep(4)
             
          
             if i== 2:
        
                     carImg = pygame.image.load(os.path.join(image_path,'monkey.jpg'))
                     gameDisplay.blit(carImg,(130,0))
                     pygame.display.update()
                     recognizer = sr.Recognizer()
                     microphone = sr.Microphone()
             
                     
                     for u in range(1,3):
                      text = recognize_speech_from_mic(recognizer, microphone)
                      print(text)   
                      if text["transcription"] == 'monkey':
                         print('good job') 
                         pygame.mixer.Sound.play(right)
                         pygame.mixer.music.stop()
                         break
                       
                      
                     else:
                         print('wrong')
                         pygame.mixer.Sound.play(wrong)
                         pygame.mixer.music.stop()
             time.sleep(4)
             

而不是使用 try-except 块(对于您要完成的事情来说,这是一个非常难看的模式,因为它不是您要捕获的实际错误,而是错误的 guess/input用户),以更有效的方式处理 r.recognize_google(audio) 的输出。

这里有一个迭代的解决方案,让玩家想猜多少次就猜多少次,直到他给出正确答案。它还循环遍历三个示例图像。

def get_audio(r, correct_answers):
   print('Say something!')
   with sr.Microphone() as source:
       audio = r.listen(source)    
       text = r.recognize_google(audio)

   if text in correct_answers:
        print('good job')
        return text
   else:
       print(text + ' was a wrong guess, try again!')
       return get_audio(r)


# Multiple Images are being guessed one after the other
carImg = pygame.image.load(os.path.join(image_path,'car.jpg'))
tigerImg = pygame.image.load(os.path.join(image_path,'tiger.jpg'))
lionImg = pygame.image.load(os.path.join(image_path,'lion.jpg'))
images = []
images.append( (carImg, ['car', 'auto']) )
images.append( (tigerImg, ['tiger', 'lion']) ) 
images.append( (lionImg, ['lion', 'multiple', 'answers']) ) 


for mytuple in images: 
    current_image, correct_answers = mytuple  # unpacking the tuple
    print("Continuing with the next image!")
    gameDisplay.blit(current_image, (130,0))
    pygame.display.update()
    r = sr.Recognizer()

    # we need to give get_audio the correct answer now, since it changes with each image:
    correct_guess = get_audio(r, correct_answers)  
    print('You guessed ' + correct_guess + '. This was correct!')

评论中关于您的问题中断的示例:我们猜测第一张图片在第一次尝试时“正确”,中断并继续第二个内部循环。

for i in range(1,3):
    print("outer: " + str(i))

    if i == 1:
        print("First picture coming up!")
        for j in range(1,3):
           print("inner_1: " + str(j))
           if 'tiger' == 'tiger':
               print('---> good job!')
               break

    if i == 2:
        print("Second picture coming up!")
        for u in range(1,3):
            print("inner_2: " + str(u))

输出:

>> outer: 1
>> First picture coming up!
>> inner_1: 1
>> ---> good job!
>> outer: 2
>> Second picture coming up!
>> inner_2: 1
>> inner_2: 2