forked from Poshushukaemsya/team-number-two-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeechToText.py
33 lines (29 loc) · 1.11 KB
/
SpeechToText.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
import os
import io
from google.cloud import speech_v1
from google.cloud.speech_v1 import enums
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./path-to-json-file.json"
class SpeechToText:
def __init__(self, local_file_path):
self.local_file_path = local_file_path
def sample_recognize(self):
transcript = ''
client = speech_v1.SpeechClient()
language_code = "ru-RU"
sample_rate_hertz = 48000
encoding = enums.RecognitionConfig.AudioEncoding.LINEAR16
config = {
"language_code": language_code,
"sample_rate_hertz": sample_rate_hertz,
"encoding": encoding,
}
with io.open(self.local_file_path, "rb") as f:
content = f.read()
audio = {"content": content}
response = client.recognize(config, audio)
for result in response.results:
alternative = result.alternatives[0]
transcript = alternative.transcript
if transcript == '':
transcript = 'Не удалось перевести.'
return transcript