File size: 2,049 Bytes
f6e8e0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c3a864
f6e8e0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/quickstart/python/from-microphone
"""
import argparse

import azure.cognitiveservices.speech as speechsdk

from project_settings import environment, project_path


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--speech_key",
        default=environment.get("speech_key"),
        type=str
    )
    parser.add_argument(
        "--service_region",
        default=environment.get("service_region"),
        type=str
    )
    parser.add_argument(
        "--filename",
        default=(project_path / "data/asr_examples/ja-JP/vm_2cb56b0c-e214-4a8d-ab01-a3a264d06c35_0.wav").as_posix(),
        type=str
    )
    parser.add_argument(
        "--language",
        default="ja-JP",
        type=str
    )
    args = parser.parse_args()
    return args


def main():
    args = get_args()

    speech_config = speechsdk.SpeechConfig(
        subscription=args.speech_key,
        region=args.service_region,
        speech_recognition_language=args.language
    )
    audio_config = speechsdk.AudioConfig(
        filename=args.filename,
    )

    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

    result = speech_recognizer.recognize_once_async().get()

    if result.reason == speechsdk.ResultReason.RecognizedSpeech:
        print("Recognized: {}".format(result.text))
    elif result.reason == speechsdk.ResultReason.NoMatch:
        print("No speech could be recognized: {}".format(result.no_match_details))
    elif result.reason == speechsdk.ResultReason.Canceled:
        cancellation_details = result.cancellation_details
        print("Speech Recognition canceled: {}".format(cancellation_details.reason))
        if cancellation_details.reason == speechsdk.CancellationReason.Error:
            print("Error details: {}".format(cancellation_details.error_details))
    return


if __name__ == '__main__':
    main()