Create ocr_text.py
Browse files- ocr_text.py +40 -0
ocr_text.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from surya.input.langs import replace_lang_with_code, get_unique_langs
|
4 |
+
from surya.input.load import load_from_folder, load_from_file, load_lang_file
|
5 |
+
from surya.model.detection.model import load_model as load_detection_model, load_processor as load_detection_processor
|
6 |
+
from surya.model.recognition.model import load_model as load_recognition_model
|
7 |
+
from surya.model.recognition.processor import load_processor as load_recognition_processor
|
8 |
+
from surya.model.recognition.tokenizer import _tokenize
|
9 |
+
from surya.ocr import run_ocr
|
10 |
+
from surya.postprocessing.text import draw_text_on_image
|
11 |
+
|
12 |
+
|
13 |
+
def main(input_path, max_pages=None, start_page=0, langs=None, lang_file=None):
|
14 |
+
|
15 |
+
assert langs or lang_file, "Must provide either langs or lang_file"
|
16 |
+
|
17 |
+
if os.path.isdir(input_path):
|
18 |
+
images, names = load_from_folder(input_path, max_pages, start_page)
|
19 |
+
else:
|
20 |
+
images, names = load_from_file(input_path, max_pages, start_page)
|
21 |
+
|
22 |
+
|
23 |
+
langs = langs.split(",")
|
24 |
+
replace_lang_with_code(langs)
|
25 |
+
image_langs = [langs] * len(images)
|
26 |
+
|
27 |
+
det_processor = load_detection_processor()
|
28 |
+
det_model = load_detection_model()
|
29 |
+
|
30 |
+
_, lang_tokens = _tokenize("", get_unique_langs(image_langs))
|
31 |
+
rec_model = load_recognition_model(langs=lang_tokens) # Prune model moe layer to only include languages we need
|
32 |
+
rec_processor = load_recognition_processor()
|
33 |
+
|
34 |
+
predictions_by_image = run_ocr(images, image_langs, det_model, det_processor, rec_model, rec_processor)
|
35 |
+
|
36 |
+
for idx, (name, image, pred, langs) in enumerate(zip(names, images, predictions_by_image, image_langs)):
|
37 |
+
bboxes = [l.bbox for l in pred.text_lines]
|
38 |
+
pred_text = [l.text for l in pred.text_lines]
|
39 |
+
page_image = draw_text_on_image(bboxes, pred_text, image.size, langs, has_math="_math" in langs)
|
40 |
+
return page_image
|