游雁
2024-12-17 fdafd3f6bc2f04d16e7cab5afcdb1257e87a8a78
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
"""
  Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
  Reserved. MIT License  (https://opensource.org/licenses/MIT)
  
  2023-2024 by zhaomingwork@qq.com  
"""
 
# pip install websocket-client
# apt install ffmpeg
 
import threading
import traceback
import json
import time
 
 
# class for recognizer in websocket
class FunasrStream:
    """
    python asr recognizer lib
 
    """
 
    def __init__(
        self,
        funasr_core
        
    ):
        """
        uri: ws or wss server uri
        msg_callback: for message received
        timeout: timeout for get result
        """
        try:
            self.funasr_core=funasr_core
 
        except Exception as e:
            print("FunasrStream init Exception:", e)
            traceback.print_exc()
    
 
    # feed data to asr engine in stream way
    def feed_chunk(self, chunk):
        try:
            if self.funasr_core is None:
                print("error in stream, funasr_core is None")
                exit(0)
            self.funasr_core.feed_chunk(chunk)
            return  
        except:
            print("feed chunk error")
            return 
         
         
 
    # return all result for this stream
    def wait_for_end(self):
       try:
 
        message = json.dumps({"is_speaking": False})
        self.funasr_core.websocket.send(message)
        self.funasr_core.wait_for_result()
        self.funasr_core.close()
 
        # return the  msg
        return self.funasr_core.rec_text
       except Exception  as e:
            print("error get_final_result ",e)
            return ""