问题描述
我正在开发基于语音识别的游戏,其中用户必须识别显示的图像并提供答案。我有一个问题,如果他们的第一个答案有误或对于显示的图像无法识别,该如何允许用户提供多个答案。我尝试使用try:和多个,但无法使其正常工作。
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,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,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块(这是您要完成的工作的非常难看的模式,因为它不是您要捕获的实际错误,而是用户的错误猜测/输入),请处理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