Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,33 +1,29 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from PIL import Image
|
| 4 |
import pytesseract
|
| 5 |
from langdetect import detect, DetectorFactory
|
| 6 |
-
from
|
| 7 |
import re
|
| 8 |
-
import io
|
| 9 |
-
from pprint import pprint
|
| 10 |
import numpy as np
|
| 11 |
import cv2
|
| 12 |
-
from PIL import Image, ImageEnhance, ImageFilter
|
| 13 |
import unicodedata
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Fix language detection randomness
|
| 16 |
DetectorFactory.seed = 0
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
image = Image.open(image_path)
|
| 27 |
-
return image
|
| 28 |
-
except Exception as e:
|
| 29 |
-
print("Error loading image:", e)
|
| 30 |
-
return None
|
| 31 |
|
| 32 |
def perform_ocr(image):
|
| 33 |
try:
|
|
@@ -73,17 +69,6 @@ def perform_ocr(image):
|
|
| 73 |
return None
|
| 74 |
|
| 75 |
|
| 76 |
-
LANG_CODE_MAP = {
|
| 77 |
-
"en": "eng",
|
| 78 |
-
"ta": "tam",
|
| 79 |
-
"hi": "hin",
|
| 80 |
-
"kn": "kan",
|
| 81 |
-
"ml": "mal",
|
| 82 |
-
"te": "tel",
|
| 83 |
-
}
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
def clean_ocr_text(text):
|
| 88 |
# Normalize unicode (fix weird diacritics, spacing issues)
|
| 89 |
text = unicodedata.normalize("NFKC", text)
|
|
@@ -114,14 +99,6 @@ def clean_ocr_text(text):
|
|
| 114 |
return text
|
| 115 |
|
| 116 |
|
| 117 |
-
def preprocess_image(image):
|
| 118 |
-
"""Convert to grayscale, remove noise, and improve text clarity."""
|
| 119 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 120 |
-
gray = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
|
| 121 |
-
gray = cv2.medianBlur(gray, 3)
|
| 122 |
-
return gray
|
| 123 |
-
|
| 124 |
-
|
| 125 |
def preprocess_image(image):
|
| 126 |
if image is None: # Check if image is None
|
| 127 |
print("Error: Input image is None.")
|
|
@@ -166,20 +143,26 @@ def detect_language(text_data):
|
|
| 166 |
return None
|
| 167 |
|
| 168 |
|
| 169 |
-
def
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
-
try:
|
| 176 |
-
translator = Translator()
|
| 177 |
-
translation = translator.translate(text_data['original_text'], src=src_lang, dest='en')
|
| 178 |
-
print("\nTranslation to English completed.")
|
| 179 |
-
return translation.text
|
| 180 |
-
except Exception as e:
|
| 181 |
-
print(f"Translation error: {e}")
|
| 182 |
-
return text_data['original_text']
|
| 183 |
|
| 184 |
|
| 185 |
def extract_field_from_lines(lines, patterns):
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
import base64
|
| 3 |
+
from PIL import Image, ImageEnhance
|
| 4 |
import pytesseract
|
| 5 |
from langdetect import detect, DetectorFactory
|
| 6 |
+
from deep_translator import GoogleTranslator
|
| 7 |
import re
|
|
|
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
import cv2
|
|
|
|
| 10 |
import unicodedata
|
| 11 |
+
import io
|
| 12 |
+
from pydantic import BaseModel
|
| 13 |
+
|
| 14 |
+
pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
|
| 15 |
|
| 16 |
# Fix language detection randomness
|
| 17 |
DetectorFactory.seed = 0
|
| 18 |
|
| 19 |
+
app = FastAPI()
|
| 20 |
+
|
| 21 |
+
LANG_CODE_MAP = {
|
| 22 |
+
"en": "eng", "ta": "tam", "hi": "hin",
|
| 23 |
+
"kn": "kan", "ml": "mal", "te": "tel",
|
| 24 |
+
"bn": "ben", "gu": "guj", "pa": "pan", "mr": "mar"
|
| 25 |
+
}
|
| 26 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def perform_ocr(image):
|
| 29 |
try:
|
|
|
|
| 69 |
return None
|
| 70 |
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
def clean_ocr_text(text):
|
| 73 |
# Normalize unicode (fix weird diacritics, spacing issues)
|
| 74 |
text = unicodedata.normalize("NFKC", text)
|
|
|
|
| 99 |
return text
|
| 100 |
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
def preprocess_image(image):
|
| 103 |
if image is None: # Check if image is None
|
| 104 |
print("Error: Input image is None.")
|
|
|
|
| 143 |
return None
|
| 144 |
|
| 145 |
|
| 146 |
+
def perform_ocr(image):
|
| 147 |
+
text = pytesseract.image_to_string(
|
| 148 |
+
image,
|
| 149 |
+
lang='eng+tam+kan+hin+tel+mal+ben+guj+pan+mar',
|
| 150 |
+
config='--psm 6'
|
| 151 |
+
).strip()
|
| 152 |
+
detected_lang = detect(text) if text else "en"
|
| 153 |
+
translated_text = None
|
| 154 |
+
if detected_lang != 'en' and text:
|
| 155 |
+
try:
|
| 156 |
+
translated_text = GoogleTranslator(source=detected_lang, target="en").translate(text)
|
| 157 |
+
except Exception as e:
|
| 158 |
+
translated_text = f"[Translation failed: {e}]"
|
| 159 |
+
|
| 160 |
+
return {
|
| 161 |
+
"detected_language": detected_lang,
|
| 162 |
+
"original_text": text,
|
| 163 |
+
"translated_text": translated_text
|
| 164 |
+
}
|
| 165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
|
| 168 |
def extract_field_from_lines(lines, patterns):
|