This repository has been archived by the owner on Dec 13, 2021. It is now read-only.
forked from gooofy/py-kaldi-asr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasr_client_multiProcess.py
126 lines (93 loc) · 3.71 KB
/
asr_client_multiProcess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# very basic example client for our example speech asr server
#
import os
import sys
import logging
import traceback
import json
import wave
import struct
import requests
from time import time
from optparse import OptionParser
from multiprocessing import Process,Pool
DEFAULT_HOST = 'localhost'
DEFAULT_PORT = 8001
NUM_WOKER = 3
#
# commandline
#
parser = OptionParser("usage: %prog [options] ")
parser.add_option ("-v", "--verbose", action="store_true", dest="verbose",
help="verbose output")
parser.add_option ("-H", "--host", dest="host", type = "string", default=DEFAULT_HOST,
help="host, default: %s" % DEFAULT_HOST)
parser.add_option ("-p", "--port", dest="port", type = "int", default=DEFAULT_PORT,
help="port, default: %d" % DEFAULT_PORT)
(options, args) = parser.parse_args()
if options.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
logging.getLogger("requests").setLevel(logging.WARNING)
#
# read samples from wave file, hand them over to asr server incrementally to simulate online decoding
#
def decoder_wavefile(url, wavfn):
time_start = time()
wavf = wave.open(wavfn, 'rb')
# check format
assert wavf.getnchannels()==1
assert wavf.getsampwidth()==2
# process file in 250ms chunks
chunk_frames =int( 250 * wavf.getframerate() / 1000)
tot_frames = wavf.getnframes()
num_frames = 0
while num_frames < tot_frames:
finalize = False
if (num_frames + chunk_frames) < tot_frames:
nframes = chunk_frames
else:
nframes = tot_frames - num_frames
finalize = True
frames = wavf.readframes(nframes)
num_frames += nframes
print(nframes)
samples = struct.unpack_from('<%dh' % nframes, frames) #h->short,16b
data = {'audio' : samples,
'do_record' : False,
'do_asr' : True,
'do_finalize': finalize}
response = requests.post(url, data=json.dumps(data))
logging.info("%6.3fs: %5d frames (%6.3fs) decoded, status=%d." % (time()-time_start,
num_frames,
float(num_frames) / float(wavf.getframerate()),
response.status_code))
assert response.status_code == 200
wavf.close()
data = response.json()
logging.debug("raw response data: %s" % repr(data))
logging.info ( "*****************************************************************")
logging.info ( "** waveName : %s" % wavfn)
logging.info ( "** asrResult : %s" % data['hstr'])
logging.info ( "** confidence : %f" % data['confidence'])
logging.info ( "** duration : %f" % (wavf.getnframes()/wavf.getframerate()))
logging.info ( "** decoding time : %8.2fs" % ( time() - time_start ))
logging.info ( "*****************************************************************")
if __name__ == "__main__" :
url =""
i=0
time_start=time()
p=Pool(NUM_WOKER)
filelist=["cn-8k-13s.wav", "cn-8k-13s.wav", "cn-8k-13s.wav"]
for file in filelist:
port=int(DEFAULT_PORT)+i
url = 'http://%s:%d/decode' % (DEFAULT_HOST, port)
p.apply_async(decoder_wavefile, args=(url,"data/"+file,))
i=i+1
#decoder_wavefile(url,"data/"+file)
p.close()
p.join()
logging.info ( "** total time : %8.2fs" % ( time() - time_start ))