jetbot09 之用语音识别 玩脑筋急转弯

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()
                     

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

walletiger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值