이번에 과제를 진행하면서 2명이 주고받는 대화를 녹음한 mp3 파일에서 2명의 화자를 구분해 내는 것을 해야 했다.
이를 speaker dirazation 이라고 하는데 이와 관련한 코드인 pyannote-audio 를 써 보았다.
OS : Ubuntu 20.04 안의 Docker Ubuntu20.04 (Ubuntu image : 11.6.1-cudnn8-devel-ubuntu20.04)
1. 환경 구축
이 모델을 돌리는데 gpu 로 돌리는 것을 추천한다. gpu 설정이나 cuda, cuDNN 설치를 로컬환경에서 할수도 있지만 나는 docker 를 설치 후 관련한 이미지를 사용하였다.
https://jeahun10717.tistory.com/42
https://jeahun10717.tistory.com/43
[Docker] Ubuntu(20.04) Docker 설치
지금 쓰는 컴퓨터가 VRAM 이 너무 작아서 딥러닝 작업이 불가능했는데 회사에서 서버를 제공해 줘서 Docker 를 설치하게 되었다. 원래도 외주를 하거나 학교에서 공동작업을 할 때는 Docker 를 썼지
jeahun10717.tistory.com
conda create -n pyannote python=3.8
conda activate pyannote
# pytorch 1.11 is required for speechbrain compatibility
# (see https://pytorch.org/get-started/previous-versions/#v1110)
conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 -c pytorch
pip install pyannote.audio
2. dirazation code
from pyannote.audio import Pipeline
pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization",
use_auth_token="hf_????")
# TODO : 작업을 실행할 mp3 파일 변수로 바꾸기 --> 유지보수용
targetDirPath = "./data/"
targetFileName = "1_10004.mp3"
targetFilePath = targetDirPath + targetFileName
from pydub import AudioSegment
# mp3 파일을 wav 파일로 변환
fileName = targetFileName.split('.') # fileName 은 targetFileName 의 확장자를 제외한 파일의 이름 저장
AudioSegment.from_mp3(targetFilePath).export(targetDirPath + fileName[0] + ".wav", format="wav")
diarization = pipeline(targetDirPath + fileName[0] + ".wav")
from pydub import AudioSegment
segLen = len(diarization)
rawAudio = AudioSegment.from_wav("./data/" + fileName[0] + ".wav") # 원본 mp3 파일을 wav 로 변환한 파일
f = open("./data/split/label_"+fileName[0]+".txt", 'w', encoding = 'utf-8')
for turn, _, speaker in diarization.itertracks(yield_label=True):
text = f"{turn.start * 1000} {turn.end * 1000} speaker_{speaker}" + '\n'
f.write(text) # 결과값을 text 파일로 저장
f.close()
위의 코드는 아래 파이프라인을 구현한 것이다.
1. mp3 파일을 wav 파일로 변환
2. diarization코드 적용
3. diarization 으로 분리된 화자를 txt 파일에 저장
위의 코드에서 저장되는 값은 아래형태로 저장되어 있다.
+ diarization 은 아래와 같은 결과를 보여준다.
diarization 객체에 관한 것은 아직 분석이 끝나지 않아서 추후에 이 포스팅에 추가하거나 다른 포스팅에 마저 적던가 할 것 같다.
'Deep Learning > NLP, ASR' 카테고리의 다른 글
[NLP / ASR] 오디오 데이터 전처리 관련 공부용 BLOG / 자료 link (0) | 2023.01.25 |
---|---|
[NLP / ASR] pyannote.audio diarization 의문점 정리 (0) | 2023.01.19 |
[NLP / ASR] openai whisper transcribe 설치 및 사용법 (0) | 2023.01.06 |