I have written a python code that translates languages from tge following input types: text, inage and audio. I am using the googletrans api for translating but it is giving htis error on running : 'NoneType' object has no attribute 'group'. But this exact same code works fine on a linux system. I used the pip package manager in both the system to install the module.
import googletrans
import pytesseract
import speech_recognition as sr
import cv2 # OpenCV for camera access
def extract_text_from_image(image):
text = pytesseract.image_to_string(image)
return text
def translate_text(text, target_language):
translator = googletrans.Translator()
translation = translator.translate(text, dest=target_language)
return translation.text
def translate_speech(target_language):
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = recognizer.listen(source, timeout=5)
try:
recognized_speech = recognizer.recognize_google(audio)
translation = translate_text(recognized_speech, target_language)
return recognized_speech, translation
except sr.UnknownValueError:
return None, None
def main():
input_type = input("Enter 'text', 'image', or 'audio' for input: ").lower()
target_language = input("Enter the target language (e.g., 'fr' for French): ").lower()
if input_type == "text":
text = input("Enter the text to translate: ")
elif input_type == "image":
image_path = input("Enter the path to the image: ")
frame = cv2.imread(image_path)
if frame is not None:
# Extract text from the image
image_text = extract_text_from_image(frame)
if image_text:
print("Text extracted from the image:", image_text)
text = image_text
else:
print("No text found in the image.")
return
else:
print("Image not found or invalid path.")
return
elif input_type == "audio":
recognized_speech, translation = translate_speech(target_language)
if recognized_speech is not None:
print("Recognized speech:", recognized_speech)
print("Translated speech:", translation)
else:
print("Could not recognize speech.")
return
else:
print("Invalid input type.")
return
translated_text = translate_text(text, target_language)
print("Translated text:")
print(translated_text)
if __name__ == "__main__":
main()
The following error is displayed on ruunig the above code :
PS C:\Users\Deepak> python -u "c:\Users\Deepak\OneDrive\Translator\new.py"
Enter 'text', 'image', or 'audio' for input: text
Enter the target language (e.g., 'fr' for French): fr
Enter the text to translate: hello! how are you?
Traceback (most recent call last):
File "c:\Users\Deepak\OneDrive\Translator\new.py", line 67, in <module>
main()
File "c:\Users\Deepak\OneDrive\Translator\new.py", line 62, in main
translated_text = translate_text(text, target_language)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\Deepak\OneDrive\Translator\new.py", line 12, in translate_text
translation = translator.translate(text, dest=target_language)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Deepak\AppData\Local\Programs\Python\Python312\Lib\site-packages\googletrans\client.py", line 182, in translate
data = self._translate(text, dest, src, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Deepak\AppData\Local\Programs\Python\Python312\Lib\site-packages\googletrans\client.py", line 78, in _translate
token = self.token_acquirer.do(text)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Deepak\AppData\Local\Programs\Python\Python312\Lib\site-packages\googletrans\gtoken.py", line 194, in do
self._update()
File "C:\Users\Deepak\AppData\Local\Programs\Python\Python312\Lib\site-packages\googletrans\gtoken.py", line 62, in _update
code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'group'
PS C:\Users\Deepak>
The most likely cause of your problem here is that the
search(r.text)
call is returningNone
on the Windows box, though it's hard to be certain without seeing the code ofRE_TKK
. Ifgoogletrans
relies on a network call (I haven't used it), it could be the Windows machine isn't configured to allow your application to issue HTTP calls, in which case it wouldn't be surprising for the search results to be empty (and you won't see what's going on if you're catching I/O exceptions without logging them.)