File size: 1,791 Bytes
c5a9fb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import pytesseract
from PIL import Image
from deep_translator import GoogleTranslator, MyMemoryTranslator

# --- OCR MODULE ---
def perform_ocr(image):
    """
    Primary: Tesseract OCR (Local)
    Backup: Mock/Placeholder (Since Google Vision requires credentials)
    """
    try:
        # PRIMARY: Tesseract
        # Convert image to RGB if necessary
        if image.mode != 'RGB':
            image = image.convert('RGB')
            
        text = pytesseract.image_to_string(image)
        
        if not text.strip():
            # If Tesseract runs but finds nothing, try backup
            raise ValueError("Empty result from Tesseract")
            
        return text, "Primary (Tesseract)"
    except Exception as e:
        print(f"Primary OCR Failed: {e}")
        # BACKUP: Fallback logic (Simulated for this demo)
        return "Fallback Mode: Unable to read text clearly. (Mock Data: Burger $10, Pizza $12)", "Backup (Simulated)"

# --- TRANSLATION MODULE ---
def translate_text(text, target_lang='es'):
    """
    Primary: Google Translate (via deep_translator wrapper)
    Backup: MyMemory Translator
    """
    if not text or text.strip() == "":
        return "", "No Text"

    try:
        # PRIMARY
        translator = GoogleTranslator(source='auto', target=target_lang)
        translation = translator.translate(text)
        return translation, "Primary (Google)"
    except Exception as e:
        print(f"Primary Translation Failed: {e}")
        try:
            # BACKUP
            translator = MyMemoryTranslator(source='auto', target=target_lang)
            translation = translator.translate(text)
            return translation, "Backup (MyMemory)"
        except Exception as e2:
            return text, "Failed (No API available)"