갈루아의 반서재

은닉 마르코프 모형 (Hidden Markov Model, HMM), 심층학습모델을 통해 오디오 파일을 텍스트로 변환할 수 있다. 여기서는 “Speech Recognition” API 와 “PyAudio” 라이브러리를 통해 간단하게 구현하는 방법을 알아보자.Speech Recognition API는 다수의 API를 지원하는데, 여기서는 Google speech recognition API 를 사용하도록 한다. 더욱 자세한 내용은 여기를 참고한다.

먼저 필요한 라이브러리를 설치한다. 이하 아나콘다 가상환경에서 진행한다. 아나콘다에서의 Speech Recognition 라이브러리 설치는 https://anaconda.org/conda-forge/speechrecognition 을 참고한다. 

 

(tfquantum) founder@hilbert:~$ conda install -c conda-forge speechrecognition
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/founder/anaconda3/envs/tfquantum

  added / updated specs:
    - speechrecognition


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    speechrecognition-3.6.3    |        py37_1000        30.7 MB  conda-forge
    ------------------------------------------------------------
                                           Total:        30.7 MB

The following NEW packages will be INSTALLED:

  speechrecognition  conda-forge/linux-64::speechrecognition-3.6.3-py37_1000


Proceed ([y]/n)? y


Downloading and Extracting Packages
speechrecognition-3. | 30.7 MB   | ##################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(tfquantum) founder@hilbert:~$ conda list
# packages in environment at /home/founder/anaconda3/envs/tfquantum:
#
# Name                    Version                   Build  Channel
speechrecognition         3.6.3                 py37_1000    conda-forge

주피터랩에서 제대로 설치되었는지 확인해보자. 3.6.3 버전이 정상 설치되었음을 알 수 있다.

import speech_recognition as sr
sr.__version__

'3.6.3'

오디오파일의 텍스트로의 변환은 다음과 같이 이루어진다.

  1. Speech recognition 라이브러리 가져오기
  2. 음성 스피치 인식을 위해 recognizer class 초기화
  3. 지원되는 오디오 파일 포맷은 다음과 같음 : wav, AIFF, AIFF-C, FLAC
  4. 여기서 사용된 오디오 클립은 다음과 같음 :  “Let not steadfast love and faithfulness forsake you…write them on the tablet of your heart.”
  5. 본 포스팅은 영어 음성 인식에 대한 것이며, 다른 언어에 대한 내용은 이 문서를 참조한다.

그럼 코드를 보자. 먼저 앞서 설치한 라이브러리를 가져온다.

import speech_recognition as sr

클래스 초기화

r = sr.Recognizer()

현재 디렉토리 위치 확인 후 음성 파일을 해당 위치에 업로드한다.

pwd

'/home/founder/speech_recognition'

여기서 사용된 파일은 오바마 대통령의 2016 9/11 Memorial Address 로 다음 사이트에서 다운받을 수 있다. 특히 이 사이트의 경우 pdf 원고가 있어서 변환 결과 대조가 용이한 장점이 있다. 다만 포맷이 mp3 라 wav 파일로의 변환이 필요하다는 점이 단점이면 단점이다. 

https://www.americanrhetoric.com/barackobamaspeeches.htm

도입부의 9초짜리 클립을 만들어 barackobama911memorial15yearsARXE.wav 이름으로 '/home/founder/speech_recognition'에 저장 후, 해당 파일을 읽어들여 audio_text 변수로 저장한다.  

with sr.AudioFile('barackobama911memorial15yearsARXE.wav') as source:
     audio_text = r.listen(source)

recognize_google() 메서드를 통해 변환결과를 출력한다. 예외처리를 통해 API 도달실패시 request error 를 출력해주도록 한다. 

try:
    text = r.recognize_google(audio_text)
    print('Converting audio transcripts into text ...')
    print(text)
except:
    print('Sorry.. run again...')
Converting audio transcripts into text ...
but not steadfast love and faithfulness forsake you write them on the tablet of your heart

변환결과를 원문과 비교해보자. 완벽하지는 않지만 유사하게 나온다.

  • 원문 Let not steadfast love and faithfulness forsake you…write them on the tablet of your heart
  • 변환 but not steadfast love and faithfulness forsake you write them on the tablet of your heart

 

참고사이트