Spaces:
Sleeping
Sleeping
Remove MMOCR
Browse files- app_pages/ocr_comparator.py +181 -171
app_pages/ocr_comparator.py
CHANGED
@@ -2,12 +2,12 @@
|
|
2 |
EasyOcr, PaddleOCR, MMOCR, Tesseract
|
3 |
"""
|
4 |
|
5 |
-
import mim
|
6 |
-
|
7 |
-
mim.install(['mmengine>=0.7.1,<1.1.0'])
|
8 |
-
mim.install(['mmcv>=2.0.0rc4,<2.1.0'])
|
9 |
-
mim.install(['mmdet>=3.0.rc5,<3.2.0'])
|
10 |
-
mim.install(['mmocr'])
|
11 |
|
12 |
import streamlit as st
|
13 |
import plotly.express as px
|
@@ -21,7 +21,7 @@ from PIL import Image, ImageColor
|
|
21 |
import PIL
|
22 |
import easyocr
|
23 |
from paddleocr import PaddleOCR
|
24 |
-
from mmocr.utils.ocr import MMOCR
|
25 |
import pytesseract
|
26 |
from pytesseract import Output
|
27 |
import os
|
@@ -80,8 +80,10 @@ def app():
|
|
80 |
plotly figure : confidence color scale figure
|
81 |
"""
|
82 |
# the readers considered
|
83 |
-
out_reader_type_list = ['EasyOCR', 'PPOCR', 'MMOCR', 'Tesseract']
|
84 |
-
out_reader_type_dict = {'EasyOCR': 0, 'PPOCR': 1, 'MMOCR': 2, 'Tesseract': 3}
|
|
|
|
|
85 |
|
86 |
# Columns for recognition details results
|
87 |
out_cols_size = [2] + [2,1]*(len(out_reader_type_list)-1) # Except Tesseract
|
@@ -123,7 +125,7 @@ def app():
|
|
123 |
'Tagalog': 'tl', 'Tamil': 'ta', 'Telugu': 'te', 'Turkish': 'tr', 'Ukranian': 'uk', \
|
124 |
'Urdu': 'ur', 'Uyghur': 'ug', 'Uzbek': 'uz', 'Vietnamese': 'vi', 'Welsh': 'cy'}
|
125 |
|
126 |
-
out_dict_lang_mmocr = {'English & Chinese': 'en'}
|
127 |
|
128 |
out_dict_lang_tesseract = {'Afrikaans': 'afr','Albanian': 'sqi','Amharic': 'amh', \
|
129 |
'Arabic': 'ara', 'Armenian': 'hye','Assamese': 'asm','Azerbaijani - Cyrilic': 'aze_cyrl', \
|
@@ -156,7 +158,8 @@ def app():
|
|
156 |
'Uzbek - Cyrilic': 'uzb_cyrl','Uzbek': 'uzb','Vietnamese': 'vie','Welsh': 'cym', \
|
157 |
'Western Frisian': 'fry','Yiddish': 'yid','Yoruba': 'yor'}
|
158 |
|
159 |
-
out_list_dict_lang = [out_dict_lang_easyocr, out_dict_lang_ppocr,
|
|
|
160 |
out_dict_lang_tesseract]
|
161 |
|
162 |
# Initialization of detection form
|
@@ -221,18 +224,18 @@ def app():
|
|
221 |
return out_ocr
|
222 |
|
223 |
###
|
224 |
-
|
225 |
-
def init_mmocr(in_params):
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
|
237 |
###
|
238 |
def init_readers(in_list_params):
|
@@ -255,10 +258,10 @@ def app():
|
|
255 |
reader_ppocr = init_ppocr(in_list_params[1])
|
256 |
|
257 |
# - MMOCR
|
258 |
-
with st.spinner("MMOCR reader initialization in progress ..."):
|
259 |
-
|
260 |
|
261 |
-
out_list_readers = [reader_easyocr, reader_ppocr
|
262 |
|
263 |
return out_list_readers
|
264 |
|
@@ -351,44 +354,44 @@ def app():
|
|
351 |
return out_ppocr_boxes_coordinates, out_status
|
352 |
|
353 |
###
|
354 |
-
|
355 |
-
def mmocr_detect(_in_reader, in_image_path):
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
|
393 |
###
|
394 |
def cropped_1box(in_box, in_img):
|
@@ -481,14 +484,14 @@ def app():
|
|
481 |
##
|
482 |
|
483 |
## ------- MMOCR Text detection
|
484 |
-
with st.spinner('MMOCR Text detection in progress ...'):
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
##
|
493 |
|
494 |
## ------- Tesseract Text detection
|
@@ -505,9 +508,11 @@ def app():
|
|
505 |
##
|
506 |
#
|
507 |
out_list_images = _in_list_images + [easyocr_image_detect, ppocr_image_detect, \
|
508 |
-
|
|
|
509 |
out_list_coordinates = [easyocr_boxes_coordinates, ppocr_boxes_coordinates, \
|
510 |
-
|
|
|
511 |
#
|
512 |
|
513 |
return out_list_images, out_list_coordinates
|
@@ -588,8 +593,8 @@ def app():
|
|
588 |
list_confidence_easyocr = []
|
589 |
list_text_ppocr = []
|
590 |
list_confidence_ppocr = []
|
591 |
-
list_text_mmocr = []
|
592 |
-
list_confidence_mmocr = []
|
593 |
|
594 |
# Create cropped images from detection
|
595 |
list_cropped_images = get_cropped(in_boxes_coordinates, in_image_cv)
|
@@ -607,9 +612,9 @@ def app():
|
|
607 |
##
|
608 |
|
609 |
# Recognize with MMOCR
|
610 |
-
with st.spinner('MMOCR Text recognition in progress ...'):
|
611 |
-
|
612 |
-
|
613 |
##
|
614 |
|
615 |
# Recognize with Tesseract
|
@@ -624,12 +629,13 @@ def app():
|
|
624 |
'confidence_easyocr': list_confidence_easyocr,
|
625 |
'text_ppocr': list_text_ppocr,
|
626 |
'confidence_ppocr': list_confidence_ppocr,
|
627 |
-
'text_mmocr': list_text_mmocr,
|
628 |
-
'confidence_mmocr': list_confidence_mmocr
|
629 |
}
|
630 |
)
|
631 |
|
632 |
-
out_list_reco_status = [status_easyocr, status_ppocr, status_mmocr, status_tesseract]
|
|
|
633 |
|
634 |
return out_df_results, out_df_results_tesseract, out_list_reco_status
|
635 |
|
@@ -711,83 +717,83 @@ def app():
|
|
711 |
return out_list_text_ppocr, out_list_confidence_ppocr, out_status
|
712 |
|
713 |
###
|
714 |
-
|
715 |
-
def mmocr_recog(in_list_images, in_params):
|
716 |
-
|
717 |
-
|
718 |
-
|
719 |
-
|
720 |
-
|
721 |
-
|
722 |
-
|
723 |
-
|
724 |
-
|
725 |
-
|
726 |
-
|
727 |
-
|
728 |
-
|
729 |
-
|
730 |
-
|
731 |
-
|
732 |
-
|
733 |
-
|
734 |
-
|
735 |
-
|
736 |
-
|
737 |
-
|
738 |
-
|
739 |
-
|
740 |
-
|
741 |
-
|
742 |
-
|
743 |
-
|
744 |
-
|
745 |
-
|
746 |
-
|
747 |
-
|
748 |
-
|
749 |
-
|
750 |
-
|
751 |
-
|
752 |
-
|
753 |
-
|
754 |
-
|
755 |
-
def tesserocr_recog(in_img, in_params, in_nb_images):
|
756 |
-
|
757 |
-
|
758 |
-
|
759 |
-
|
760 |
-
|
761 |
-
|
762 |
-
|
763 |
-
|
764 |
-
|
765 |
-
|
766 |
-
|
767 |
-
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
|
780 |
-
|
781 |
-
|
782 |
-
|
783 |
-
|
784 |
-
|
785 |
-
|
786 |
-
|
787 |
-
|
788 |
-
|
789 |
-
|
790 |
-
|
791 |
|
792 |
###
|
793 |
def draw_reco_images(in_image, in_boxes_coordinates, in_list_texts, in_list_confid, \
|
@@ -939,13 +945,13 @@ def app():
|
|
939 |
# Clear caches
|
940 |
easyocr_detect.clear()
|
941 |
ppocr_detect.clear()
|
942 |
-
mmocr_detect.clear()
|
943 |
tesserocr_detect.clear()
|
944 |
process_detect.clear()
|
945 |
get_cropped.clear()
|
946 |
easyocr_recog.clear()
|
947 |
ppocr_recog.clear()
|
948 |
-
mmocr_recog.clear()
|
949 |
tesserocr_recog.clear()
|
950 |
|
951 |
|
@@ -953,6 +959,7 @@ def app():
|
|
953 |
#print("PID : ", os.getpid())
|
954 |
|
955 |
st.title("OCR solutions comparator")
|
|
|
956 |
st.markdown("##### *EasyOCR, PPOCR, MMOCR, Tesseract*")
|
957 |
#st.markdown("#### PID : " + str(os.getpid()))
|
958 |
|
@@ -969,8 +976,8 @@ def app():
|
|
969 |
easyocr_lang = list_dict_lang[0][easyocr_key_lang]
|
970 |
ppocr_key_lang = lang_col[1].selectbox(reader_type_list[1]+" :", list_dict_lang[1].keys(), 22)
|
971 |
ppocr_lang = list_dict_lang[1][ppocr_key_lang]
|
972 |
-
mmocr_key_lang = lang_col[2].selectbox(reader_type_list[2]+" :", list_dict_lang[2].keys(), 0)
|
973 |
-
mmocr_lang = list_dict_lang[2][mmocr_key_lang]
|
974 |
tesserocr_key_lang = lang_col[3].selectbox(reader_type_list[3]+" :", list_dict_lang[3].keys(), 35)
|
975 |
tesserocr_lang = list_dict_lang[3][tesserocr_key_lang]
|
976 |
|
@@ -1076,7 +1083,7 @@ def app():
|
|
1076 |
help='''slow: use polygon box to calculate bbox score, fast: use rectangle box \
|
1077 |
to calculate. (default = fast) \n
|
1078 |
Use rectlar box to calculate faster, and polygonal box more accurate for curved text area.''')
|
1079 |
-
|
1080 |
with col2.expander("Choose detection hyperparameters for " + reader_type_list[2], \
|
1081 |
expanded=False):
|
1082 |
t2_det = st.selectbox('det', ['DB_r18','DB_r50','DBPP_r50','DRRG','FCE_IC15', \
|
@@ -1088,8 +1095,9 @@ def app():
|
|
1088 |
[here](https://mmocr.readthedocs.io/en/latest/textdet_models.html)")
|
1089 |
t2_merge_xdist = st.slider('merge_xdist', 1, 50, 20, step=1, \
|
1090 |
help='The maximum x-axis distance to merge boxes. (defaut=20)')
|
1091 |
-
|
1092 |
-
with col2.expander("Choose detection hyperparameters for " + reader_type_list[3], \
|
|
|
1093 |
expanded=False):
|
1094 |
t3_psm = st.selectbox('Page segmentation mode (psm)', \
|
1095 |
[' - Default', \
|
@@ -1153,7 +1161,7 @@ def app():
|
|
1153 |
'det_east_cover_thresh': t1_det_east_cover_thresh, \
|
1154 |
'det_east_nms_thresh': t1_det_east_nms_thresh, \
|
1155 |
'det_db_score_mode': t1_det_db_score_mode}],
|
1156 |
-
[mmocr_lang, {'det': t2_det, 'merge_xdist': t2_merge_xdist}],
|
1157 |
[tesserocr_lang, {'lang': tesserocr_lang, 'config': t3_config}]
|
1158 |
]
|
1159 |
|
@@ -1264,7 +1272,7 @@ def app():
|
|
1264 |
t1_drop_score = st.slider('drop_score', 0., 1., 0.25, step=.05, \
|
1265 |
help="Filter the output by score (from the recognition model), and those \
|
1266 |
below this score will not be returned. (default=0.5)")
|
1267 |
-
|
1268 |
with st.expander("Choose recognition hyperparameters for " + reader_type_list[2], \
|
1269 |
expanded=False):
|
1270 |
t2_recog = st.selectbox('recog', ['ABINet','CRNN','CRNN_TPS','MASTER', \
|
@@ -1273,8 +1281,9 @@ def app():
|
|
1273 |
help='Text recognition algorithm. (default = SAR)')
|
1274 |
st.write("###### *More about text recognition models* 👉 \
|
1275 |
[here](https://mmocr.readthedocs.io/en/latest/textrecog_models.html)")
|
1276 |
-
|
1277 |
-
with st.expander("Choose recognition hyperparameters for " + reader_type_list[3], \
|
|
|
1278 |
expanded=False):
|
1279 |
t3r_psm = st.selectbox('Page segmentation mode (psm)', \
|
1280 |
[' - Default', \
|
@@ -1387,7 +1396,8 @@ def app():
|
|
1387 |
in_conf_threshold=st.session_state.conf_threshold_sld)
|
1388 |
|
1389 |
st.subheader("Recognition details")
|
1390 |
-
with st.expander("Detailed areas for EasyOCR, PPOCR, MMOCR", expanded=True):
|
|
|
1391 |
cols = st.columns(cols_size)
|
1392 |
cols[0].markdown('#### Detected area')
|
1393 |
for i in range(1, (len(reader_type_list)-1)*2, 2):
|
|
|
2 |
EasyOcr, PaddleOCR, MMOCR, Tesseract
|
3 |
"""
|
4 |
|
5 |
+
#import mim
|
6 |
+
#
|
7 |
+
#mim.install(['mmengine>=0.7.1,<1.1.0'])
|
8 |
+
#mim.install(['mmcv>=2.0.0rc4,<2.1.0'])
|
9 |
+
#mim.install(['mmdet>=3.0.rc5,<3.2.0'])
|
10 |
+
#mim.install(['mmocr'])
|
11 |
|
12 |
import streamlit as st
|
13 |
import plotly.express as px
|
|
|
21 |
import PIL
|
22 |
import easyocr
|
23 |
from paddleocr import PaddleOCR
|
24 |
+
#from mmocr.utils.ocr import MMOCR
|
25 |
import pytesseract
|
26 |
from pytesseract import Output
|
27 |
import os
|
|
|
80 |
plotly figure : confidence color scale figure
|
81 |
"""
|
82 |
# the readers considered
|
83 |
+
#out_reader_type_list = ['EasyOCR', 'PPOCR', 'MMOCR', 'Tesseract']
|
84 |
+
#out_reader_type_dict = {'EasyOCR': 0, 'PPOCR': 1, 'MMOCR': 2, 'Tesseract': 3}
|
85 |
+
out_reader_type_list = ['EasyOCR', 'PPOCR', 'Tesseract']
|
86 |
+
out_reader_type_dict = {'EasyOCR': 0, 'PPOCR': 1, 'Tesseract': 2}
|
87 |
|
88 |
# Columns for recognition details results
|
89 |
out_cols_size = [2] + [2,1]*(len(out_reader_type_list)-1) # Except Tesseract
|
|
|
125 |
'Tagalog': 'tl', 'Tamil': 'ta', 'Telugu': 'te', 'Turkish': 'tr', 'Ukranian': 'uk', \
|
126 |
'Urdu': 'ur', 'Uyghur': 'ug', 'Uzbek': 'uz', 'Vietnamese': 'vi', 'Welsh': 'cy'}
|
127 |
|
128 |
+
#out_dict_lang_mmocr = {'English & Chinese': 'en'}
|
129 |
|
130 |
out_dict_lang_tesseract = {'Afrikaans': 'afr','Albanian': 'sqi','Amharic': 'amh', \
|
131 |
'Arabic': 'ara', 'Armenian': 'hye','Assamese': 'asm','Azerbaijani - Cyrilic': 'aze_cyrl', \
|
|
|
158 |
'Uzbek - Cyrilic': 'uzb_cyrl','Uzbek': 'uzb','Vietnamese': 'vie','Welsh': 'cym', \
|
159 |
'Western Frisian': 'fry','Yiddish': 'yid','Yoruba': 'yor'}
|
160 |
|
161 |
+
out_list_dict_lang = [out_dict_lang_easyocr, out_dict_lang_ppocr, \
|
162 |
+
#out_dict_lang_mmocr, \
|
163 |
out_dict_lang_tesseract]
|
164 |
|
165 |
# Initialization of detection form
|
|
|
224 |
return out_ocr
|
225 |
|
226 |
###
|
227 |
+
#@st.experimental_memo(show_spinner=False)
|
228 |
+
#def init_mmocr(in_params):
|
229 |
+
# """Initialization of MMOCR reader
|
230 |
+
#
|
231 |
+
# Args:
|
232 |
+
# in_params (dict): dict with parameters
|
233 |
+
#
|
234 |
+
# Returns:
|
235 |
+
# mmocr reader: the ppocr reader instance
|
236 |
+
# """
|
237 |
+
# out_ocr = MMOCR(recog=None, **in_params[1])
|
238 |
+
# return out_ocr
|
239 |
|
240 |
###
|
241 |
def init_readers(in_list_params):
|
|
|
258 |
reader_ppocr = init_ppocr(in_list_params[1])
|
259 |
|
260 |
# - MMOCR
|
261 |
+
#with st.spinner("MMOCR reader initialization in progress ..."):
|
262 |
+
# reader_mmocr = init_mmocr(in_list_params[2])
|
263 |
|
264 |
+
out_list_readers = [reader_easyocr, reader_ppocr] #, reader_mmocr]
|
265 |
|
266 |
return out_list_readers
|
267 |
|
|
|
354 |
return out_ppocr_boxes_coordinates, out_status
|
355 |
|
356 |
###
|
357 |
+
#@st.experimental_memo(show_spinner=False)
|
358 |
+
#def mmocr_detect(_in_reader, in_image_path):
|
359 |
+
# """Detection with MMOCR
|
360 |
+
#
|
361 |
+
# Args:
|
362 |
+
# _in_reader (EasyORC reader) : the previously initialized instance
|
363 |
+
# in_image_path (string) : locally saved image path
|
364 |
+
# in_params (list) : list with the parameters
|
365 |
+
#
|
366 |
+
# Returns:
|
367 |
+
# list : list of the boxes coordinates
|
368 |
+
# exception on error, string 'OK' otherwise
|
369 |
+
# """
|
370 |
+
# # MMOCR detection method
|
371 |
+
# out_mmocr_boxes_coordinates = []
|
372 |
+
# try:
|
373 |
+
# det_result = _in_reader.readtext(in_image_path, details=True)
|
374 |
+
# bboxes_list = [res['boundary_result'] for res in det_result]
|
375 |
+
# for bboxes in bboxes_list:
|
376 |
+
# for bbox in bboxes:
|
377 |
+
# if len(bbox) > 9:
|
378 |
+
# min_x = min(bbox[0:-1:2])
|
379 |
+
# min_y = min(bbox[1:-1:2])
|
380 |
+
# max_x = max(bbox[0:-1:2])
|
381 |
+
# max_y = max(bbox[1:-1:2])
|
382 |
+
# #box = [min_x, min_y, max_x, min_y, max_x, max_y, min_x, max_y]
|
383 |
+
# else:
|
384 |
+
# min_x = min(bbox[0:-1:2])
|
385 |
+
# min_y = min(bbox[1::2])
|
386 |
+
# max_x = max(bbox[0:-1:2])
|
387 |
+
# max_y = max(bbox[1::2])
|
388 |
+
# box4 = [ [min_x, min_y], [max_x, min_y], [max_x, max_y], [min_x, max_y] ]
|
389 |
+
# out_mmocr_boxes_coordinates.append(box4)
|
390 |
+
# out_status = 'OK'
|
391 |
+
# except Exception as e:
|
392 |
+
# out_status = e
|
393 |
+
#
|
394 |
+
# return out_mmocr_boxes_coordinates, out_status
|
395 |
|
396 |
###
|
397 |
def cropped_1box(in_box, in_img):
|
|
|
484 |
##
|
485 |
|
486 |
## ------- MMOCR Text detection
|
487 |
+
#with st.spinner('MMOCR Text detection in progress ...'):
|
488 |
+
# mmocr_boxes_coordinates, mmocr_status = mmocr_detect(_in_list_readers[2], in_image_path)
|
489 |
+
# # Visualization
|
490 |
+
# if mmocr_boxes_coordinates:
|
491 |
+
# mmocr_image_detect = draw_detected(_in_list_images[0], mmocr_boxes_coordinates, \
|
492 |
+
# in_color, 'None', 3)
|
493 |
+
# else:
|
494 |
+
# mmocr_image_detect = mmocr_status
|
495 |
##
|
496 |
|
497 |
## ------- Tesseract Text detection
|
|
|
508 |
##
|
509 |
#
|
510 |
out_list_images = _in_list_images + [easyocr_image_detect, ppocr_image_detect, \
|
511 |
+
# mmocr_image_detect, \
|
512 |
+
tesserocr_image_detect]
|
513 |
out_list_coordinates = [easyocr_boxes_coordinates, ppocr_boxes_coordinates, \
|
514 |
+
# mmocr_boxes_coordinates, \
|
515 |
+
tesserocr_boxes_coordinates]
|
516 |
#
|
517 |
|
518 |
return out_list_images, out_list_coordinates
|
|
|
593 |
list_confidence_easyocr = []
|
594 |
list_text_ppocr = []
|
595 |
list_confidence_ppocr = []
|
596 |
+
#list_text_mmocr = []
|
597 |
+
#list_confidence_mmocr = []
|
598 |
|
599 |
# Create cropped images from detection
|
600 |
list_cropped_images = get_cropped(in_boxes_coordinates, in_image_cv)
|
|
|
612 |
##
|
613 |
|
614 |
# Recognize with MMOCR
|
615 |
+
#with st.spinner('MMOCR Text recognition in progress ...'):
|
616 |
+
# list_text_mmocr, list_confidence_mmocr, status_mmocr = \
|
617 |
+
# mmocr_recog(list_cropped_images, in_list_dict_params[2])
|
618 |
##
|
619 |
|
620 |
# Recognize with Tesseract
|
|
|
629 |
'confidence_easyocr': list_confidence_easyocr,
|
630 |
'text_ppocr': list_text_ppocr,
|
631 |
'confidence_ppocr': list_confidence_ppocr,
|
632 |
+
#'text_mmocr': list_text_mmocr,
|
633 |
+
#'confidence_mmocr': list_confidence_mmocr
|
634 |
}
|
635 |
)
|
636 |
|
637 |
+
#out_list_reco_status = [status_easyocr, status_ppocr, status_mmocr, status_tesseract]
|
638 |
+
out_list_reco_status = [status_easyocr, status_ppocr, status_tesseract]
|
639 |
|
640 |
return out_df_results, out_df_results_tesseract, out_list_reco_status
|
641 |
|
|
|
717 |
return out_list_text_ppocr, out_list_confidence_ppocr, out_status
|
718 |
|
719 |
###
|
720 |
+
#@st.experimental_memo(suppress_st_warning=True, show_spinner=False)
|
721 |
+
#def mmocr_recog(in_list_images, in_params):
|
722 |
+
# """Recognition with MMOCR
|
723 |
+
#
|
724 |
+
# Args:
|
725 |
+
# in_list_images (list) : list of cropped images
|
726 |
+
# in_params (dict) : parameters for recognition
|
727 |
+
#
|
728 |
+
# Returns:
|
729 |
+
# list : list of recognized text
|
730 |
+
# list : list of recognition confidence
|
731 |
+
# string/Exception : recognition status
|
732 |
+
# """
|
733 |
+
# ## ------- MMOCR Text recognition
|
734 |
+
# out_list_text_mmocr = []
|
735 |
+
# out_list_confidence_mmocr = []
|
736 |
+
# try:
|
737 |
+
# reader_mmocr = MMOCR(det=None, **in_params)
|
738 |
+
# step = 2*len(in_list_images) # third recognition process
|
739 |
+
# nb_steps = 4 * len(in_list_images)
|
740 |
+
# progress_bar = st.progress(step/nb_steps)
|
741 |
+
#
|
742 |
+
# for ind_img, cropped in enumerate(in_list_images):
|
743 |
+
# result = reader_mmocr.readtext(cropped, details=True)
|
744 |
+
# try:
|
745 |
+
# out_list_text_mmocr.append(result[0]['text'])
|
746 |
+
# out_list_confidence_mmocr.append(np.round(100* \
|
747 |
+
# (np.array(result[0]['score']).mean()), 1))
|
748 |
+
# except:
|
749 |
+
# out_list_text_mmocr.append('Not recognize')
|
750 |
+
# out_list_confidence_mmocr.append(100.)
|
751 |
+
# progress_bar.progress((step+ind_img+1)/nb_steps)
|
752 |
+
# out_status = 'OK'
|
753 |
+
# except Exception as e:
|
754 |
+
# out_status = e
|
755 |
+
# progress_bar.empty()
|
756 |
+
#
|
757 |
+
# return out_list_text_mmocr, out_list_confidence_mmocr, out_status
|
758 |
+
#
|
759 |
+
####
|
760 |
+
#@st.experimental_memo(suppress_st_warning=True, show_spinner=False)
|
761 |
+
#def tesserocr_recog(in_img, in_params, in_nb_images):
|
762 |
+
# """Recognition with Tesseract
|
763 |
+
#
|
764 |
+
# Args:
|
765 |
+
# in_image_cv (matrix) : original image
|
766 |
+
# in_params (dict) : parameters for recognition
|
767 |
+
# in_nb_images : nb cropped images (used for progress bar)
|
768 |
+
#
|
769 |
+
# Returns:
|
770 |
+
# Pandas data frame : recognition results
|
771 |
+
# string/Exception : recognition status
|
772 |
+
# """
|
773 |
+
# ## ------- Tesseract Text recognition
|
774 |
+
# step = 3*in_nb_images # fourth recognition process
|
775 |
+
# nb_steps = 4 * in_nb_images
|
776 |
+
# progress_bar = st.progress(step/nb_steps)
|
777 |
+
#
|
778 |
+
# try:
|
779 |
+
# out_df_result = pytesseract.image_to_data(in_img, **in_params,output_type=Output.DATAFRAME)
|
780 |
+
#
|
781 |
+
# out_df_result['box'] = out_df_result.apply(lambda d: [[d['left'], d['top']], \
|
782 |
+
# [d['left'] + d['width'], d['top']], \
|
783 |
+
# [d['left']+d['width'], d['top']+d['height']], \
|
784 |
+
# [d['left'], d['top'] + d['height']], \
|
785 |
+
# ], axis=1)
|
786 |
+
# out_df_result['cropped'] = out_df_result['box'].apply(lambda b: cropped_1box(b, in_img))
|
787 |
+
# out_df_result = out_df_result[(out_df_result.word_num > 0) & (out_df_result.text != ' ')] \
|
788 |
+
# .reset_index(drop=True)
|
789 |
+
# out_status = 'OK'
|
790 |
+
# except Exception as e:
|
791 |
+
# out_df_result = pd.DataFrame([])
|
792 |
+
# out_status = e
|
793 |
+
#
|
794 |
+
# progress_bar.progress(1.)
|
795 |
+
#
|
796 |
+
# return out_df_result, out_status
|
797 |
|
798 |
###
|
799 |
def draw_reco_images(in_image, in_boxes_coordinates, in_list_texts, in_list_confid, \
|
|
|
945 |
# Clear caches
|
946 |
easyocr_detect.clear()
|
947 |
ppocr_detect.clear()
|
948 |
+
#mmocr_detect.clear()
|
949 |
tesserocr_detect.clear()
|
950 |
process_detect.clear()
|
951 |
get_cropped.clear()
|
952 |
easyocr_recog.clear()
|
953 |
ppocr_recog.clear()
|
954 |
+
#mmocr_recog.clear()
|
955 |
tesserocr_recog.clear()
|
956 |
|
957 |
|
|
|
959 |
#print("PID : ", os.getpid())
|
960 |
|
961 |
st.title("OCR solutions comparator")
|
962 |
+
#st.markdown("##### *EasyOCR, PPOCR, Tesseract*")
|
963 |
st.markdown("##### *EasyOCR, PPOCR, MMOCR, Tesseract*")
|
964 |
#st.markdown("#### PID : " + str(os.getpid()))
|
965 |
|
|
|
976 |
easyocr_lang = list_dict_lang[0][easyocr_key_lang]
|
977 |
ppocr_key_lang = lang_col[1].selectbox(reader_type_list[1]+" :", list_dict_lang[1].keys(), 22)
|
978 |
ppocr_lang = list_dict_lang[1][ppocr_key_lang]
|
979 |
+
#mmocr_key_lang = lang_col[2].selectbox(reader_type_list[2]+" :", list_dict_lang[2].keys(), 0)
|
980 |
+
#mmocr_lang = list_dict_lang[2][mmocr_key_lang]
|
981 |
tesserocr_key_lang = lang_col[3].selectbox(reader_type_list[3]+" :", list_dict_lang[3].keys(), 35)
|
982 |
tesserocr_lang = list_dict_lang[3][tesserocr_key_lang]
|
983 |
|
|
|
1083 |
help='''slow: use polygon box to calculate bbox score, fast: use rectangle box \
|
1084 |
to calculate. (default = fast) \n
|
1085 |
Use rectlar box to calculate faster, and polygonal box more accurate for curved text area.''')
|
1086 |
+
"""
|
1087 |
with col2.expander("Choose detection hyperparameters for " + reader_type_list[2], \
|
1088 |
expanded=False):
|
1089 |
t2_det = st.selectbox('det', ['DB_r18','DB_r50','DBPP_r50','DRRG','FCE_IC15', \
|
|
|
1095 |
[here](https://mmocr.readthedocs.io/en/latest/textdet_models.html)")
|
1096 |
t2_merge_xdist = st.slider('merge_xdist', 1, 50, 20, step=1, \
|
1097 |
help='The maximum x-axis distance to merge boxes. (defaut=20)')
|
1098 |
+
"""
|
1099 |
+
#with col2.expander("Choose detection hyperparameters for " + reader_type_list[3], \
|
1100 |
+
with col2.expander("Choose detection hyperparameters for " + reader_type_list[2], \
|
1101 |
expanded=False):
|
1102 |
t3_psm = st.selectbox('Page segmentation mode (psm)', \
|
1103 |
[' - Default', \
|
|
|
1161 |
'det_east_cover_thresh': t1_det_east_cover_thresh, \
|
1162 |
'det_east_nms_thresh': t1_det_east_nms_thresh, \
|
1163 |
'det_db_score_mode': t1_det_db_score_mode}],
|
1164 |
+
#[mmocr_lang, {'det': t2_det, 'merge_xdist': t2_merge_xdist}],
|
1165 |
[tesserocr_lang, {'lang': tesserocr_lang, 'config': t3_config}]
|
1166 |
]
|
1167 |
|
|
|
1272 |
t1_drop_score = st.slider('drop_score', 0., 1., 0.25, step=.05, \
|
1273 |
help="Filter the output by score (from the recognition model), and those \
|
1274 |
below this score will not be returned. (default=0.5)")
|
1275 |
+
"""
|
1276 |
with st.expander("Choose recognition hyperparameters for " + reader_type_list[2], \
|
1277 |
expanded=False):
|
1278 |
t2_recog = st.selectbox('recog', ['ABINet','CRNN','CRNN_TPS','MASTER', \
|
|
|
1281 |
help='Text recognition algorithm. (default = SAR)')
|
1282 |
st.write("###### *More about text recognition models* 👉 \
|
1283 |
[here](https://mmocr.readthedocs.io/en/latest/textrecog_models.html)")
|
1284 |
+
"""
|
1285 |
+
#with st.expander("Choose recognition hyperparameters for " + reader_type_list[3], \
|
1286 |
+
with st.expander("Choose recognition hyperparameters for " + reader_type_list[2], \
|
1287 |
expanded=False):
|
1288 |
t3r_psm = st.selectbox('Page segmentation mode (psm)', \
|
1289 |
[' - Default', \
|
|
|
1396 |
in_conf_threshold=st.session_state.conf_threshold_sld)
|
1397 |
|
1398 |
st.subheader("Recognition details")
|
1399 |
+
#with st.expander("Detailed areas for EasyOCR, PPOCR, MMOCR", expanded=True):
|
1400 |
+
with st.expander("Detailed areas for EasyOCR, PPOCR", expanded=True):
|
1401 |
cols = st.columns(cols_size)
|
1402 |
cols[0].markdown('#### Detected area')
|
1403 |
for i in range(1, (len(reader_type_list)-1)*2, 2):
|