websocket

This commit is contained in:
游雁 2024-01-31 10:09:36 +08:00
parent 7085ee3ad9
commit e2154e4599
2 changed files with 31 additions and 15 deletions

View File

@ -5,7 +5,7 @@
2022-2023 by zhaomingwork@qq.com
'''
# pip install websocket-client
import ssl
import ssl
from websocket import ABNF
from websocket import create_connection
from queue import Queue
@ -20,7 +20,13 @@ class Funasr_websocket_recognizer():
python asr recognizer lib
'''
def __init__(self, host="127.0.0.1", port="30035", is_ssl=True,chunk_size="5, 10, 5",chunk_interval=10,mode="offline",wav_name="default"):
def __init__(self, host="127.0.0.1",
port="30035",
is_ssl=True,
chunk_size="0, 10, 5",
chunk_interval=10,
mode="offline",
wav_name="default"):
'''
host: server host ip
port: server port
@ -43,17 +49,21 @@ class Funasr_websocket_recognizer():
self.msg_queue = Queue() # used for recognized result text
print("connect to url",uri)
self.websocket=create_connection(uri,ssl=ssl_context,sslopt=ssl_opt)
self.websocket=create_connection(uri, ssl=ssl_context, sslopt=ssl_opt)
self.thread_msg = threading.Thread(target=Funasr_websocket_recognizer.thread_rec_msg,args=(self,))
self.thread_msg = threading.Thread(target=Funasr_websocket_recognizer.thread_rec_msg, args=(self,))
self.thread_msg.start()
chunk_size = [int(x) for x in chunk_size.split(",")]
stride = int(60 * chunk_size[1]/ chunk_interval / 1000 * 16000 * 2)
stride = int(60 * chunk_size[1] / chunk_interval / 1000 * 16000 * 2)
chunk_num = (len(audio_bytes) - 1) // stride + 1
message = json.dumps({"mode": args.mode, "chunk_size": args.chunk_size, "encoder_chunk_look_back": 4,
"decoder_chunk_look_back": 1, "chunk_interval": args.chunk_interval,
"wav_name": wav_name, "is_speaking": True})
message = json.dumps({"mode": mode,
"chunk_size": chunk_size,
"encoder_chunk_look_back": 4,
"decoder_chunk_look_back": 1,
"chunk_interval": chunk_interval,
"wav_name": wav_name,
"is_speaking": True})
self.websocket.send(message)
@ -68,7 +78,7 @@ class Funasr_websocket_recognizer():
try:
while(True):
msg=self.websocket.recv()
if msg is None or len(msg)==0:
if msg is None or len(msg) == 0:
continue
msg = json.loads(msg)
@ -77,7 +87,7 @@ class Funasr_websocket_recognizer():
print("client closed")
# feed data to asr engine, wait_time means waiting for result until time out
def feed_chunk(self, chunk,wait_time=0.01):
def feed_chunk(self, chunk, wait_time=0.01):
try:
self.websocket.send(chunk, ABNF.OPCODE_BINARY)
# loop to check if there is a message, timeout in 0.01s
@ -89,6 +99,7 @@ class Funasr_websocket_recognizer():
return msg
except:
return ""
def close(self,timeout=1):
message = json.dumps({"is_speaking": False})
self.websocket.send(message)
@ -103,9 +114,10 @@ class Funasr_websocket_recognizer():
return msg
if __name__ == '__main__':
print('example for Funasr_websocket_recognizer')
import wave
wav_path="asr_example.wav"
wav_path = "/Users/zhifu/Downloads/modelscope_models/speech_seaco_paraformer_large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/example/asr_example.wav"
with wave.open(wav_path, "rb") as wav_file:
params = wav_file.getparams()
frames = wav_file.readframes(wav_file.getnframes())
@ -115,20 +127,24 @@ if __name__ == '__main__':
stride = int(60 * 10 / 10 / 1000 * 16000 * 2)
chunk_num = (len(audio_bytes) - 1) // stride + 1
# create an recognizer
rcg=Funasr_websocket_recognizer(host="127.0.0.1",port="30035",is_ssl=True,mode="2pass")
rcg = Funasr_websocket_recognizer(host="127.0.0.1",
port="10095",
is_ssl=True,
mode="2pass",
chunk_size="0,10,5")
# loop to send chunk
for i in range(chunk_num):
beg = i * stride
data = audio_bytes[beg:beg + stride]
text=rcg.feed_chunk(data,wait_time=0.02)
text = rcg.feed_chunk(data,wait_time=0.02)
if len(text)>0:
print("text",text)
time.sleep(0.05)
# get last message
text=rcg.close(timeout=3)
text = rcg.close(timeout=3)
print("text",text)

View File

@ -152,7 +152,7 @@ async def ws_serve(websocket, path):
frames_asr = []
frames_asr_online = []
global websocket_users
await clear_websocket()
# await clear_websocket()
websocket_users.add(websocket)
websocket.status_dict_asr = {}
websocket.status_dict_asr_online = {"cache": {}, "is_final": False}