Update app.py
Browse files
app.py
CHANGED
|
@@ -20,8 +20,8 @@ from paddleocr import PaddleOCR
|
|
| 20 |
@st.cache_resource
|
| 21 |
def load_models():
|
| 22 |
doctr_model = ocr_predictor(pretrained=True)
|
| 23 |
-
easyocr_reader = easyocr.Reader(['en'])
|
| 24 |
-
paddleocr_reader = PaddleOCR(use_angle_cls=True, lang='en')
|
| 25 |
return doctr_model, easyocr_reader, paddleocr_reader
|
| 26 |
|
| 27 |
doctr_model, easyocr_reader, paddleocr_reader = load_models()
|
|
@@ -32,11 +32,20 @@ textract_client = boto3.client('textract', region_name='us-west-2')
|
|
| 32 |
def extract_text_aws(image_bytes):
|
| 33 |
try:
|
| 34 |
response = textract_client.detect_document_text(Document={'Bytes': image_bytes})
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
st.error(f"Error in AWS Textract: {str(e)}")
|
| 39 |
-
return []
|
| 40 |
|
| 41 |
def extract_text_doctr(image_path):
|
| 42 |
try:
|
|
@@ -48,17 +57,17 @@ def extract_text_doctr(image_path):
|
|
| 48 |
st.error(f"Error in Doctr OCR: {str(e)}")
|
| 49 |
return []
|
| 50 |
|
| 51 |
-
def extract_text_easyocr(image_path):
|
| 52 |
try:
|
| 53 |
-
result =
|
| 54 |
return [(detection[1], detection[0], detection[2]) for detection in result]
|
| 55 |
except Exception as e:
|
| 56 |
st.error(f"Error in EasyOCR: {str(e)}")
|
| 57 |
return []
|
| 58 |
|
| 59 |
-
def extract_text_paddleocr(image_path):
|
| 60 |
try:
|
| 61 |
-
result =
|
| 62 |
return [(line[1][0], line[0], line[1][1]) for line in result[0]]
|
| 63 |
except Exception as e:
|
| 64 |
st.error(f"Error in PaddleOCR: {str(e)}")
|
|
@@ -103,7 +112,7 @@ def combine_ocr_results(results, weights):
|
|
| 103 |
return ' '.join(final_words)
|
| 104 |
|
| 105 |
def main():
|
| 106 |
-
st.title("CV Text Extraction and Combination")
|
| 107 |
|
| 108 |
uploaded_file = st.file_uploader("Choose a CV image file", type=["png", "jpg", "jpeg"])
|
| 109 |
|
|
@@ -116,11 +125,20 @@ def main():
|
|
| 116 |
|
| 117 |
st.image(image, caption='Uploaded CV', use_column_width=True)
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
results = {
|
| 120 |
-
"aws":
|
| 121 |
"doctr": extract_text_doctr("temp_cv_image.png"),
|
| 122 |
-
"easyocr": extract_text_easyocr("temp_cv_image.png"),
|
| 123 |
-
"paddleocr": extract_text_paddleocr("temp_cv_image.png"),
|
| 124 |
}
|
| 125 |
|
| 126 |
weights = {"aws": 0.4, "doctr": 0.3, "easyocr": 0.2, "paddleocr": 0.1}
|
|
@@ -138,5 +156,5 @@ def main():
|
|
| 138 |
|
| 139 |
st.write("Note: This application combines text extracted from the CV using multiple OCR methods to improve accuracy.")
|
| 140 |
|
| 141 |
-
if
|
| 142 |
main()
|
|
|
|
| 20 |
@st.cache_resource
|
| 21 |
def load_models():
|
| 22 |
doctr_model = ocr_predictor(pretrained=True)
|
| 23 |
+
easyocr_reader = easyocr.Reader(['en']) # Initialize with English, we'll update languages dynamically
|
| 24 |
+
paddleocr_reader = PaddleOCR(use_angle_cls=True, lang='en') # Initialize with English, we'll update language dynamically
|
| 25 |
return doctr_model, easyocr_reader, paddleocr_reader
|
| 26 |
|
| 27 |
doctr_model, easyocr_reader, paddleocr_reader = load_models()
|
|
|
|
| 32 |
def extract_text_aws(image_bytes):
|
| 33 |
try:
|
| 34 |
response = textract_client.detect_document_text(Document={'Bytes': image_bytes})
|
| 35 |
+
text_results = [(item['Text'], item['Geometry']['BoundingBox'], item['Confidence'])
|
| 36 |
+
for item in response['Blocks'] if item['BlockType'] == 'WORD']
|
| 37 |
+
|
| 38 |
+
# Detect dominant language
|
| 39 |
+
lang_response = textract_client.detect_document_text(
|
| 40 |
+
Document={'Bytes': image_bytes},
|
| 41 |
+
FeatureTypes=['LANGUAGE']
|
| 42 |
+
)
|
| 43 |
+
detected_language = lang_response['DocumentMetadata']['Languages'][0]['LanguageCode']
|
| 44 |
+
|
| 45 |
+
return text_results, detected_language
|
| 46 |
except Exception as e:
|
| 47 |
st.error(f"Error in AWS Textract: {str(e)}")
|
| 48 |
+
return [], 'en' # Default to English if detection fails
|
| 49 |
|
| 50 |
def extract_text_doctr(image_path):
|
| 51 |
try:
|
|
|
|
| 57 |
st.error(f"Error in Doctr OCR: {str(e)}")
|
| 58 |
return []
|
| 59 |
|
| 60 |
+
def extract_text_easyocr(image_path, reader):
|
| 61 |
try:
|
| 62 |
+
result = reader.readtext(image_path)
|
| 63 |
return [(detection[1], detection[0], detection[2]) for detection in result]
|
| 64 |
except Exception as e:
|
| 65 |
st.error(f"Error in EasyOCR: {str(e)}")
|
| 66 |
return []
|
| 67 |
|
| 68 |
+
def extract_text_paddleocr(image_path, reader):
|
| 69 |
try:
|
| 70 |
+
result = reader.ocr(image_path, cls=True)
|
| 71 |
return [(line[1][0], line[0], line[1][1]) for line in result[0]]
|
| 72 |
except Exception as e:
|
| 73 |
st.error(f"Error in PaddleOCR: {str(e)}")
|
|
|
|
| 112 |
return ' '.join(final_words)
|
| 113 |
|
| 114 |
def main():
|
| 115 |
+
st.title("Multilingual CV Text Extraction and Combination")
|
| 116 |
|
| 117 |
uploaded_file = st.file_uploader("Choose a CV image file", type=["png", "jpg", "jpeg"])
|
| 118 |
|
|
|
|
| 125 |
|
| 126 |
st.image(image, caption='Uploaded CV', use_column_width=True)
|
| 127 |
|
| 128 |
+
# Use AWS Textract for language detection and initial OCR
|
| 129 |
+
aws_results, detected_lang = extract_text_aws(uploaded_file.getvalue())
|
| 130 |
+
|
| 131 |
+
st.info(f"Detected language: {detected_lang}")
|
| 132 |
+
|
| 133 |
+
# Update EasyOCR and PaddleOCR with the detected language
|
| 134 |
+
easyocr_reader = easyocr.Reader([detected_lang])
|
| 135 |
+
paddleocr_reader = PaddleOCR(use_angle_cls=True, lang=detected_lang)
|
| 136 |
+
|
| 137 |
results = {
|
| 138 |
+
"aws": aws_results,
|
| 139 |
"doctr": extract_text_doctr("temp_cv_image.png"),
|
| 140 |
+
"easyocr": extract_text_easyocr("temp_cv_image.png", easyocr_reader),
|
| 141 |
+
"paddleocr": extract_text_paddleocr("temp_cv_image.png", paddleocr_reader),
|
| 142 |
}
|
| 143 |
|
| 144 |
weights = {"aws": 0.4, "doctr": 0.3, "easyocr": 0.2, "paddleocr": 0.1}
|
|
|
|
| 156 |
|
| 157 |
st.write("Note: This application combines text extracted from the CV using multiple OCR methods to improve accuracy.")
|
| 158 |
|
| 159 |
+
if __name__ == "__main__":
|
| 160 |
main()
|