"OSError:没有可用的默认输入设备"在Google Colab上 [英] "OSError: No Default Input Device Available" on Google Colab

查看:92
本文介绍了"OSError:没有可用的默认输入设备"在Google Colab上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用"pip3 install pyaudio"安装了pyaudio.在Google Colab上基于

I installed pyaudio using "pip3 install pyaudio" on Google Colab based on this answer from angelokh's.

Then, I got this error,

OSError: No Default Input Device Available

on Google Colab.

The code is shown below.

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

解决方案

You can't use the mic from Google Colab directly as you do with your own machine. You have to use JavaScript to let the browser enable the mic. This can be done using the following code found 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

Now you can use the record() function to record the audio. This function returns the audio as byte-stream. You can try it yourself on colab following this link:

这篇关于"OSError:没有可用的默认输入设备"在Google Colab上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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