2017-10-30 15:01:26 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-03-01 02:49:34 +00:00
|
|
|
# Copyright 2018 Mycroft AI Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2018-02-22 07:10:41 +00:00
|
|
|
import os
|
2017-10-30 15:01:26 +00:00
|
|
|
import sys
|
2018-02-09 00:43:03 +00:00
|
|
|
|
2018-02-21 05:42:04 +00:00
|
|
|
from prettyparse import create_parser
|
|
|
|
|
2017-11-03 19:39:52 +00:00
|
|
|
from precise import __version__
|
2018-02-22 07:10:41 +00:00
|
|
|
from precise.network_runner import Listener
|
2017-10-30 15:01:26 +00:00
|
|
|
|
2018-02-09 00:43:03 +00:00
|
|
|
usage = '''
|
2018-02-21 05:42:04 +00:00
|
|
|
stdin should be a stream of raw int16 audio, written in
|
|
|
|
groups of CHUNK_SIZE samples. If no CHUNK_SIZE is given
|
|
|
|
it will read until EOF. For every chunk, an inference
|
|
|
|
will be given via stdout as a float string, one per line
|
|
|
|
|
|
|
|
:model_name str
|
2018-02-23 21:23:20 +00:00
|
|
|
Keras or TensorFlow model to read from
|
2018-02-21 05:42:04 +00:00
|
|
|
|
|
|
|
...
|
2018-02-09 00:43:03 +00:00
|
|
|
'''
|
2017-10-30 15:01:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
|
|
|
stdout = sys.stdout
|
|
|
|
sys.stdout = sys.stderr
|
|
|
|
|
2018-02-09 00:43:03 +00:00
|
|
|
parser = create_parser(usage)
|
2017-11-03 19:39:52 +00:00
|
|
|
parser.add_argument('-v', '--version', action='version', version=__version__)
|
2018-02-09 00:43:03 +00:00
|
|
|
parser.add_argument('chunk_size', type=int, nargs='?', default=-1,
|
2018-03-07 19:36:42 +00:00
|
|
|
help='Number of bytes to read before making a prediction.'
|
2018-02-09 00:43:03 +00:00
|
|
|
'Higher values are less computationally expensive')
|
2017-11-03 19:39:52 +00:00
|
|
|
parser.usage = parser.format_usage().strip().replace('usage: ', '') + ' < audio.wav'
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if sys.stdin.isatty():
|
|
|
|
parser.error('Please pipe audio via stdin using < audio.wav')
|
2017-10-30 15:01:26 +00:00
|
|
|
|
2017-11-03 19:39:52 +00:00
|
|
|
listener = Listener(args.model_name, args.chunk_size)
|
2017-10-30 15:01:26 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
conf = listener.update(sys.stdin.buffer)
|
|
|
|
stdout.buffer.write((str(conf) + '\n').encode('ascii'))
|
|
|
|
stdout.buffer.flush()
|
|
|
|
except (EOFError, KeyboardInterrupt):
|
|
|
|
pass
|
|
|
|
|
2018-02-09 00:43:03 +00:00
|
|
|
|
2017-10-30 15:01:26 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|