From 4e3da64968e332830b918bcec07bcd29277f6058 Mon Sep 17 00:00:00 2001 From: David Jeffrey Merwin <52674935+davidmerwin@users.noreply.github.com> Date: Fri, 29 Sep 2023 17:31:19 -0700 Subject: [PATCH] Sorting the files for LangMers --- .DS_Store | Bin 0 -> 6148 bytes langmersroughdraftcode.py | 116 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 .DS_Store create mode 100644 langmersroughdraftcode.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5872d9ac1c98ededfbfad955d3074538e2139240 GIT binary patch literal 6148 zcmeHKJ5EC}5S)b+k)6@?RUfdqnR9wZ{7e--EAXv}_^rw3hVqFHG@_Ik&b zr+E7ofNeex_rMy!lJ1BvALi!!?lZfoh!N>L;|(wPz!`g-X4$_7oV&t6#yego{L|t5 zJUktbgQv)(fE17dQa}nwfeR^6g>|{Q(0MvW3P^$LQNX_sjqcbB$He$_aEKOwxL`Pp z^XMgr%>%?=eJ0Q^+b(QKnhG1 zxXtC->;D7&kN$s3(n< 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() \ No newline at end of file