본문 바로가기
Linguistic Intelligence/Speech Recognition

[Speech Recognition] wav2vec2 모델을 이용하여 음성 인식 프로젝트

by goatlab 2024. 8. 6.
728x90
반응형
SMALL

허깅페이스 로그인 

 

from huggingface_hub import login

login(token="hf_")

 

라이브러리 및 모델 로드

 

import torch
import librosa
import numpy as np
from scipy.io import wavfile
from IPython.display import Audio
from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer

tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h")
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")

# 오디오 파일 로드
file_name = 'test.wav'
Audio(file_name)

 

토큰화 (tokenize)

 

data = wavfile.read(file_name)
sampling_rate = data[0]
sound_data = data[1]

 

Facebook 모델은 16000Hz 샘플링 속도를 허용하므로 샘플링 속도를 16000Hz로 변환한다.

 

input_audio, _ = librosa.load(file_name, sr=16000)

 

디코딩

 

# retrieve logits
input_values = tokenizer(input_audio, return_tensors="pt").input_values
logits = model(input_values).logits

# take argmax and decode
predicted_ids = torch.argmax(logits, dim=-1)
transcription = tokenizer.batch_decode(predicted_ids)[0]
print(transcription)
ALICE WAS BEGINNING TO GET VERY TIRED OF SITTING BY HER SISTER ON THE BANK AND OF HAVING NOTHING TO DO ONCE OR TWICE SHE PEEPED INTO THE BOOK HER SISTER WAS READING BUT IT HAD NO PICTURES OR CONVERSATIONS IN IT AND WHAT'S THE USE OF A BOOK THOUGHT ALICE WITHOUT PICTURES OR CONVERSATION SO SHE WAS CONSIDERING IN HER OWN MIND AS WELL AS SHE COULD FOR THE HOT DAY MADE HER FEEL VERY SLEEPY AND STUPID WHETHER THE PLEASURE OF MAKING A DAISY CHAIN WOULD BE WORTH THE TROUBLE OF GETTING UP AND PICKING THE DAISIES WHEN SUDDENLY A WHITE RABBIT WITH PINK EYES RAN CLOSE BY HER THERE WAS NOTHING SO VERY REMARKABLE IN THAT NOR DID ALICE THINK IT SO VERY MUCH OUT OF THE WAY TO HEAR THE RABBIT SAY TO ITSELF OH DEAR OH DEAR I SHALL BE LATE WHEN SHE THOUGHT IT OVER AFTERWARDS IT OCCURRED TO HER THAT SHE OUGHT TO HAVE WONDERED AT THIS BUT AT THE TIME IT ALL SEEMED QUITE NATURAL

 

 

https://www.linkedin.com/pulse/ok-google-speech-text-python-deep-learning-2-minutes-kumar-

 

“Ok, Google!”— Speech to Text in Python with Deep Learning in 2 minutes

Introduction You may be frequently using Google Assistant or Apple’s Siri or even Amazon Alexa to find out quick answers on the web or to simply command something. These AI assistants are well known for understanding our speech commands and performing th

www.linkedin.com

 

728x90
반응형
LIST