语音识别崩溃

时间:2020-09-16 22:23:49

标签: python pygame raspberry-pi

我正在尝试在pygame上制作一个显示不同图像的游戏,并且用户应通过语音识别为图像提供答案。程序崩溃并显示以下消息: enter image description here 有时它会在第一个图像之后退出,有时在中间,我不确定是哪里出了问题。请帮忙。

谢谢。

import os
import glob
import pygame
import time
import speech_recognition as sr

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
alpha = (0,88,255)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('GUI Speech Recognition')

current_path = os.path.dirname(__file__) 
#resource_path = os.path.join(current_path, 'test') 
image_path = os.path.join('MG') 


    


def close():
   pygame.quit()
   quit()

def message_display(text):
   largeText = pygame.font.Font('freesansbold.ttf',30)
   TextSurf, TextRect = text_objects(text, largeText)
   TextRect.center = ((display_width/2),(display_height/2))
   gameDisplay.blit(TextSurf, TextRect)

   pygame.display.update()


def text_objects(text, font):
   textSurface = font.render(text, True, alpha)
   return textSurface, textSurface.get_rect()

def button(msg,x,y,w,h,ic,ac,action=None):
   mouse = pygame.mouse.get_pos()
   click = pygame.mouse.get_pressed()
   if x+w > mouse[0] > x and y+h > mouse[1] > y:
       pygame.draw.rect(gameDisplay, ac,(x,y,w,h))

       if click[0] == 1 and action != None:
           action()         
   else:
       pygame.draw.rect(gameDisplay, ic,(x,y,w,h))

   smallText = pygame.font.SysFont("comicsansms",20)
   textSurf, textRect = text_objects(msg, smallText)
   textRect.center = ( (x+(w/2)), (y+(h/2)) )
   gameDisplay.blit(textSurf, textRect)

def load_the_image(image):
    return pygame.image.load(os.path.join(image_path,image))

images = [
    load_the_image('cat.jpg'),
    load_the_image('monkey.jpg'),
    load_the_image('dog.jpg'),
     load_the_image('green.jpg'),
      load_the_image('red.jpg'),
       load_the_image('yellow.jpg')
]




def s2t():
  
          
     
   

   for i in range(1,7):
       
       
       
       if i== 1:
          
           carImg = pygame.image.load(os.path.join(image_path,'cat.jpg'))
           gameDisplay.blit(carImg,(130,0))
           pygame.display.update()
           r = sr.Recognizer()
           with sr.Microphone() as source:
                 print ('Say Something!')
                 audio = r.listen(source)
               
    
           text = r.recognize_google(audio)
           print(text)
           if text == 'cat':
               message_display('good job')
               
           else:
               message_display('wrong')
       elif i== 2:
           
           carImg = pygame.image.load(os.path.join(image_path,'monkey.jpg'))
           gameDisplay.blit(carImg,(130,0))
           pygame.display.update()
           r = sr.Recognizer()
           with sr.Microphone() as source:
                 print ('Say Something!')
                 audio = r.listen(source)
               
    
           text = r.recognize_google(audio)
           print(text)
           
           if text == 'monkey':
               message_display('good job')
           else:
               message_display('wrong')
       elif i== 3:
           
           carImg = pygame.image.load(os.path.join(image_path,'dog.jpg'))
           gameDisplay.blit(carImg,(130,0))
           pygame.display.update()
           r = sr.Recognizer()
           with sr.Microphone() as source:
                 print ('Say Something!')
                 audio = r.listen(source)
               
    
           text = r.recognize_google(audio)
           print(text)
           
           if text == 'dog':
               message_display('good job')
           else:
               message_display('wrong')
       elif i== 4:
           
           carImg = pygame.image.load(os.path.join(image_path,'green.jpg'))
           gameDisplay.blit(carImg,(130,0))
           pygame.display.update()
           r = sr.Recognizer()
           with sr.Microphone() as source:
                 print ('Say Something!')
                 audio = r.listen(source)
               
    
           text = r.recognize_google(audio)
           print(text)
           
           if text == 'green':
               message_display('good job')
           else:
               message_display('wrong')
       elif i== 5:
           
           carImg = pygame.image.load(os.path.join(image_path,'red.jpg'))
           gameDisplay.blit(carImg,(130,0))
           pygame.display.update()
           r = sr.Recognizer()
           with sr.Microphone() as source:
                 print ('Say Something!')
                 audio = r.listen(source)
               
    
           text = r.recognize_google(audio)
           print(text)
           
           if text == 'red':
               message_display('good job')
           else:
               message_display('wrong')
       elif i== 6:
           
           carImg = pygame.image.load(os.path.join(image_path,'yellow.jpg'))
           gameDisplay.blit(carImg,(130,0))
           pygame.display.update()
           r = sr.Recognizer()
           with sr.Microphone() as source:
                 print ('Say Something!')
                 audio = r.listen(source)
               
    
           text = r.recognize_google(audio)
           print(text)
           
           if text == 'yellow':
               message_display('good job')
           else:
               message_display('wrong')
               

def main():
   while True:
       for event in pygame.event.get():
           if event.type == pygame.QUIT:
               pygame.quit()
               quit()
       button("Play!",150,450,100,50,green,bright_green,s2t)
       button("Quit",550,450,100,50,red,bright_red,close)
       pygame.display.update()

if __name__ == '__main__':
   main()

1 个答案:

答案 0 :(得分:0)

好像您需要一些exception handling,非常粗糙的初始步骤如下:

try:
    text = r.recognize_google(audio)
except:
    print("Failed to recognize sound")
    text = ""

理想情况下,您将需要进行更具体的异常处理。您也可以使用traceback.print_exc()打印完整的追溯。或使用breakpoint()内置函数进入调试器。