본문 바로가기

플랫폼/Google Cloud

[ Google Cloud:STT ] 스트리밍 끝나는 지점 체크


Google Cloud

Where is ended a point at Streaming STT

#Python3.6.5

#command #사용자변수



Streaming service를 이용할 때 streaming되고 있는 data가 잡음인지 실제 사람의 음성(필요한 data)인지 구분하기 위해 Google STT에서 제공하고 있는 옵션을 사용해 보고자 한다.

일단 Google Streaming STT 홈페이지에 있는 설명을 한 번 보고 실제 동작은 어떻게 이루어지는지 확인까지 해보자.


영문 아래 한글은 번역을 한게 아니며, 대충의 내용을 요약 정리한 것이기 때문에 상세 내용은 꼭 영문을 읽어보시길 바랍니다 :)



Let page explan

Note: To use streaming recognition to stop listening after the user speaks a single word, like in the case of voice commands, set the single_utterance field to true in theStreamingRecognitionConfig object. The single_utterance flag tells the Speech API to end the transcription request once it detects that the speech has ended like at the end of a single word.

음성 명령의 경우 사용자가 한 단어를 말한 후 듣는 것을 멈추려면 single_utterance 옵션을 true로 설정하면 된다.

single_utterance는 한 단어가 끝난 지점을 감지하고 Speech API가 텍스트 변환 요청을 끝내도록 지시한다.


StreamingRecognitionConfig의 항목인 single_utterance 옵션의 내용을 좀 더 살펴보자.

 single_utterance

 bool


 Optional If false or omitted, the recognizer will perform continuous recognition (continuing to wait for and process audio even if the user pauses speaking) until the client   closes the input stream (gRPC API) or until the maximum time limit has been reached. May return multiple StreamingRecognitionResults with the is_final flag set to true.


 If true, the recognizer will detect a single spoken utterance. When it detects that the user has paused or stopped speaking, it will return an END_OF_SINGLE_UTTERANCE   event and cease recognition. It will return no more than one StreamingRecognitionResult with the is_final flag set to true.

single_utterance 옵션을 false로 설정하는 경우 사용자가 잠시 말을 중단하더라도 입력 스트림이 최대 시간 제한에 도달 할 때 까지 입력을 유지합니다.


single_utterance 옵션을 true로 설정하는 경우 사용자가 말을 잠시 중단하거나 말을 중지 했다는 것을 감지하게 되면 다음 입력을 기다리지 않고 END_OF_SINGLE_UTTERANCE 이벤트를 발생하고 인식을 중단합니다.


 END_OF_SINGLE_UTTERANCE

 This event indicates that the server has detected the end of the user's speech utterance and expects no additional speech. Therefore, the server will not process additional   audio (although it may subsequently return additional results). The client should stop sending additional audio data, half-close the gRPC connection, and wait for any   additional results until the server closes the gRPC connection. This event is only sent if single_utterance was set to true, and is not used otherwise.

END_OF_SINGLE_UTTERANCE는 서버가 사용자의 음성 발언의 끝을 감지했으며 추가 발언이 없음을 나타냅니다. 끝을 감지 했기 때문에 추가 오디오를 처리 하지 않습니다. 이 이벤트는 single_utterance가 true로 설정된 경우에만 발생합니다.


Let's Do it

일단 StreamingRecognitionConfig의 옵션에 single_utterance를 추가하고 값을 True로 설정하여 준다.

single_utterance를 True로 설정하고 나면 내가 말을 시작한 뒤 말을 멈추거나 말을 끝내면 더 이상 STT 동작을 하지 않도록 설정이 된다.


1
2
3
4
    streaming_config = types.StreamingRecognitionConfig(
                config=config,
                single_utterance=True,
                interim_results=True)


그러면 이제 동작을 멈췄을 때 응답을 돌려받거나 다음 동작으로 넘어갈 수 있도록 result text를 반환해보자.

예제 코드에서 함수명이 [ listen_print_loop ] 인 것이 있다 이 함수 내부에 코드를 추가할 것이다.


함수 내부에는 응답을 돌려받아 하나씩 비교를 하는 for문이 있는데 이 하단의 "추가할 부분" 위치에 해당 코드를 추가 해주면

END_OF_SINGLE_UTTERANCE 이벤트 발생시 STT result text를 반환한다.


1
2
3
4
5
6
7
8
9
10
for response in responses:
 
# 추가할 부분 - - - - - - - - - - - - - - - - - - - - - - - - - - -
            if response.speech_event_type:
                text = transcript + overwrite_chars
                print('here is final text: {}'.format(text))
 
                return text
# 추가할 부분 - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
            if not response.results:
                continue


response.speech_event_type를 응답결과 마다 출력해보면, 이벤트 발생시에는 값이 1이고 이벤트 발생이 없는 경우 값이 0으로 출력된다.

bool의 의미를 가지기 때문에 0과 1로 표시되는 것 같다.


text = transcript + overwrite_chars 를 하여야 string형태로 결과가 출력된다.

각 값이 무슨 의미를 가지는지 알고 싶다면 for문 안에서 response를 출력해 보길 바란다.


위의 내용처럼 코드를 수정하고 동작시키면, 사용자가 처음 말하고 나서 멈추기전까지 내용을 해석한 결과를 반환하고 그 후에는 사용자가 아무리 마이크에 대고 말해도 동작을 수행하지 않는 것을 알 수 있다. 당연이 STT동작을 재수행하면 다시 동작이 된다.





#2019년01월28일

참조하면 좋은

- Package google.cloud.speech.v1

- Streaming requests