Repository navigation
Expand file tree
/
Copy pathsentence_asr.py
More file actions
120 lines (102 loc) · 4.47 KB
/
Copy pathsentence_asr.py
File metadata and controls
120 lines (102 loc) · 4.47 KB
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
"""Example: One-shot sentence recognition.
Usage:
python examples/sentence_asr.py -f test.pcm
python examples/sentence_asr.py -f test.wav -fmt wav
python examples/sentence_asr.py -u https://example.com/test.wav -fmt wav
Prerequisites:
1. Get Tencent Cloud APPID: https://console.cloud.tencent.com/cam/capi
2. Create a TRTC application: https://console.cloud.tencent.com/trtc/app
3. Get SDKAppID and SDK secret key from the application overview page
4. Prepare an audio file (duration <= 60s, size <= 3MB)
"""
import argparse
import os
import logging
import sys
from trtc_asr import Credential
from trtc_asr.sentence_recognizer import (
SOURCE_TYPE_DATA,
SOURCE_TYPE_URL,
SentenceRecognitionRequest,
SentenceRecognizer,
)
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger(__name__)
# ===== Configuration =====
# Fill in your credentials before running.
APP_ID = int(os.environ.get("TRTC_ASR_APP_ID", "0"))
SDK_APP_ID = int(os.environ.get("TRTC_ASR_SDK_APP_ID", "0"))
SECRET_KEY = os.environ.get("TRTC_ASR_SECRET_KEY", "")
def main():
parser = argparse.ArgumentParser(description="TRTC Sentence ASR Example")
parser.add_argument("-f", "--file", default="", help="path to local audio file")
parser.add_argument("-u", "--url", default="", help="URL of audio file")
parser.add_argument(
"-e", "--engine", required=True, help="engine model type, required (e.g. bigmodel)"
)
parser.add_argument(
"--lang", default="", help="language hint; when omitted, the bigmodel engine uses zh"
)
parser.add_argument("-fmt", "--format", default="pcm", help="audio format (pcm, wav, mp3, ...)")
parser.add_argument("-w", "--word-info", type=int, default=0, help="word-level timing: 0=hide, 1=show, 2=with punctuation")
args = parser.parse_args()
# The bigmodel engine is best used with an explicit language; every other
# engine falls back to server-side detection unless --lang is given.
if not args.lang and args.engine == "bigmodel":
args.lang = "zh"
if APP_ID == 0 or SDK_APP_ID == 0 or not SECRET_KEY:
print(
"Error: Please set APP_ID, SDK_APP_ID and SECRET_KEY in the code.\n\n"
"Steps:\n"
" 1. Get APPID from CAM Console: https://console.cloud.tencent.com/cam/capi\n"
" 2. Open TRTC Console: https://console.cloud.tencent.com/trtc/app\n"
" 3. Create or select an application\n"
" 4. Copy SDKAppID and SDK secret key from the application overview\n"
" 5. Fill in the credentials at the top of this file.\n",
file=sys.stderr,
)
sys.exit(1)
if not args.file and not args.url:
print(
"Error: Please specify either -f (local file) or -u (audio URL).\n\n"
"Examples:\n"
" python examples/sentence_asr.py -f test.pcm\n"
" python examples/sentence_asr.py -f test.wav -fmt wav\n"
" python examples/sentence_asr.py -u https://example.com/test.wav -fmt wav\n",
file=sys.stderr,
)
sys.exit(1)
credential = Credential(APP_ID, SDK_APP_ID, SECRET_KEY)
recognizer = SentenceRecognizer(credential)
if args.url:
log.info("Recognizing from URL: %s", args.url)
req = SentenceRecognitionRequest(
eng_service_type=args.engine,
source_type=SOURCE_TYPE_URL,
voice_format=args.format,
url=args.url,
word_info=args.word_info,
language=args.lang,
)
result = recognizer.recognize(req)
else:
with open(args.file, "rb") as f:
data = f.read()
log.info("Recognizing from file: %s (%d bytes)", args.file, len(data))
req = SentenceRecognitionRequest(
eng_service_type=args.engine,
source_type=SOURCE_TYPE_DATA,
voice_format=args.format,
word_info=args.word_info,
language=args.lang,
)
result = recognizer.recognize_data_with_options(data, req)
print("Result: {}".format(result.result))
print("Audio Duration: {} ms".format(result.audio_duration))
print("Request ID: {}".format(result.request_id))
if result.word_list:
print("Word Count: {}".format(result.word_size))
for i, w in enumerate(result.word_list):
print(" [{}] {} ({}-{} ms)".format(i, w.word, w.start_time, w.end_time))
if __name__ == "__main__":
main()