import time import wave import string import json import sys import os import azure.cognitiveservices.speech as speechsdk try: import azure.cognitiveservices.speech as speechsdk except ImportError: print(""" Importing the Speech SDK for Python failed. Refer to https://docs.microsoft.com/azure/cognitive-services/speech-service/quickstart-python for installation instructions. """) import sys sys.exit(1) # Set up the subscription info for the Speech Service: # Replace with your own subscription key and service region (e.g., "westus"). speech_key, service_region = "fbf79ce16bb64cbc8e9ef93f70537bfe", "chinanorth3" # Specify the path to an audio file containing speech (mono WAV / PCM with a sampling rate of 16 # kHz). # weatherfilenamemp3 = "whatstheweatherlike.mp3" def speech_recognize_continuous_from_file(): """performs continuous speech recognition with input from an audio file""" # speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region) #audio_config = speechsdk.audio.AudioConfig(filename=weatherfilename) #audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True) audio_config = speechsdk.audio.AudioConfig(filename="/data/weiwang26/weiruanApi/audios/iat000e000a@lg18c22c7f6613228802.wav",use_default_microphone=False) speech_config.speech_recognition_language = "zh-CN" # speech_config.set_property(speechsdk.PropertyId.SpeechServiceConnection_EnableInverseTextNormalization, "false") # speech_config.set_property(speechsdk., "false") speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config) done = False def stop_cb(evt): """callback that signals to stop continuous recognition upon receiving an event `evt`""" print('CLOSING on {}'.format(evt)) nonlocal done done = True # Connect callbacks to the events fired by the speech recognizer # speech_recognizer.recognizing.connect(lambda evt: print('RECOGNIZING: {}'.format(evt))) if '/' in weatherfilename: output_txt = './%s/%s.txt' % (output_dir, weatherfilename.replace('.wav', '').split('/')[-1]) elif '\\' in weatherfilename: output_txt = './%s/%s.txt' % (output_dir, weatherfilename.replace('.wav', '').split('\\')[-1]) else: output_txt = './%s/%s.txt' % (output_dir, weatherfilename.replace('.wav', '')) filetest = open(output_txt, 'a', encoding='utf8') speech_recognizer.recognized.connect( lambda evt: (print('RECOGNIZED: {}'.format(evt)), filetest.writelines('RECOGNIZED: {}\n'.format(evt)))) speech_recognizer.session_started.connect(lambda evt: print('SESSION STARTED: {}'.format(evt))) speech_recognizer.session_stopped.connect(lambda evt: print('SESSION STOPPED {}'.format(evt))) speech_recognizer.canceled.connect(lambda evt: print('CANCELED {}'.format(evt))) # stop continuous recognition on either session stopped or canceled event speech_recognizer.session_stopped.connect(stop_cb) speech_recognizer.canceled.connect(stop_cb) # Start continuous speech recognition speech_recognizer.start_continuous_recognition() while not done: time.sleep(.5) speech_recognizer.stop_continuous_recognition() # filetest.close() with open("test.scp", 'r', encoding='utf8') as fr: lines = fr.readlines() output_dir = 'output_%s' % "test.scp" os.system('mkdir %s' % output_dir) for line in lines: line = line.strip('\n') weatherfilename = line speech_recognize_continuous_from_file()