用于Flask的HTML麦克风输入

2024-04-27 22:55:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个带有Flask和speech recognition的web应用程序,用于音频输入、处理和在屏幕上显示输出This post帮助我组织音频文件的输入,以下是我的代码:

list_msg = []

@app.route("/process_wav", methods=['GET', 'POST'])
def process_wav():
    if request.method == 'POST':
        audio_file = request.form.get("audio-recording")
        dir = 'audio/'+str(audio_file)
        msg = Mensaje(dir)  # this is a custom class I created for storing and processing the audio
        list_msg.append((dir, msg.sentence, msg.word_counter))
    return render_template("process_wav.html", list_msg=list_msg)

和我的HTML代码:

{% block flask %}
<h1>Prototype</h1>

<section>
<form action="" method="POST" class="form-input">
<div class="form-input">
  <label for="audio-recording">Audio file (.wav)</label>
  <input name="audio-recording" type="file" accept="audio/*" id="audio-recording" >
  <button class="form-submit" type="submit" id="entry">Submit</button>
</div>
</form>
</section>

{% for msg in list_msg %}
<section class="form-output" id="output">
  <h3>{{ msg[0] }}</h3>
  <p>Result: "{{ msg[1] }}"</p>
  <p>Counter: {{ msg[2] }}</p>
</section>
{% endfor %}
{% endblock flask %}

但我发现有必要通过麦克风直接输入音频(并以与.wav音频文件相同的方式处理)。所以我的问题是:如何通过麦克风将音频以HTML格式输入到网页,然后将其传递给Flask和python进行处理,然后显示输出

由于对Flask和HTML不熟悉,我发现很难找到好的参考资料,尽管我发现HTML本身并没有提供这一功能。欢迎任何意见


Tags: formflaskhtmldirsectionmsg音频post
1条回答
网友
1楼 · 发布于 2024-04-27 22:55:06

函数使用flask实时传输音频。事实上我没有得到你真正想要做的。但通过这种方式,您可以在flask中传输音频

@app.route("/fetchAudio",methods=["GET"])
def fetch_audio():
    def generate():
       with open("SampleAudio.mp3", "rb") as audio: # your audio file path here
          audio_data = audio.read(1024)
       while audio_data:
            yield audio_data
            audio_data = audio.read(1024)
    return Response(generate(), mimetype="audio/x-wav") # header for audio type

我希望这会有帮助github_link

相关问题 更多 >