Update app.py
Browse files
app.py
CHANGED
|
@@ -15,19 +15,35 @@ import easyocr
|
|
| 15 |
import numpy as np
|
| 16 |
from shapely.geometry import Polygon
|
| 17 |
from paddleocr import PaddleOCR
|
|
|
|
|
|
|
| 18 |
|
| 19 |
@st.cache_resource
|
| 20 |
def load_models():
|
| 21 |
doctr_model = ocr_predictor(pretrained=True)
|
| 22 |
-
|
| 23 |
-
paddleocr_reader = PaddleOCR(use_angle_cls=True, lang='ml') # 'ml' for multilingual support
|
| 24 |
-
return doctr_model, easyocr_reader, paddleocr_reader
|
| 25 |
|
| 26 |
-
doctr_model
|
| 27 |
|
| 28 |
# AWS Textract client
|
| 29 |
textract_client = boto3.client('textract', region_name='us-west-2')
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def extract_text_aws(image_bytes):
|
| 32 |
try:
|
| 33 |
response = textract_client.detect_document_text(Document={'Bytes': image_bytes})
|
|
@@ -47,17 +63,19 @@ def extract_text_doctr(image_path):
|
|
| 47 |
st.error(f"Error in Doctr OCR: {str(e)}")
|
| 48 |
return []
|
| 49 |
|
| 50 |
-
def extract_text_easyocr(image_path):
|
| 51 |
try:
|
| 52 |
-
|
|
|
|
| 53 |
return [(detection[1], detection[0], detection[2]) for detection in result]
|
| 54 |
except Exception as e:
|
| 55 |
st.error(f"Error in EasyOCR: {str(e)}")
|
| 56 |
return []
|
| 57 |
|
| 58 |
-
def extract_text_paddleocr(image_path):
|
| 59 |
try:
|
| 60 |
-
|
|
|
|
| 61 |
return [(line[1][0], line[0], line[1][1]) for line in result[0]]
|
| 62 |
except Exception as e:
|
| 63 |
st.error(f"Error in PaddleOCR: {str(e)}")
|
|
@@ -115,11 +133,21 @@ def main():
|
|
| 115 |
|
| 116 |
st.image(image, caption='Uploaded CV', use_column_width=True)
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
results = {
|
| 119 |
"aws": extract_text_aws(uploaded_file.getvalue()),
|
| 120 |
"doctr": extract_text_doctr("temp_cv_image.png"),
|
| 121 |
-
"easyocr": extract_text_easyocr("temp_cv_image.png"),
|
| 122 |
-
"paddleocr": extract_text_paddleocr("temp_cv_image.png"),
|
| 123 |
}
|
| 124 |
|
| 125 |
weights = {"aws": 0.4, "doctr": 0.3, "easyocr": 0.2, "paddleocr": 0.1}
|
|
|
|
| 15 |
import numpy as np
|
| 16 |
from shapely.geometry import Polygon
|
| 17 |
from paddleocr import PaddleOCR
|
| 18 |
+
import pytesseract
|
| 19 |
+
from langdetect import detect_langs
|
| 20 |
|
| 21 |
@st.cache_resource
|
| 22 |
def load_models():
|
| 23 |
doctr_model = ocr_predictor(pretrained=True)
|
| 24 |
+
return doctr_model
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
doctr_model = load_models()
|
| 27 |
|
| 28 |
# AWS Textract client
|
| 29 |
textract_client = boto3.client('textract', region_name='us-west-2')
|
| 30 |
|
| 31 |
+
def detect_language(image_path):
|
| 32 |
+
try:
|
| 33 |
+
# Use Tesseract to detect the language
|
| 34 |
+
lang_code = pytesseract.image_to_osd(image_path, output_type=pytesseract.Output.DICT)['lang']
|
| 35 |
+
return lang_code
|
| 36 |
+
except:
|
| 37 |
+
st.warning("Automatic language detection failed. Attempting text-based detection...")
|
| 38 |
+
try:
|
| 39 |
+
# If image-based detection fails, try text-based detection
|
| 40 |
+
text = pytesseract.image_to_string(image_path)
|
| 41 |
+
langs = detect_langs(text)
|
| 42 |
+
return langs[0].lang if langs else None
|
| 43 |
+
except:
|
| 44 |
+
st.error("Language detection failed. Please select the language manually.")
|
| 45 |
+
return None
|
| 46 |
+
|
| 47 |
def extract_text_aws(image_bytes):
|
| 48 |
try:
|
| 49 |
response = textract_client.detect_document_text(Document={'Bytes': image_bytes})
|
|
|
|
| 63 |
st.error(f"Error in Doctr OCR: {str(e)}")
|
| 64 |
return []
|
| 65 |
|
| 66 |
+
def extract_text_easyocr(image_path, lang):
|
| 67 |
try:
|
| 68 |
+
reader = easyocr.Reader([lang])
|
| 69 |
+
result = reader.readtext(image_path)
|
| 70 |
return [(detection[1], detection[0], detection[2]) for detection in result]
|
| 71 |
except Exception as e:
|
| 72 |
st.error(f"Error in EasyOCR: {str(e)}")
|
| 73 |
return []
|
| 74 |
|
| 75 |
+
def extract_text_paddleocr(image_path, lang):
|
| 76 |
try:
|
| 77 |
+
reader = PaddleOCR(use_angle_cls=True, lang=lang)
|
| 78 |
+
result = reader.ocr(image_path, cls=True)
|
| 79 |
return [(line[1][0], line[0], line[1][1]) for line in result[0]]
|
| 80 |
except Exception as e:
|
| 81 |
st.error(f"Error in PaddleOCR: {str(e)}")
|
|
|
|
| 133 |
|
| 134 |
st.image(image, caption='Uploaded CV', use_column_width=True)
|
| 135 |
|
| 136 |
+
# Detect language
|
| 137 |
+
detected_lang = detect_language("temp_cv_image.png")
|
| 138 |
+
|
| 139 |
+
if detected_lang:
|
| 140 |
+
st.write(f"Detected language: {detected_lang}")
|
| 141 |
+
else:
|
| 142 |
+
# If language detection failed, let the user select the language
|
| 143 |
+
lang_options = ['eng', 'fra', 'deu', 'spa', 'ita', 'por', 'rus', 'ara', 'hin', 'jpn', 'kor', 'chi_sim']
|
| 144 |
+
detected_lang = st.selectbox("Select the document language:", lang_options)
|
| 145 |
+
|
| 146 |
results = {
|
| 147 |
"aws": extract_text_aws(uploaded_file.getvalue()),
|
| 148 |
"doctr": extract_text_doctr("temp_cv_image.png"),
|
| 149 |
+
"easyocr": extract_text_easyocr("temp_cv_image.png", detected_lang),
|
| 150 |
+
"paddleocr": extract_text_paddleocr("temp_cv_image.png", detected_lang),
|
| 151 |
}
|
| 152 |
|
| 153 |
weights = {"aws": 0.4, "doctr": 0.3, "easyocr": 0.2, "paddleocr": 0.1}
|