-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcriptors.py
67 lines (53 loc) · 1.82 KB
/
transcriptors.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
"""
Filename: transcriptors.py
Author: Iliya Vereshchagin
Copyright (c) 2023. All rights reserved.
Created: 25.08.2023
Last Modified: 17.10.2023
Description:
This module contains implementation for Custom Transcriptor
"""
import speech_recognition as sr
class CustomTranscriptor:
"""This is wrapper class for Google Transcriptor which uses microphone to get audio sample."""
def __init__(self, language="en-EN"):
"""
General init.
:param language: Language, what needs to be transcripted.
"""
self.___recognizer = sr.Recognizer()
self.___source = sr.Microphone()
self.___language = language
@property
def language(self):
"""
Getter for language.
:return: The language.
"""
return self.___language
@language.setter
def language(self, value):
"""
Setter for language.
:param value: The language.
"""
self.___language = value
def transcript(self):
"""
This function transcripts audio (from microphone recording) to text using Google transcriptor.
:return: transcripted text (string).
"""
print("Listening beginning...")
with self.___source as source:
audio = self.___recognizer.listen(source, timeout=5)
user_input = None
try:
user_input = self.___recognizer.recognize_google(audio, language=self.language)
except sr.UnknownValueError:
print("Google Speech Recognition can't transcript audio")
except sr.RequestError as error:
print(f"Unable to fetch from resource Google Speech Recognition: {error}")
except sr.WaitTimeoutError as error:
print(f"Input timeout, only silence is get: {error}")
return user_input