jetbot09 之用语音识别 玩脑筋急转弯
jetbot玩脑筋急转弯2语音转文字用的百度api, 文字转语音用的 硬件 xfs5152 .题目是文本格式每个题一行, 格式如下:问题1-正确答案-匹配正确关键词1|匹配正确关键词2 ...问题2-正确答案-匹配正确关键词1|匹配正确关键词2 ...下面是部分实现:import timeimport Speechfrom voice_capture import VoiceCaptureimpo
·
jetbot玩脑筋急转弯2
语音转文字用的百度api, 文字转语音用的 硬件 xfs5152 .
题目是文本格式每个题一行, 格式如下:
问题1-正确答案-匹配正确关键词1|匹配正确关键词2 ...
问题2-正确答案-匹配正确关键词1|匹配正确关键词2 ...
下面是部分实现:
import time
import Speech
from voice_capture import VoiceCapture
import random
from wav2text import pcm2text
from cam_ptz import cam_node_head, cam_shake_head
class Question(object):
def __init__(self):
self.question = ''
self.best_answer = ''
self.match_answer = []
def __str__(self):
return '%s-%s-%s' %(self.question, self.best_answer, self.match_answer)
def load_all_questons(fname):
ret = []
print(fname)
with open(fname, "r") as f:
while True:
s = f.readline()
if not s:
break
s = s.strip('\n')
words = s.split('-')
if len(words) < 3:
continue
q = Question()
q.question = words[0]
q.best_answer = words[1]
q.match_answer = words[2].split('|')
ret.append(q)
return ret
class BrainTwist(object):
def __init__(self):
self.all_questions = load_all_questons('brain.txt')
self.cur_question = None
self.voice_cap = VoiceCapture(self.on_voice_captured)
self.success_tokens = ['很好', '你真棒', '你怎么知道的', '真厉害']
self.fail_tokens = ['弄弄弄', '再好好翔想', '差一点啦']
self.start_token = ['开始', "吡", "吡一下开始", "你来答"]
self.question_answered = False
Speech.SetReader(Speech.Reader_Type["Reader_XuXiaoBao"])
Speech.SetVolume(8)
Speech.SetSpeed(5)
def run(self):
i = 0
self.voice_cap.start()
self.voice_cap.paused()
num = len(self.all_questions)
while True:
q = self.all_questions[i]
time.sleep(1)
# 开始下一题读题
Speech.Block_Speech_text(q.question)
start_token = random.choice(self.start_token)
Speech.Block_Speech_text(start_token)
# 听你回答
self.cur_question = q
self.question_answered = False
self.voice_cap.resume()
# 等你回答完毕
while not self.question_answered:
time.sleep(0.1)
i += 1
if i == num:
i = 0
def on_voice_captured(self, pcm):
if not self.cur_question:
self.question_answered = True
return
self.voice_cap.paused()
ret = pcm2text(pcm)
if not ret:
Speech.Block_Speech_text('没有听清')
Speech.Block_Speech_text('正确答案是:' + self.cur_question.best_answer)
self.question_answered = True
self.cur_question = None
return
for ans in self.cur_question.match_answer:
if ans in ret:
success_token = random.choice(self.success_tokens)
Speech.Speech_text(success_token)
cam_node_head()
Speech.Speech_wait()
self.question_answered = True
success_token = random.choice(self.fail_tokens)
Speech.Block_Speech_text(success_token)
Speech.Speech_text('正确答案是:' + self.cur_question.best_answer)
cam_shake_head()
Speech.Speech_wait()
self.question_answered = True
self.cur_question = None
def main():
brain_twist = BrainTwist()
brain_twist.run()
#def on_voice_capture_ready()
if __name__ == '__main__':
main()
更多推荐
已为社区贡献2条内容
所有评论(0)