• firefox 实现web交互机器人


    现在仅有火狐浏览器可以这样操作 -- Filefox

    下面是项目目录 -- 

    前端页面 -- html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>我是玩具</title>
    </head>
    <body>
    
    <p><audio id="player" controls autoplay  ></audio></p>  <!--  src  -->
    <button onclick="start_reco()">录音</button>
    <button onclick="stop_reco()">发送语音</button>
    <div id="content"></div>
    </body>
    <script type="text/javascript" src="/static/Recorder.js"></script>
    <script type="text/javascript" src="/static/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
        var serv = "http://192.168.11.173:9009";
    
        var reco = null;
        var audio_context = new AudioContext(); //音频内容对象  音频DOM 对象
        navigator.getUserMedia = (navigator.getUserMedia ||     //获取浏览器
            navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia ||
            navigator.msGetUserMedia);
    
        navigator.getUserMedia({audio:true}, create_stream, function (err) {
            console.log(err)                //success回调函数    error
        });
    
        function create_stream(user_media) {
            var stream_input = audio_context.createMediaStreamSource(user_media);  //创建流媒体容器
            reco = new Recorder(stream_input);   // 录音的js
        }
    
    
        function start_reco() {
            reco.record();   // 开始录音
        }
    
        function stop_reco() {
            reco.stop();     // 停了
    
            reco.exportWAV(function (wav_file) {     // 输出音频
                console.log(wav_file);
                var formdata = new FormData();      // form 表单 {key:value}
                formdata.append("reco",wav_file);  // form input type="file"
                formdata.append("key","value");   // str 值int 文件 随便填
                // # <input type="text" name = "key"> value
                $.ajax({
                url: serv + "/upload",
                type: 'post',
                processData: false,   // 校验格式
                contentType: false,   // 检查内容
                data: formdata,
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    if(data.code == 0){
                        document.getElementById("player").src = "http://192.168.11.173:9009/get_file/"+data.filename;
                        document.getElementById("content").innerText = data.content;
    
                    }
                }
                })
            });
    
            reco.clear();    // 清空
        }
      
    </script>
    </html>

    项目 主 py 文件

    from flask import Flask, render_template, request, jsonify, send_file
    from uuid import uuid4
    from other import audio2text,text2audio,my_nlp
    app = Flask(__name__)
    
    @app.route("/")
    def index():
        return render_template("WebToy.html")
    
    @app.route("/upload",methods=["POST"])
    def upload():
        fi = request.files.get("reco")
        fi_name = f"{uuid4()}.wav"
        fi.save(fi_name)
    
        text = audio2text(fi_name)
        new_text = my_nlp(text)
        filename = text2audio(new_text)
    
        ret = {
            "filename":filename,
            "content":new_text,
            "code":0
        }
    
        return jsonify(ret)
    
    @app.route("/get_file/<filename>")
    def get_file(filename):
        return send_file(filename)
    
    if __name__ == '__main__':
        app.run("0.0.0.0",9009,debug=True)

    其他  py  文件

    import os
    from aip import AipSpeech
    from aip import AipNlp
    from uuid import uuid4
    
    APP_ID = '15845079'
    API_KEY = '5GGaPeBu2I0LsONjlWhM04yL'
    SECRET_KEY = 'Uo8zIi8VRZMRgqKkCQaqaGIorsQtkDlw'
    
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)  # 实例化
    nlp_client = AipNlp(APP_ID, API_KEY, SECRET_KEY)  # 实例化
    
    # 读取文件
    def get_file_content(filePath):
        os.system(f'ffmpeg -y  -i {filePath}  -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm')
        with open(f'{filePath}.pcm', 'rb') as fp:
            return fp.read()
    
    
    def audio2text(filePath):
        res = client.asr(get_file_content(filePath), 'pcm', 16000, {
            'dev_pid': 1536,
        })
        text = res.get('result')[0]
        # print(text)
    
        return text
    
    # 智能问答
    import requests  # 请求模块
    
    def to_tuling(text, uid):  # 问题
        data = {
            "reqType": 0,
            "perception": {
                "inputText": {
                    "text": "上海"
                },
            },
            "userInfo": {
                "apiKey": "29f1be0165d74f7290a8cd899a4358a8",
                "userId": "123"
            }
        }
        data['perception']['inputText']['text'] = text
        data['userInfo']['userId'] = uid
        res = requests.post('http://openapi.tuling123.com/openapi/api/v2', json=data)
        print(res)
    
        res_json = res.json()
        text = res_json.get('results')[0].get('values').get('text')
        return text
    
    def my_nlp(text):
        if nlp_client.simnet(text, '你叫什么名字').get('score') >= 0.77:
            A = '我叫jay吧'
            return A
    
        if nlp_client.simnet(text, '你今年几岁了').get('score') >= 0.77:
            A = '今年999岁'
            return A
        A = to_tuling(text, '007')
        return A
    
    def text2audio(text):
        result = client.synthesis(text, 'zh', 1, {
            'vol': 5,
            'per': 4,
            'spd': 4,
            'pit': 7
        })
        filename = f'{uuid4()}.mp3'
    
        # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
        if not isinstance(result, dict):
            # with open(f'{time.time()}.mp3', 'wb') as f:
            with open(filename, 'wb') as f:
                f.write(result)
        return filename
  • 相关阅读:
    [原]使用ASP.NET MVC构建RESTful服务
    [原]ASP.NET MVC 3 使用 DotNetOpenAuth 实现SSO
    ASP.NET MVC + RESTful服务之HttpStatusResult
    [原]ASP.NET MVC 3 Razor 多国语言参考解决方案 补充三
    [原]ASP.NET MVC 3 使用jqGrid之TreeGrid
    [原]ASP.NET MVC 3 Razor 多国语言参考解决方案 补充
    vsftp
    对付CC攻击不必动用防火墙
    Symfony and Godaddy
    godaddy的VPS使用SSH登录的方法
  • 原文地址:https://www.cnblogs.com/zhangchen-sx/p/10603476.html
Copyright © 2020-2023  润新知