Azure AI Tutorial 9 - Nhận Diện Giọng Nói Trên Azure
Ứng dụng AI vào thực tế càng ngày càng phổ biến, hiện tại chúng ta còn mong đợi có thể giao tiếp với hệ thống thông qua ngôn ngữ tự nhiên đặc biệt thông qua lời nói.
1. Tạo Cognitive Service Resource
Tương tự như các bài trước. Xem tại đây nhé.
2. Tạo notebook và Install thư viện
- Install thư viện (Nếu đã có thì bỏ qua bước này nhé)
! pip install azure-cognitiveservices-language-textanalytics
- Copy key, endpoint và cog_region
cog_key = '0afbab280c9f42e589143a683845a0c5'
cog_endpoint = 'https://mvcognitiveservice.cognitiveservices.azure.com/'
cog_location = 'westus'
3. Nhận diện giọng nói (speech to text)
Giờ chúng ta có thể thực hành nhận diện giọng nói và sau đó chuyển đổi từ âm thanh thành văn bản (speech-to-text). Đầu vào có thể là từ microphone hoặc 1 file âm thanh.
3.1 Test với nhận diện giọng nói từ phone
import os
import IPython
from azure.cognitiveservices.speech import SpeechConfig, SpeechRecognizer, AudioConfig
# Configure speech recognizer
speech_config = SpeechConfig(cog_key, cog_location)
# Have students say "turn the light on"
speech_recognizer = SpeechRecognizer(speech_config)
# Use a one-time, synchronous call to transcribe the speech
speech = speech_recognizer.recognize_once()
print(speech.text)
3.2 Nhận diện giọng nói với audio file
import os
from playsound import playsound
from azure.cognitiveservices.speech import SpeechConfig, SpeechRecognizer, AudioConfig
# Get spoken command from audio file
file_name = 'light-on.wav'
audio_file = os.path.join('data', 'speech', file_name)
# Configure speech recognizer
speech_config = SpeechConfig(cog_key, cog_location)
audio_config = AudioConfig(filename=audio_file) # Use file instead of default (microphone)
speech_recognizer = SpeechRecognizer(speech_config, audio_config)
# Use a one-time, synchronous call to transcribe the speech
speech = speech_recognizer.recognize_once()
# Play audio and show transcribed text
playsound(audio_file)
print(speech.text)
4. Chuyển đổi Text to speech
import os
import matplotlib.pyplot as plt
from PIL import Image
from azure.cognitiveservices.speech import SpeechConfig, SpeechSynthesizer, AudioConfig
%matplotlib inline
# Get text to be spoken
response_text = 'Turning the light on.'
# Configure speech synthesis
speech_config = SpeechConfig(cog_key, cog_location)
speech_synthesizer = SpeechSynthesizer(speech_config)
# Transcribe text into speech
result = speech_synthesizer.speak_text(response_text)
Chúc các bạn thành công