Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法使用flask和render_模板选项获取所需的html页面_Javascript_Html_Flask_Audio_Backend - Fatal编程技术网

Javascript 无法使用flask和render_模板选项获取所需的html页面

Javascript 无法使用flask和render_模板选项获取所需的html页面,javascript,html,flask,audio,backend,Javascript,Html,Flask,Audio,Backend,我是web开发新手,我正在尝试构建一个在线记录器,并将一些数据作为音频反馈(通过单击index.html中的upload获得)发送回results.html 我试图在python flask app.py文件中生成一些print语句,以检查事件是否触发了“POST”,它可以工作并在命令提示符中打印我想要的数据列表。但最后它没有重定向到results.html,而是再次显示index.html 大部分代码和含义取自 Python文件 #!/usr/bin/env python # -*- co

我是web开发新手,我正在尝试构建一个在线记录器,并将一些数据作为音频反馈(通过单击index.html中的upload获得)发送回results.html

我试图在python flask app.py文件中生成一些print语句,以检查事件是否触发了“POST”,它可以工作并在命令提示符中打印我想要的数据列表。但最后它没有重定向到results.html,而是再次显示index.html

大部分代码和含义取自

Python文件

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
from flask import request
from flask import render_template
import os
import random
import datetime
from detections import clean_dir,get_speech_info
from shutil import copyfile
import numpy as np
import json
from flask import Flask, redirect, url_for, request

app = Flask(__name__)

questions = ['Tell Me About Yourself.',
'Why Should We Hire You?',
' What is Your Greatest Strength?',
'What is Your Greatest Weakness?',
'What Are Your Salary Expectations?',
'How Do You Handle Stress and Pressure?',
'Describe a Difficult Work Situation or Project and How You Handled It.',
'What Are Your Goals for The Future?',
]


@app.route('/<usr>',methods = ['GET'])  
def user(usr):
    return f"<h1>hello {usr}</h1>"

data = np.zeros((6))

@app.route("/", methods=['POST', 'GET'])
def index():
    num1 = random.randint(0, len(questions)-1)
    ques = questions[num1]
    

    if request.method == "POST":
        f  = request.files['audio_data']
        basepath = os.path.dirname(__file__)
        x = str(datetime.datetime.now()).replace(" ", "").replace(":","").replace(".","-")+'.wav'

        clean_dir(basepath+'/audio/')

        with open('audio/'+x, 'wb') as audio:
            f.save(audio)
        
        print('file uploaded successfully')

        global data
        data = np.array(get_speech_info(basepath+'/rough')[0])

        clean_dir(basepath+'/rough/')
        print(data,'post is fired')
        return render_template('results.html', data=data)

    else:
        print(data,'basic file')
        return render_template("index.html",q = ques)

if __name__ == "__main__":
    app.run(debug = True)
#/usr/bin/env python
#-*-编码:utf-8-*-
从烧瓶进口烧瓶
从烧瓶进口请求
从flask导入渲染模板
导入操作系统
随机输入
导入日期时间
从检测导入清洁目录,获取语音信息
从shutil导入复制文件
将numpy作为np导入
导入json
从烧瓶导入烧瓶,重定向,url\u,请求
app=烧瓶(名称)
问题=[“告诉我你自己的情况。”,
“我们为什么要雇用你?”,
“你最大的优点是什么?”,
“你最大的弱点是什么?”,
“你期望的薪水是多少?”,
“你如何处理压力?”,
“描述一个困难的工作环境或项目,以及你是如何处理的。”,
“你未来的目标是什么?”,
]
@app.route('/',方法=['GET'])
def用户(usr):
返回f“hello{usr}”
数据=np.零((6))
@app.route(“/”,方法=['POST','GET'])
def index():
num1=random.randint(0,len(问题)-1)
问题=问题[num1]
如果request.method==“POST”:
f=请求.文件['audio_data']
basepath=os.path.dirname(_文件__)
x=str(datetime.datetime.now()).replace(“,”).replace(“:”,”).replace(“,“-”)+”.wav'
clean_dir(basepath+/audio/)
以open('audio/'+x,'wb')作为音频:
f、 保存(音频)
打印('文件已成功上载')
全球数据
data=np.array(获取语音信息(basepath+/rough')[0])
清理目录(基本路径+'/rough/'))
打印(数据,'post已启动')
返回呈现模板('results.html',data=data)
其他:
打印(数据,“基本文件”)
返回render_模板(“index.html”,q=ques)
如果名称=“\uuuuu main\uuuuuuuu”:
app.run(debug=True)
JavaScript

//webkitURL is deprecated but nevertheless
URL = window.URL || window.webkitURL;

var gumStream;                      //stream from getUserMedia()
var rec;                            //Recorder.js object
var input;                          //MediaStreamAudioSourceNode we'll be recording

// shim for AudioContext when it's not avb.
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext //audio context to help us record

var recordButton = document.getElementById("recordButton");
var stopButton = document.getElementById("stopButton");
var pauseButton = document.getElementById("pauseButton");

//add events to those 2 buttons
recordButton.addEventListener("click", startRecording);
stopButton.addEventListener("click", stopRecording);
pauseButton.addEventListener("click", pauseRecording);

window.onload = function () { console.log({{ val }}) };
    function sendData() {
        var str = 'This is some data';
        $.ajax({
            url: '/newData',
            type: 'POST',
            data: str,
            success: function (res) { console.log(res) }, 
              // res is the response from the server 
              // (from return request.data)
            error: function (error) { console.log(error) }
        })
    }



function startRecording() {
    console.log("recordButton clicked");

    /*
        Simple constraints object, for more advanced audio features see
        https://addpipe.com/blog/audio-constraints-getusermedia/
    */

    var constraints = { audio: true, video:false }

    /*
        Disable the record button until we get a success or fail from getUserMedia()
    */

    recordButton.disabled = true;
    stopButton.disabled = false;
    pauseButton.disabled = false

    /*
        We're using the standard promise based getUserMedia()
        https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
    */

    navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
        console.log("getUserMedia() success, stream created, initializing Recorder.js ...");

        /*
            create an audio context after getUserMedia is called
            sampleRate might change after getUserMedia is called, like it does on macOS when recording through AirPods
            the sampleRate defaults to the one set in your OS for your playback device

        */
        audioContext = new AudioContext();

        //update the format
        document.getElementById("formats").innerHTML="Format: 1 channel pcm @ "+audioContext.sampleRate/1000+"kHz"

        /*  assign to gumStream for later use  */
        gumStream = stream;

        /* use the stream */
        input = audioContext.createMediaStreamSource(stream);

        /*
            Create the Recorder object and configure to record mono sound (1 channel)
            Recording 2 channels  will double the file size
        */
        rec = new Recorder(input,{numChannels:1})

        //start the recording process
        rec.record()

        console.log("Recording started");

    }).catch(function(err) {
        //enable the record button if getUserMedia() fails
        recordButton.disabled = false;
        stopButton.disabled = true;
        pauseButton.disabled = true
    });
}

function pauseRecording(){
    console.log("pauseButton clicked rec.recording=",rec.recording );
    if (rec.recording){
        //pause
        rec.stop();
        pauseButton.innerHTML="Resume";
    }else{
        //resume
        rec.record()
        pauseButton.innerHTML="Pause";

    }
}

function stopRecording() {
    console.log("stopButton clicked");

    //disable the stop button, enable the record too allow for new recordings
    stopButton.disabled = true;
    recordButton.disabled = false;
    pauseButton.disabled = true;

    //reset button just in case the recording is stopped while paused
    pauseButton.innerHTML="Pause";

    //tell the recorder to stop the recording
    rec.stop();

    //stop microphone access
    gumStream.getAudioTracks()[0].stop();

    //create the wav blob and pass it on to createDownloadLink
    rec.exportWAV(createDownloadLink);
}

function createDownloadLink(blob) {

    var url = URL.createObjectURL(blob);
    var au = document.createElement('audio');
    var li = document.createElement('li');
    var link = document.createElement('a');

    //name of .wav file to use during upload and download (without extendion)
    var filename = new Date().toISOString();

    //add controls to the <audio> element
    au.controls = true;
    au.src = url;

    //save to disk link
    link.href = url;
    link.download = filename+".wav"; //download forces the browser to donwload the file using the  filename
    link.innerHTML = "Save to disk";

    //add the new audio element to li
    li.appendChild(au);

    //add the filename to the li
    li.appendChild(document.createTextNode(filename+".wav "))

    //add the save to disk link to li
    li.appendChild(link);

    //upload link
    var upload = document.createElement('a');
    upload.href="/";
    upload.innerHTML = "Predict";
    upload.addEventListener("click", function(event){
          var xhr=new XMLHttpRequest();
          xhr.onload=function(e) {
              if(this.readyState === 4) {
                  console.log("Server returned: ",e.target.responseText);
              }
          };
          var fd=new FormData();
          fd.append("audio_data",blob, filename);
          xhr.open("POST","/",true);
          xhr.send(fd);
    })
    li.appendChild(document.createTextNode (" "))//add a space in between
    li.appendChild(upload)//add the upload link to li

    //add the li element to the ol
    recordingsList.appendChild(li);
}
//webkitURL不推荐使用,但
URL=window.URL | | window.webkitURL;
var gumStream//来自getUserMedia()的流
var-rec//Recorder.js对象
var输入//我们将录制MediaStreamAudioSourceNode
//当不是avb时,为AudioContext填充。
var AudioContext=window.AudioContext | | window.webkitadiocontext;
var audioContext//audio context帮助我们录制
var recordButton=document.getElementById(“recordButton”);
var stopButton=document.getElementById(“stopButton”);
var pauseButton=document.getElementById(“pauseButton”);
//向这两个按钮添加事件
recordButton.addEventListener(“单击”,开始录制);
stopButton.addEventListener(“单击”,停止录制);
pauseButton.addEventListener(“单击”,暂停录制);
window.onload=function(){console.log({val}}});
函数sendData(){
var str='这是一些数据';
$.ajax({
url:“/newData”,
键入:“POST”,
数据:str,
成功:函数(res){console.log(res)},
//res是来自服务器的响应
//(来自退货请求。数据)
错误:函数(错误){console.log(错误)}
})
}
函数startRecording(){
日志(“单击记录按钮”);
/*
简单约束对象,有关更高级的音频功能,请参见
https://addpipe.com/blog/audio-constraints-getusermedia/
*/
var约束={audio:true,video:false}
/*
禁用录制按钮,直到我们从getUserMedia()获得成功或失败
*/
recordButton.disabled=true;
stopButton.disabled=false;
pauseButton.disabled=false
/*
我们使用的是基于承诺的标准getUserMedia()
https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
*/
navigator.mediaDevices.getUserMedia(约束)。然后(函数(流){
log(“getUserMedia()成功,创建流,初始化Recorder.js…”);
/*
在调用getUserMedia后创建音频上下文
sampleRate在调用getUserMedia后可能会更改,就像在macOS上通过AirPods录制时一样
采样器默认为操作系统中为播放设备设置的采样器
*/
audioContext=新的audioContext();
//更新格式
document.getElementById(“格式”).innerHTML=“格式:1通道pcm@”+audioContext.sampleRate/1000+“kHz”
/*分配给gumStream供以后使用*/
gumStream=溪流;
/*使用流*/
输入=audioContext.createMediaStreamSource(流);
/*
创建Recorder对象并配置为录制单声道声音(1个通道)
录制2个频道将使文件大小加倍
*/
rec=新记录器(输入,{numChannels:1})
//开始录制过程
记录
console.log(“记录已开始”);
}).catch(函数(err){
//如果getUserMedia()失败,请启用录制按钮
recordButton.disabled=false;
stopButton.disabled=true;
pauseButton.disabled=true
});
}
函数暂停录制(){
console.log(“pauseButton clicked rec.recording=“,rec.recording”);
如果(记录){
//停顿
建议停止();
pauseButton.innerHTML=“Resume”;
}否则{
//恢复
记录
pauseButton.innerHTML=“暂停”;
}
}
函数stopRecording(){
日志(“单击停止按钮”);
//禁用停止按钮,启用记录也允许新建
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Simple Recorder.js demo with record, stop and pause - addpipe.com</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
  <script src="https://cdn.rawgit.com/mattdiamond/Recorderjs/08e7abd9/dist/recorder.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">



</head>

<body>
  <nav class="navbar navbar-dark bg-primary">
    <a class="navbar-brand" href="#">
      My Prosodic Features
    </a>
  </nav>

  <div id ="controls" class="container">
  <div class="row">
    <div class="col-sm">
      <div class="jumbotron">
        <h1 class="display-4">Hello, People!</h1>
        <p class="lead">This is a simple unit,since its campus placement season I wanted to help my friends to know about their prosodic skills.</p>
        <p class='lead'>I have used python libraries like librosa and PRAAT for speech processing techniques along with Tensorflow for detection of certain tasks</p>

        <hr class="my-4">
        <p>We just wanted you know that the results are not 100% accurate, but it provides you some idea about your presentation and speaking skills.</p>
        <p>What we do is give some random question and you try to answer it. Make sure you limit yourself upto 1 minute for a preparation time so that you can perform well in real interviews. </p>
        <!-- <p class="lead">
        <a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a>
      </p> -->
      </div>
    </div>
    <div  class="col-sm">
      <div class="col-sm">
        <div class="jumbotron">
          <h1 class="display-4"> Question</h1>
          <p class = 'lead'>{{ q }}</p>

          <p class = 'lead'>
            <ol>
            <li>Click on Record button</li>
            <li>Speak for 5 minutes</li>
            <li>Click on upload button</li>
            </ol>
            <hr class="my-4">

          </p>

                  <button id="recordButton">Record</button>
                  <button id="pauseButton" disabled>Pause</button>
                  <button id="stopButton" disabled>Stop</button>
                  <div id="formats">Format: start recording to see sample rate</div>
                  <p><strong>Recordings:</strong></p>
                  <ol id="recordingsList"></ol>

        </div>
      </div>

    </div>

  </div>

</div>
  <!-- <div id="controls">
    <button id="recordButton">Record</button>
    <button id="pauseButton" disabled>Pause</button>
    <button id="stopButton" disabled>Stop</button>
  </div>
  <div id="formats">Format: start recording to see sample rate</div>
  <p><strong>Recordings:</strong></p>
  <ol id="recordingsList"></ol> -->
  <!-- inserting these scripts at the end to be able to use all the elements in the DOM -->
  <script src="https://code.jquery.com/jquery-3.5.1.js"></script>

  <script src="https://cdn.rawgit.com/mattdiamond/Recorderjs/08e7abd9/dist/recorder.js"></script>
  
  <script src={{ url_for('static', filename='js/app.js') }}></script>

</body>

</html>
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
  <script src="https://cdn.rawgit.com/mattdiamond/Recorderjs/08e7abd9/dist/recorder.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

</head>

<!-- nsyll, npause, dur (s), phonationtime (s), speechrate (nsyll/dur), articulation rate (nsyll / phonationtime), -->

<body>
    <table class="table">
        <thead class="thead-dark">
          <tr>
            <th scope="col">#</th>
            <th scope="col">Feature</th>
            <th scope="col">Result</th>
            <th scope="col">Average Result</th>
          </tr>
        </thead>
        <tbody>

          <tr>
            <th scope="row">1</th>
            <td>nsyll</td>
            <td>{{ data[0] }}</td>
            <td>@fat</td>
          </tr>
          <tr>
            <th scope="row">2</th>
            <td>npause</td>
            <td>{{ data[1] }}</td>
            <td>@twitter</td>
          </tr>
          <tr>
            <th scope="row">3</th>
            <td>dur</td>
            <td>{{ data[2] }}</td>
            <td>@twitter</td>
          </tr>
          <tr>
            <th scope="row">4</th>
            <td>phonationtime</td>
            <td>{{ data[3] }}</td>
            <td>@twitter</td>
          </tr>
          <tr>
            <th scope="row">5</th>
            <td>speechrate</td>
            <td>{{ data[4] }}</td>
            <td>@twitter</td>
          </tr>
          <tr>
            <th scope="row">6</th>
            <td>articulation rate</td>
            <td>{{ data[5] }}</td>
            <td>@twitter</td>
          </tr>
        </tbody>
      </table>
</body>
project
|---run.py
|---templates
|   |---index.html
|   |---results.html
|---static
    |---js
        |---app.js