36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
import os
|
|
from google.cloud import speech
|
|
from google.cloud.speech import RecognitionConfig, RecognitionAudio
|
|
|
|
|
|
# Set the path to your Google Cloud service account key
|
|
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "credentials.json"
|
|
|
|
# Initialize the Google Cloud Speech client
|
|
client = speech.SpeechClient()
|
|
|
|
# Path to the audio file
|
|
audio_file_path = "he_speech_demo.wav"
|
|
|
|
# Read the audio file
|
|
with open(audio_file_path, 'rb') as audio_file:
|
|
content = audio_file.read()
|
|
|
|
# Create a RecognitionAudio instance with the audio content
|
|
audio = RecognitionAudio(content=content)
|
|
|
|
# Create a RecognitionConfig instance with the appropriate settings
|
|
config = RecognitionConfig(
|
|
#encoding=RecognitionConfig.AudioEncoding.LINEAR16,
|
|
sample_rate_hertz=8000,
|
|
model='telephony',
|
|
language_code='iw-IL'
|
|
)
|
|
|
|
# Transcribe the audio file
|
|
response = client.recognize(config=config, audio=audio)
|
|
|
|
# Print the transcription
|
|
for result in response.results:
|
|
print('Transcript: {}'.format(result.alternatives[0].transcript))
|