micmon/notebooks/predict.ipynb

173 lines
24 KiB
Text
Raw Normal View History

2020-10-27 15:21:32 +01:00
{
"cells": [
{
"cell_type": "markdown",
"source": [
"This notebook uses a pre-trained Tensorflow model to make live\n",
"predictions on the audio samples recorded from a microphone."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DEBUG:matplotlib:(private) matplotlib data path: /usr/lib/python3.8/site-packages/matplotlib/mpl-data\n",
"DEBUG:matplotlib:matplotlib data path: /usr/lib/python3.8/site-packages/matplotlib/mpl-data\n",
"DEBUG:matplotlib:CONFIGDIR=/home/blacklight/.config/matplotlib\n",
"DEBUG:matplotlib:matplotlib version 3.3.2\n",
"DEBUG:matplotlib:interactive is False\n",
"DEBUG:matplotlib:platform is linux\n",
"DEBUG:matplotlib:loaded modules: ['sys', 'builtins', '_frozen_importlib', '_imp', '_warnings', '_frozen_importlib_external', '_io', 'marshal', 'posix', '_thread', '_weakref', 'time', 'zipimport', '_codecs', 'codecs', 'encodings.aliases', 'encodings', 'encodings.utf_8', '_signal', '__main__', 'encodings.latin_1', '_abc', 'abc', 'io', '_stat', 'stat', '_collections_abc', 'genericpath', 'posixpath', 'os.path', 'os', '_sitebuiltins', '_locale', '_bootlocale', 'types', 'importlib._bootstrap', 'importlib._bootstrap_external', 'warnings', 'importlib', 'importlib.machinery', 'importlib.abc', '_operator', 'operator', 'keyword', '_heapq', 'heapq', 'itertools', 'reprlib', '_collections', 'collections', '_functools', 'functools', 'contextlib', 'importlib.util', 'google', 'google.assistant', 'google.assistant.embedded', 'googlesamples', 'googlesamples.assistant', 'google.cloud', 'google.logging', 'google.iam', 'mpl_toolkits', 'ruamel', 'sphinxcontrib', 'zope', 'site', '_weakrefset', 'weakref', 'pkgutil', 'runpy', 'ipykernel._version', '__future__', 'enum', '_sre', 'sre_constants', 'sre_parse', 'sre_compile', 'copyreg', 're', '_json', 'json.scanner', 'json.decoder', 'json.encoder', 'json', 'errno', 'signal', 'threading', '_posixsubprocess', 'select', 'collections.abc', 'math', 'selectors', 'subprocess', 'IPython.core', 'IPython.core.getipython', 'IPython.core.release', 'atexit', 'copy', 'fnmatch', 'glob', 'token', 'tokenize', 'linecache', 'traceback', '_string', 'string', 'logging', 'zlib', '_compression', '_bz2', 'bz2', '_lzma', 'lzma', 'pwd', 'grp', 'shutil', '_opcode', 'opcode', 'dis', 'inspect', '_struct', 'struct', 'six', 'traitlets.utils', 'traitlets.utils.getargspec', 'ipython_genutils._version', 'ipython_genutils', 'locale', 'ipython_genutils.encoding', 'ipython_genutils.py3compat', 'traitlets.utils.importstring', 'traitlets.utils.sentinel', 'traitlets.utils.bunch', 'traitlets.traitlets', 'traitlets._version', 'traitlets', 'decorator', 'gettext', 'argparse', '_ast', 'ast', '_bisect', 'bisect', '_sha512', '_random', 'random', 'ipython_genutils.path', 'traitlets.config.loader', 'textwrap', 'ipython_genutils.text', 'traitlets.config.configurable', 'ipython_genutils.importstring', 'traitlets.config.application', 'traitlets.config', 'pprint', 'platform', 'sysconfig', 'urllib', 'urllib.parse', '_sysconfigdata__linux_x86_64-linux-gnu', 'pydoc', 'bdb', 'IPython.utils', 'IPython.utils.ipstruct', 'IPython.utils.coloransi', 'pygments', 'IPython.utils.colorable', 'IPython.utils.PyColorize', 'IPython.utils.encoding', 'IPython.utils.py3compat', 'IPython.core.excolors', 'IPython.testing', 'IPython.testing.skipdoctest', 'cmd', 'codeop', 'code', 'pdb', 'IPython.core.debugger', 'IPython.core.display_trap', 'pexpect.exceptions', 'pexpect.utils', 'pexpect.expect', 'termios', 'tty', 'pty', 'fcntl', 'resource', 'ptyprocess.util', 'ptyprocess.ptyprocess', 'ptyprocess', 'pexpect.spawnbase', 'pexpect.pty_spawn', 'pexpect.run', 'pexpect', 'shlex', 'IPython.utils._process_common', 'IPython.utils._process_posix', 'IPython.utils.process', 'IPython.utils.decorators', 'IPython.utils.path', 'IPython.utils.data', 'IPython.utils.terminal', 'IPython.core.ultratb', 'IPython.utils._sysinfo', 'IPython.utils.sysinfo', 'IPython.core.crashhandler', 'tempfile', 'IPython.utils.importstring', 'IPython.paths', 'IPython.core.profiledir', 'IPython.core.application', 'IPython.terminal', '_hashlib', '_blake2', '_sha3', 'hashlib', 'IPython.core.compilerop', 'IPython.core.error', 'ntpath', 'pathlib', 'IPython.utils.text', 'IPython.core.magic_arguments', 'getopt', 'typing.io', 'typing.re', 'typing', 'binascii', 'mimetypes', 'IPython.core.display', 'IPython.core.page', 'getpass', 'IPython.lib.security', 'IPython.lib', '_datetime', 'datetime', 'IPython.lib.pretty', 'IPython.utils.openpy', 'IPython.utils.dir2', 'IPython.utils.wildcard', 'pygments.lexers._mapping', 'pygments.modeline', 'pygments.plugin', 'pygments.util', 'pygments.lexers', 'pygments.filter', 'pygments.token', 'pygments.filters', 'pygments.regexopt', 'pygments.lexer', 'pygments.unistring', 'pygments.lexers.p
"DEBUG:matplotlib:CACHEDIR=/home/blacklight/.cache/matplotlib\n",
"DEBUG:matplotlib.font_manager:Using fontManager instance from /home/blacklight/.cache/matplotlib/fontlist-v330.json\n",
"DEBUG:matplotlib.pyplot:Loaded backend module://ipykernel.pylab.backend_inline version unknown.\n",
"DEBUG:matplotlib.pyplot:Loaded backend module://ipykernel.pylab.backend_inline version unknown.\n",
"DEBUG:tensorflow:Falling back to TensorFlow client; we recommended you install the Cloud TPU client directly with pip install cloud-tpu-client.\n",
"WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.\n",
"WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.\n",
"WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.\n",
"WARNING:root:Limited tf.summary API due to missing TensorBoard installation.\n"
]
}
],
"source": [
"import os\n",
"\n",
"from micmon.audio import AudioDevice, AudioPlayer\n",
"from micmon.model import Model\n",
"\n",
2020-10-28 18:12:19 +01:00
"model_dir = os.path.expanduser(os.path.join('~', 'models', 'baby-monitor'))\n",
2020-10-27 15:21:32 +01:00
"audio_system = 'alsa'\n",
"audio_device = 'plughw:3,0'\n",
"label_names = ['negative', 'positive']"
]
},
{
"cell_type": "markdown",
"source": [
"Load the model"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [],
"source": [
2020-10-28 18:12:19 +01:00
"model = Model.load(model_dir)"
2020-10-27 15:21:32 +01:00
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"Play some audio from the input device"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"with AudioDevice(audio_system, device=audio_device) as source, AudioPlayer() as player:\n",
" for sample in source:\n",
" player.play(sample)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"Record frames from an audio source and make predictions using the model"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%% md\n"
}
}
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2020-10-28 18:12:19 +01:00
"negative\n",
"negative\n",
"negative\n",
"negative\n",
"negative\n",
"negative\n",
2020-10-27 15:21:32 +01:00
"negative\n",
"negative\n",
"negative\n",
"negative\n"
]
}
],
"source": [
"with AudioDevice(audio_system, device=audio_device) as source:\n",
" for sample in source:\n",
" source.pause()\n",
" prediction = model.predict(sample)\n",
" print(prediction)\n",
2020-10-28 18:12:19 +01:00
" source.resume()\n"
2020-10-27 15:21:32 +01:00
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}