forked from davidmerwin/steve-langmers-systems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlangmersroughdraftcode.py
116 lines (97 loc) · 3.53 KB
/
langmersroughdraftcode.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import pytesseract
from PIL import Image
from translate import Translator
from gtts import gTTS
from playsound import playsound
from langdetect import detect
from ctypes import cdll
import os
import random
lib = cdll.LoadLibrary('./hardware.so')
class LangMers:
"""
A class for the LangMers system.
Attributes:
----------
image_path : str
The path to the image. Default value is '/path/to/image.jpg'.
translator : Translator
The Translator object for translating text.
Methods:
-------
capture_image():
Captures the image.
get_geolocation_data():
Gets the geolocation data from the libraby function.
extract_text_from_image():
Extracts text from the input image using pyTesseract.
recognize_language(text: str) -> str:
Recognizes language of the input text.
translate_text(text: str, dest_language: str = 'fr') -> str:
Translates the input text into the destination language. Default destination language is french.
word_generator(text: str) -> Generator:
Yields word in the text.
language_processing(text: str, dest_language: str = 'fr') -> str:
Process the language and returns the translated text.
build_pronunciation_feedback(text: str, detected_language: str):
Saves the pronunciation of the input text in an audio file and plays it.
run():
Main function to run the LangMers system.
"""
def __init__(self, image_path='/path/to/image.jpg'):
self.image_path = image_path
self.translator = Translator(service_urls=['translate.google.com'])
def capture_image(self):
try:
lib.capture_image()
except Exception as e:
print(f"Unable to capture image: {str(e)}")
def get_geolocation_data(self):
try:
lib.get_geolocation_data()
except Exception as e:
print(f"Unable to get geolocation data: {str(e)}")
def extract_text_from_image(self):
try:
img = Image.open(self.image_path)
text = pytesseract.image_to_string(img, lang="eng")
return text
except Exception as e:
print(f"Unable to open file: {str(e)}")
def recognize_language(self, text):
try:
return detect(text)
except:
return "Unable to detect language"
def translate_text(self, text, dest_language='fr'):
try:
result = self.translator.translate(text, dest_language)
return result.text
except Exception as e:
return f"Couldn't translate due to : {str(e)}"
@staticmethod
def word_generator(text):
for word in text.split():
yield word
def language_processing(self, text, dest_language='fr'):
result = self.translate_text(text, dest_language)
return result
def build_pronunciation_feedback(self, text, detected_language):
try:
tts = gTTS(text=text, lang=detected_language)
audio_path = 'text_audio.mp3'
tts.save(audio_path)
playsound(audio_path)
except Exception as e:
print(f'Error in build pronunciation feedback: {str(e)}')
def run(self):
self.capture_image()
self.get_geolocation_data()
text = self.extract_text_from_image()
detected_language = self.recognize_language(text)
self.build_pronunciation_feedback(text, detected_language)
def main():
langMers = LangMers()
langMers.run()
if __name__ == "__main__":
main()