pip3在Google Colab上安装pyaudio

时间:2019-11-24 00:54:25

标签: google-colaboratory pyaudio

通过angelokh's的回答,我在Google Colab上通过“ pip3 install pyaudio”成功安装了pyaudio。 然后,我得到了错误,

OSError: No Default Input Device Available

在Google Colab上。

代码在下面。

import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
    r.adjust_for_ambient_noise(source)
    audio = r.listen(source)

1 个答案:

答案 0 :(得分:1)

您不能像使用自己的计算机一样直接使用Google Colab的麦克风。您必须使用JavaScript才能让浏览器启用麦克风。可以使用以下找到的here代码进行此操作:

# all imports
from io import BytesIO
from base64 import b64decode
from google.colab import output
from IPython.display import Javascript

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):
  print("Speak Now...")
  display(Javascript(RECORD))
  sec += 1
  s = output.eval_js('record(%d)' % (sec*1000))
  print("Done Recording !")
  b = b64decode(s.split(',')[1])
  return b #byte stream

现在,您可以使用record()功能来录制音频。此函数将音频作为字节流返回。您可以通过以下链接在colab上尝试一下: