如何解决语音识别崩溃
我正在尝试在pygame上制作一个显示不同图像的游戏,并且用户应通过语音识别为图像提供答案。程序崩溃并显示以下消息: 有时它会在第一个图像之后退出,有时在中间,我不确定是哪里出了问题。请帮忙。
谢谢。
import os
import glob
import pygame
import time
import speech_recognition as sr
pygame.init()
display_width = 800
display_height = 600
black = (0,0)
alpha = (0,88,255)
white = (255,255,255)
red = (200,0)
green = (0,200,0)
bright_red = (255,0)
bright_green = (0,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,(x,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gamedisplay,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,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,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,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,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,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,red,bright_red,close)
pygame.display.update()
if __name__ == '__main__':
main()
解决方法
好像您需要一些exception handling,非常粗糙的初始步骤如下:
try:
text = r.recognize_google(audio)
except:
print("Failed to recognize sound")
text = ""
理想情况下,您将需要进行更具体的异常处理。您也可以使用traceback.print_exc()打印完整的追溯。或使用breakpoint()内置函数进入调试器。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。