有没有办法在 Azure Notebooks/Google Collab 上输入麦克风? [英] Is there any way to input microphone on Azure Notebooks/ Google Collab?

查看:47
本文介绍了有没有办法在 Azure Notebooks/Google Collab 上输入麦克风?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在 Google Collab 和 Azure Notebook 上使用 Python Notebooks 构建语音转文本应用程序.我面临的问题是基于云的 VM 没有用于使用我的麦克风的音频端口.因此,我什至无法安装 PyAudio 包.有什么建议吗?

I've been trying to build a speech to text application using Python Notebooks on Google Collab and Azure Notebook. The problem I'm facing is that the cloud based VM does not have an audio port for using my microphone. Due to this, I'm even unable to install PyAudio Package. Any suggestions?

推荐答案

这是在 Colab 中记录和保存文件的代码.

Here is the code to record and save the file in Colab.

from IPython.display import Javascript
from google.colab import output
from base64 import b64decode

RECORD = """
const sleep  = time => new Promise(resolve => setTimeout(resolve, time))
const b2text = blob => new Promise(resolve => {
  const reader = new FileReader()
  reader.onloadend = e => resolve(e.srcElement.result)
  reader.readAsDataURL(blob)
})
var record = time => new Promise(async resolve => {
  stream = await navigator.mediaDevices.getUserMedia({ audio: true })
  recorder = new MediaRecorder(stream)
  chunks = []
  recorder.ondataavailable = e => chunks.push(e.data)
  recorder.start()
  await sleep(time)
  recorder.onstop = async ()=>{
    blob = new Blob(chunks)
    text = await b2text(blob)
    resolve(text)
  }
  recorder.stop()
})
"""

def record(sec=3):
  display(Javascript(RECORD))
  s = output.eval_js('record(%d)' % (sec*1000))
  b = b64decode(s.split(',')[1])
  with open('audio.wav','wb') as f:
    f.write(b)
  return 'audio.wav'

这篇关于有没有办法在 Azure Notebooks/Google Collab 上输入麦克风?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆