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
| from aip import AipSpeech
import wave
from pyaudio import PyAudio, paInt16
def save_wave_file(filename, data):
'''save the date to the wavfile'''
wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(sampwidth)
wf.setframerate(framerate)
wf.writeframes(b"".join(data))
wf.close()
def my_record():
pa = PyAudio()
stream = pa.open(format=paInt16, channels=1,
rate=framerate, input=True,
frames_per_buffer=NUM_SAMPLES)
my_buf = []
count = 0
print('录音开始')
while count < TIME*10: # 控制录音时间
string_audio_data = stream.read(NUM_SAMPLES)
my_buf.append(string_audio_data)
count += 1
print('录音结束')
save_wave_file('01.pcm', my_buf)
stream.close()
if __name__ == "__main__":
framerate = 16000
NUM_SAMPLES = 2000
channels = 1
sampwidth = 2
TIME = 2
my_record()
""" 我的 APPID AK SK """
APP_ID = '16795182 '
API_KEY = '8io5s7uv8pjYPUrQp1i8PcIg'
SECRET_KEY = 'gK2MljvbcF3SKS4p0C2ieq1SCBNY48BD'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
# 读取文件
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 识别本地文件
result = client.asr(get_file_content('01.pcm'), 'pcm', 16000, {
'dev_pid': 1536,
})
# print(type(result))
print("语音识别结果:", result['result'])
|