-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRecognizer.py
41 lines (34 loc) · 1.43 KB
/
Recognizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from google.cloud import speech_v1
from google.cloud.speech_v1 import enums
import io
class GoogleSpeechRecognizer(object):
def __init__(self):
super(GoogleSpeechRecognizer, self).__init__()
#you need to put your own google api json here
self.client = speech_v1.SpeechClient.from_service_account_json("gapiKey.json")
self.language_code = "en-US"
def recognize(self, local_file_path):
"""
Transcribe a short audio file using synchronous speech recognition
Args:
local_file_path Path to local audio file, e.g. /path/audio.wav
"""
# Sample rate in Hertz of the audio data sent
sample_rate_hertz = 16000
# Encoding of audio data sent. This sample sets this explicitly.
# This field is optional for FLAC and WAV audio formats.
encoding = enums.RecognitionConfig.AudioEncoding.FLAC
config = {
"language_code": self.language_code,
"sample_rate_hertz": sample_rate_hertz,
"encoding": encoding,
}
with io.open(local_file_path, "rb") as f:
content = f.read()
audio = {"content": content}
response = self.client.recognize(config, audio)
for result in response.results:
# First alternative is the most probable result
alternative = result.alternatives[0]
return alternative.transcript
return None