Make Metadata.items more idiomatic in Python bindings

This commit is contained in:
Reuben Morais 2019-04-24 21:35:10 -03:00
parent 9815d54218
commit f397006436
4 changed files with 98 additions and 89 deletions

View File

@ -39,6 +39,7 @@ N_FEATURES = 26
# Size of the context window used for producing timesteps in the input vector
N_CONTEXT = 9
def convert_samplerate(audio_path):
sox_cmd = 'sox {} --type raw --bits 16 --channels 1 --rate 16000 --encoding signed-integer --endian little --compression 0.0 --no-dither - '.format(quote(audio_path))
try:
@ -50,11 +51,9 @@ def convert_samplerate(audio_path):
return 16000, np.frombuffer(output, np.int16)
def metadata_to_string(metadata):
retval = ''
for item in range(metadata.num_items):
retval += metadata.items[item].character
return retval
return ''.join(item.character for item in metadata.items)
class VersionAction(argparse.Action):
@ -65,6 +64,7 @@ class VersionAction(argparse.Action):
printVersions()
exit(0)
def main():
parser = argparse.ArgumentParser(description='Running DeepSpeech inference.')
parser.add_argument('--model', required=True,

View File

@ -33,17 +33,19 @@ import_array();
%append_output(SWIG_NewPointerObj(%as_voidptr(*$1), $*1_descriptor, 0));
}
%extend struct MetadataItem {
MetadataItem* __getitem__(size_t i) {
return &$self[i];
}
}
%typemap(out) Metadata* {
// owned, extended destructor needs to be called by SWIG
%append_output(SWIG_NewPointerObj(%as_voidptr($1), $1_descriptor, SWIG_POINTER_OWN));
}
%typemap(out) MetadataItem* %{
$result = PyList_New(arg1->num_items);
for (int i = 0; i < arg1->num_items; ++i) {
PyObject* o = SWIG_NewPointerObj(SWIG_as_voidptr(&arg1->items[i]), SWIGTYPE_p_MetadataItem, 0);
PyList_SetItem($result, i, o);
}
%}
%extend struct Metadata {
~Metadata() {
DS_FreeMetadata($self);

View File

@ -1,6 +1,5 @@
[build_ext]
include-dirs=./
swig-opts=-c++ -keyword
build-lib=temp_build
build-temp=temp_build

View File

@ -7,6 +7,7 @@ import os
import subprocess
import sys
def main():
try:
import numpy
try:
@ -47,6 +48,8 @@ def lib_dirs_split(a):
if os.name == 'nt':
return []
raise AssertionError('os.name == java not expected')
def libs_split(a):
if os.name == 'posix':
return a.split('-l')[1:]
@ -54,12 +57,14 @@ def libs_split(a):
if os.name == 'nt':
return a.split('.lib')[0:1]
ds_ext = Extension('deepspeech._impl',
['impl.i'],
raise AssertionError('os.name == java not expected')
ds_ext = Extension(name='deepspeech._impl',
sources=['impl.i'],
include_dirs=[numpy_include, '../'],
library_dirs=list(map(lambda x: x.strip(), lib_dirs_split(os.getenv('MODEL_LDFLAGS', '')))),
libraries = list(map(lambda x: x.strip(), libs_split(os.getenv('MODEL_LIBS', ''))))
)
libraries=list(map(lambda x: x.strip(), libs_split(os.getenv('MODEL_LIBS', '')))),
swig_opts=['-c++', '-keyword', '-builtin'])
setup(name=project_name,
description='A library for running inference on a DeepSpeech model',
@ -97,3 +102,6 @@ setup(name = project_name,
'Topic :: Scientific/Engineering',
'Topic :: Utilities',
])
if __name__ == '__main__':
main()