听音识物——基于hTML+PYTHON+百度语音识别API接口

机器学习已经成为热点中的热点,百度语音接口很好的实现了对语音识别图像识别以及视频识别。
接下来记录的一点经验:
1.安装API接口
参考文档:
https://cloud.baidu.com/doc/SPEECH/s/4jwvy5zp1/

2.安装mysql
参考文档:
https://baijiahao.baidu.com/s?id=1632332658294840653&wfr=spider&for=pc

在这里插入图片描述
在这里插入图片描述
更改密码命令

 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.06 sec)

数据库安装成功之后安装了编辑软件:Navicat

安装在详细的数据包当中。

项目中用了flask进行连接前后台,如图代码:

APP.py界面

from flask import Flask,render_template,request
import pymysql
db = pymysql.connect("localhost", "root", "123456", "bird",charset="utf8")
from base64 import b64encode
from pprint import pprint
import requests
app = Flask(__name__)
import json

@app.route('/')
def hello_world():
    cursor = db.cursor()
    cursor.execute("select * from feek")
    date = cursor.fetchall()
    return render_template("index.html",date=date)
    # return render_template("index.html",date=date)
@app.route('/select',methods=["GET"])
def select():
    name = request.args.get("id")
    cursor = db.cursor()
    cursor.execute("select * from feek where name ='%s'"%(name))
    date = cursor.fetchall()
    print(type(date))
    db.commit()
    return json.dumps(date)

def aa(path):
        m = open(path, "rb")
        sound = str(b64encode(m.read()), 'utf-8')

        request_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/sound_cls/brids"

        params = '{"sound":"' + sound + '","top_num":"6"}'

        access_token = '24.4da10deb7e7da69e384c8165002b2046.2592000.1578159483.282335-17721968'
        request_url = request_url + "?access_token=" + access_token
        headers = {'content-type': 'application/json'}
        response = requests.post(request_url, data=params, headers=headers)
        if response:
            pprint(response.json())
            # return response.json()
            return "American_Robin"
@app.route('/api',methods=["GET"])
def api():
    path = request.args.get("path")
    name=aa(path)
    cursor = db.cursor()
    cursor.execute("select * from feek where name ='%s'"%(name))
    date = cursor.fetchall()
    db.commit()
    return json.dumps(date)

@app.route('/store',methods=["GET"])
def store():
    print("ewfwefwefwefewfwef")
    path = request.args.get("image64")
    print(path)
    cursor = db.cursor()
    db.commit()
    return json.dumps()
if __name__ == '__main__':
    app.run()

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="../static/js/jQuery.js"></script>

    <script  src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
    <script  src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/js/materialize.min.js"></script>
    <script  src="../static/classify.css"></script>

    <title>可编辑</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="../static/js/classify.js" type="text/css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/css/materialize.min.css" />
    <link href="https://fonts.googleapis.com/css?family=Noto+Sans" rel="stylesheet" />
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" />

</head>

<style>
    table{
        border: 1px solid #000;
        border-collapse: collapse;
        margin: auto;
    }
    th,td{
        width: 100px;
        height: 20px;
        text-align: center;
        border: 1px solid #000;
    }
</style>
<body>
 <table>

        <tr>
            <th>id</th>
            <th>名称</th>
            <th>简介</th>
        </tr>
        {% for row in date %}
         <tr>
            <td>{{row[0]}}</td>
            <td>{{row[1]}}</td>
            <td>{{row[2]}}</td>
        </tr>
        {% endfor %}
  <tr>
            <th>id</th>
            <th>名称</th>
            <th>简介</th>
  </tr>
    <button type="button" class="btn btn-danger" id="del">点我点我快点我</button>
 </table>
 <div id="dek">点我点我快点我</div>

 <h1>请上传你的鸟叫声</h1>
  <div class="container row main-container">
    <div class="col s12 dd-container">
      <div class="card dd-card">
        <div class="card-content black-text file-field">
          <div id="drop-container">
            <i class="material-icons large upload-icon">cloud_upload</i>
            <span class="drag-txt"><b>Drag and drop audio here</b></span>
            <span class="or-txt">or</span>
            <form action="#">
              <div class="waves-effect waves-light btn" id="browse-container">
                <span>Browse files</span>
                <input type="file" id="browse-button" accept="audio/*">
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
    <div class="col s12 uf-container">
      <div class="card uf-card">
        <span>source audio</span>
        <audio id="img-card"  controls></audio><br>
      </div>
      <div class="waves-effect waves-light btn" id="upload-button" >
        <span>Upload and Detect Audio</span>
      </div>
      <div class="waves-effect waves-light btn" id="go-back">
        <span>Go Back</span>
      </div>
    </div>
    <div class="col s12 dt-container">
      <div class="card dt-card" id="stat-table">

      </div>
      <div class="waves-effect waves-light btn" id="go-start">
        <span>Go Back</span>
      </div>
    </div>
    <div id="modal1" class="modal">
      <div class="modal-content">
{#        <div style="width: 100%; overflow: hidden;">#}
        <div style="width: 100%; ">
          <div style="width: 100px; float: left;">
            <div class="preloader-wrapper big active">
              <div class="spinner-layer spinner-blue-only">
                <div class="circle-clipper left">
                  <div class="circle">我是个圆</div>
                </div>
                <div class="gap-patch">
                  <div class="circle"></div>
                </div>
                <div class="circle-clipper right">
                  <div class="circle"></div>
                </div>
              </div>
            </div>
          </div>
          <div class="loading-text">
            <h5>Loading...</h5></div>
        </div>
      </div>
    </div>
  </div>
 <div class="up">rangtabaocunluj</div>
{# <script>#}
{##}
{##}
{#    #}
{##}
{#//在这里上传视频#}
{#</script>#}
<script>
    $('#dek').on("click",function () {
         id="American_Robin",
        $.ajax({
            url:"/select",
            data:{id:id},
            dataType:"json",
            success:(e)=>{
                if(e){
                    for(i=0;i<e.length;i++){
                $(`<tr><td class="id">${e[i][0]}</td><td attr="name">${e[i][1]}</td><td attr="feed">${e[i][2]}</td></tr>`).appendTo("table")
            }
                }
            }
        })
    });
    $(document).ready(function() {
        var dropContainer = document.getElementById('drop-container');
        dropContainer.ondragover = dropContainer.ondragend = function () {
            return false;
        };
        dropContainer.ondrop = function (e) {
            e.preventDefault();
            loadAudio(e.dataTransfer.files[0])
        };
        $("#browse-button").change(function () {
            loadAudio($("#browse-button").prop("files")[0]);
            $.ajax({
                type: "GET",
                url: '/api',
                data: {
                    path: "C:\\Users\\Administrator\\PycharmProjects\\untitled2\\XC14088_1.mp3"
                },
                dataType: 'json',
                success: function (e) {
                    if(e){
                    for(i=0;i<e.length;i++){
                $(`<tr><td class="id">${e[i][0]}</td><td attr="name">${e[i][1]}</td><td attr="feed">${e[i][2]}</td></tr>`).appendTo("table")
            }
                }
                },
            });

            $('#go-back, #go-start').click(function () {
                $('#img-card').removeAttr("src");
                $('#stat-table').html('');
                switchCard(0);
            });

            $('#upload-button').click(function () {
                $('.modal').modal('open');
            });
        });

        switchCard = function (cardNo) {
            var containers = [".dd-container", ".uf-container", ".dt-container"];
            var visibleContainer = containers[cardNo];
            for (var i = 0; i < containers.length; i++) {
                var oz = (containers[i] === visibleContainer) ? '1' : '0';
                $(containers[i]).animate({
                    opacity: oz
                }, {
                    duration: 200,
                    queue: false,
                }).css("z-index", oz);
            }
        };

        loadAudio = function (file) {
            var reader = new FileReader();
            reader.onload = function (event) {
                $('#img-card').attr('src', event.target.result);
            };
            reader.readAsDataURL(file);
            switchCard(1);
        };

        loadStats = function (jsonData) {
            switchCard(2);
            var data = JSON.parse(jsonData);
            if (data["success"] == true) {

                var markup = `
      <span>source audio</span><br>
      <audio id="result" src="/static/audio/XC14088_1.mp3" controls></audio><br>
      <span>target audio</span><br>
      <audio id="result2" src="/static/audio/XC14088_1.mp3" controls></audio><br>

      `;
                $("#stat-table").append(markup);

            }
        };
    });

</script>
</body>
</html>

后续详细情况继续更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值