Spaces:
Sleeping
Sleeping
File size: 68,233 Bytes
9c06ac7 ec0a485 9c06ac7 0a84764 9c06ac7 6350480 9c06ac7 6350480 9c06ac7 ec0a485 9c06ac7 ec0a485 9c06ac7 ec0a485 9c06ac7 ec0a485 9c06ac7 0a84764 9c06ac7 0a84764 ec0a485 9c06ac7 ec0a485 0a84764 ec0a485 9c06ac7 0a84764 9c06ac7 ec0a485 9c06ac7 6c40532 ec0a485 6c40532 ec0a485 6c40532 ec0a485 6c40532 ec0a485 0a84764 9c06ac7 0a84764 9c06ac7 0a84764 9c06ac7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 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 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 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 |
"""This Streamlit app allows you to compare, from a given image, the results of different solutions:
EasyOcr, PaddleOCR, MMOCR, Tesseract
"""
import streamlit as st
import plotly.express as px
import numpy as np
import math
import pandas as pd
import cv2
from PIL import Image, ImageColor
import PIL
import easyocr
from paddleocr import PaddleOCR
from mmocr.utils.ocr import MMOCR
import pytesseract
from pytesseract import Output
import os
from mycolorpy import colorlist as mcp
###################################################################################################
## FUNCTIONS
###################################################################################################
@st.cache
def convert_df(in_df):
"""Convert data frame function, used by download button
Args:
in_df (data frame): data frame to convert
Returns:
data frame: converted data frame
"""
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return in_df.to_csv().encode('utf-8')
###
def easyocr_coord_convert(in_list_coord):
"""Convert easyocr coordinates to standard format used by others functions
Args:
in_list_coord (list of numbers): format [x_min, x_max, y_min, y_max]
Returns:
list of lists: format [ [x_min, y_min], [x_max, y_min], [x_max, y_max], [x_min, y_max] ]
"""
coord = in_list_coord
return [[coord[0], coord[2]], [coord[1], coord[2]], [coord[1], coord[3]], [coord[0], coord[3]]]
###
@st.cache(show_spinner=False)
def initializations():
"""Initializations for the app
Returns:
list of strings : list of OCR solutions names
(['EasyOCR', 'PPOCR', 'MMOCR', 'Tesseract'])
dict : names and indices of the OCR solutions
({'EasyOCR': 0, 'PPOCR': 1, 'MMOCR': 2, 'Tesseract': 3})
tuple : color of the detected boxes
list of dicts : list of languages supported by each OCR solution
list of int : columns for recognition details results
dict : confidence color scale
plotly figure : confidence color scale figure
"""
# the readers considered
out_reader_type_list = ['EasyOCR', 'PPOCR', 'MMOCR', 'Tesseract']
out_reader_type_dict = {'EasyOCR': 0, 'PPOCR': 1, 'MMOCR': 2, 'Tesseract': 3}
# Columns for recognition details results
out_cols_size = [2] + [2,1]*(len(out_reader_type_list)-1) # Except Tesseract
# Color of the detected boxes
out_color = (0, 76, 153)
# Dicts of laguages supported by each reader
out_dict_lang_easyocr = {'Abaza': 'abq', 'Adyghe': 'ady', 'Afrikaans': 'af', 'Angika': 'ang', \
'Arabic': 'ar', 'Assamese': 'as', 'Avar': 'ava', 'Azerbaijani': 'az', 'Belarusian': 'be', \
'Bulgarian': 'bg', 'Bihari': 'bh', 'Bhojpuri': 'bho', 'Bengali': 'bn', 'Bosnian': 'bs', \
'Simplified Chinese': 'ch_sim', 'Traditional Chinese': 'ch_tra', 'Chechen': 'che', \
'Czech': 'cs', 'Welsh': 'cy', 'Danish': 'da', 'Dargwa': 'dar', 'German': 'de', \
'English': 'en', 'Spanish': 'es', 'Estonian': 'et', 'Persian (Farsi)': 'fa', 'French': 'fr', \
'Irish': 'ga', 'Goan Konkani': 'gom', 'Hindi': 'hi', 'Croatian': 'hr', 'Hungarian': 'hu', \
'Indonesian': 'id', 'Ingush': 'inh', 'Icelandic': 'is', 'Italian': 'it', 'Japanese': 'ja', \
'Kabardian': 'kbd', 'Kannada': 'kn', 'Korean': 'ko', 'Kurdish': 'ku', 'Latin': 'la', \
'Lak': 'lbe', 'Lezghian': 'lez', 'Lithuanian': 'lt', 'Latvian': 'lv', 'Magahi': 'mah', \
'Maithili': 'mai', 'Maori': 'mi', 'Mongolian': 'mn', 'Marathi': 'mr', 'Malay': 'ms', \
'Maltese': 'mt', 'Nepali': 'ne', 'Newari': 'new', 'Dutch': 'nl', 'Norwegian': 'no', \
'Occitan': 'oc', 'Pali': 'pi', 'Polish': 'pl', 'Portuguese': 'pt', 'Romanian': 'ro', \
'Russian': 'ru', 'Serbian (cyrillic)': 'rs_cyrillic', 'Serbian (latin)': 'rs_latin', \
'Nagpuri': 'sck', 'Slovak': 'sk', 'Slovenian': 'sl', 'Albanian': 'sq', 'Swedish': 'sv', \
'Swahili': 'sw', 'Tamil': 'ta', 'Tabassaran': 'tab', 'Telugu': 'te', 'Thai': 'th', \
'Tajik': 'tjk', 'Tagalog': 'tl', 'Turkish': 'tr', 'Uyghur': 'ug', 'Ukranian': 'uk', \
'Urdu': 'ur', 'Uzbek': 'uz', 'Vietnamese': 'vi'}
out_dict_lang_ppocr = {'Abaza': 'abq', 'Adyghe': 'ady', 'Afrikaans': 'af', 'Albanian': 'sq', \
'Angika': 'ang', 'Arabic': 'ar', 'Avar': 'ava', 'Azerbaijani': 'az', 'Belarusian': 'be', \
'Bhojpuri': 'bho','Bihari': 'bh','Bosnian': 'bs','Bulgarian': 'bg','Chinese & English': 'ch', \
'Chinese Traditional': 'chinese_cht', 'Croatian': 'hr', 'Czech': 'cs', 'Danish': 'da', \
'Dargwa': 'dar', 'Dutch': 'nl', 'English': 'en', 'Estonian': 'et', 'French': 'fr', \
'German': 'german','Goan Konkani': 'gom','Hindi': 'hi','Hungarian': 'hu','Icelandic': 'is', \
'Indonesian': 'id', 'Ingush': 'inh', 'Irish': 'ga', 'Italian': 'it', 'Japan': 'japan', \
'Kabardian': 'kbd', 'Korean': 'korean', 'Kurdish': 'ku', 'Lak': 'lbe', 'Latvian': 'lv', \
'Lezghian': 'lez', 'Lithuanian': 'lt', 'Magahi': 'mah', 'Maithili': 'mai', 'Malay': 'ms', \
'Maltese': 'mt', 'Maori': 'mi', 'Marathi': 'mr', 'Mongolian': 'mn', 'Nagpur': 'sck', \
'Nepali': 'ne', 'Newari': 'new', 'Norwegian': 'no', 'Occitan': 'oc', 'Persian': 'fa', \
'Polish': 'pl', 'Portuguese': 'pt', 'Romanian': 'ro', 'Russia': 'ru', 'Saudi Arabia': 'sa', \
'Serbian(cyrillic)': 'rs_cyrillic', 'Serbian(latin)': 'rs_latin', 'Slovak': 'sk', \
'Slovenian': 'sl', 'Spanish': 'es', 'Swahili': 'sw', 'Swedish': 'sv', 'Tabassaran': 'tab', \
'Tagalog': 'tl', 'Tamil': 'ta', 'Telugu': 'te', 'Turkish': 'tr', 'Ukranian': 'uk', \
'Urdu': 'ur', 'Uyghur': 'ug', 'Uzbek': 'uz', 'Vietnamese': 'vi', 'Welsh': 'cy'}
out_dict_lang_mmocr = {'English & Chinese': 'en'}
out_dict_lang_tesseract = {'Afrikaans': 'afr','Albanian': 'sqi','Amharic': 'amh', \
'Arabic': 'ara', 'Armenian': 'hye','Assamese': 'asm','Azerbaijani - Cyrilic': 'aze_cyrl', \
'Azerbaijani': 'aze', 'Basque': 'eus','Belarusian': 'bel','Bengali': 'ben','Bosnian': 'bos', \
'Breton': 'bre', 'Bulgarian': 'bul','Burmese': 'mya','Catalan; Valencian': 'cat', \
'Cebuano': 'ceb', 'Central Khmer': 'khm','Cherokee': 'chr','Chinese - Simplified': 'chi_sim', \
'Chinese - Traditional': 'chi_tra','Corsican': 'cos','Croatian': 'hrv','Czech': 'ces', \
'Danish':'dan','Dutch; Flemish':'nld','Dzongkha':'dzo','English, Middle (1100-1500)':'enm', \
'English': 'eng','Esperanto': 'epo','Estonian': 'est','Faroese': 'fao', \
'Filipino (old - Tagalog)': 'fil','Finnish': 'fin','French, Middle (ca.1400-1600)': 'frm', \
'French': 'fra','Galician': 'glg','Georgian - Old': 'kat_old','Georgian': 'kat', \
'German - Fraktur': 'frk','German': 'deu','Greek, Modern (1453-)': 'ell','Gujarati': 'guj', \
'Haitian; Haitian Creole': 'hat','Hebrew': 'heb','Hindi': 'hin','Hungarian': 'hun', \
'Icelandic': 'isl','Indonesian': 'ind','Inuktitut': 'iku','Irish': 'gle', \
'Italian - Old': 'ita_old','Italian': 'ita','Japanese': 'jpn','Javanese': 'jav', \
'Kannada': 'kan','Kazakh': 'kaz','Kirghiz; Kyrgyz': 'kir','Korean (vertical)': 'kor_vert', \
'Korean': 'kor','Kurdish (Arabic Script)': 'kur_ara','Lao': 'lao','Latin': 'lat', \
'Latvian':'lav','Lithuanian':'lit','Luxembourgish':'ltz','Macedonian':'mkd','Malay':'msa', \
'Malayalam': 'mal','Maltese': 'mlt','Maori': 'mri','Marathi': 'mar','Mongolian': 'mon', \
'Nepali': 'nep','Norwegian': 'nor','Occitan (post 1500)': 'oci', \
'Orientation and script detection module':'osd','Oriya':'ori','Panjabi; Punjabi':'pan', \
'Persian':'fas','Polish':'pol','Portuguese':'por','Pushto; Pashto':'pus','Quechua':'que', \
'Romanian; Moldavian; Moldovan': 'ron','Russian': 'rus','Sanskrit': 'san', \
'Scottish Gaelic': 'gla','Serbian - Latin': 'srp_latn','Serbian': 'srp','Sindhi': 'snd', \
'Sinhala; Sinhalese': 'sin','Slovak': 'slk','Slovenian': 'slv', \
'Spanish; Castilian - Old': 'spa_old','Spanish; Castilian': 'spa','Sundanese': 'sun', \
'Swahili': 'swa','Swedish': 'swe','Syriac': 'syr','Tajik': 'tgk','Tamil': 'tam', \
'Tatar':'tat','Telugu':'tel','Thai':'tha','Tibetan':'bod','Tigrinya':'tir','Tonga':'ton', \
'Turkish': 'tur','Uighur; Uyghur': 'uig','Ukrainian': 'ukr','Urdu': 'urd', \
'Uzbek - Cyrilic': 'uzb_cyrl','Uzbek': 'uzb','Vietnamese': 'vie','Welsh': 'cym', \
'Western Frisian': 'fry','Yiddish': 'yid','Yoruba': 'yor'}
out_list_dict_lang = [out_dict_lang_easyocr, out_dict_lang_ppocr, out_dict_lang_mmocr, \
out_dict_lang_tesseract]
# Initialization of detection form
if 'columns_size' not in st.session_state:
st.session_state.columns_size = [2] + [1 for x in out_reader_type_list[1:]]
if 'column_width' not in st.session_state:
st.session_state.column_width = [500] + [400 for x in out_reader_type_list[1:]]
if 'columns_color' not in st.session_state:
st.session_state.columns_color = ["rgb(228,26,28)"] + \
["rgb(0,0,0)" for x in out_reader_type_list[1:]]
# Confidence color scale
out_list_confid = list(np.arange(0,101,1))
out_list_grad = mcp.gen_color_normalized(cmap="Greens",data_arr=np.array(out_list_confid))
out_dict_back_colors = {out_list_confid[i]: out_list_grad[i] \
for i in range(len(out_list_confid))}
list_y = [1 for i in out_list_confid]
df_confid = pd.DataFrame({'% confidence scale': out_list_confid, 'y': list_y})
out_fig = px.scatter(df_confid, x='% confidence scale', y='y', \
hover_data={'% confidence scale': True, 'y': False},
color=out_dict_back_colors.values(), range_y=[0.9,1.1], range_x=[0,100],
color_discrete_map="identity",height=50,symbol='y',symbol_sequence=['square'])
out_fig.update_xaxes(showticklabels=False)
out_fig.update_yaxes(showticklabels=False, range=[0.1, 1.1], visible=False)
out_fig.update_traces(marker_size=50)
out_fig.update_layout(paper_bgcolor="white", margin=dict(b=0,r=0,t=0,l=0), xaxis_side="top", \
showlegend=False)
return out_reader_type_list, out_reader_type_dict, out_color, out_list_dict_lang, \
out_cols_size, out_dict_back_colors, out_fig
###
@st.experimental_memo(show_spinner=False)
def init_easyocr(in_params):
"""Initialization of easyOCR reader
Args:
in_params (list): list with the language
Returns:
easyocr reader: the easyocr reader instance
"""
out_ocr = easyocr.Reader(in_params)
return out_ocr
###
@st.cache(show_spinner=False)
def init_ppocr(in_params):
"""Initialization of PPOCR reader
Args:
in_params (dict): dict with parameters
Returns:
ppocr reader: the ppocr reader instance
"""
out_ocr = PaddleOCR(lang=in_params[0], **in_params[1])
return out_ocr
###
@st.experimental_memo(show_spinner=False)
def init_mmocr(in_params):
"""Initialization of MMOCR reader
Args:
in_params (dict): dict with parameters
Returns:
mmocr reader: the ppocr reader instance
"""
out_ocr = MMOCR(recog=None, **in_params[1])
return out_ocr
###
def init_readers(in_list_params):
"""Initialization of the readers, and return them as list
Args:
in_list_params (list): list of dicts of parameters for each reader
Returns:
list: list of the reader's instances
"""
# Instantiations of the readers :
# - EasyOCR
with st.spinner("EasyOCR reader initialization in progress ..."):
reader_easyocr = init_easyocr([in_list_params[0][0]])
# - PPOCR
# Paddleocr
with st.spinner("PPOCR reader initialization in progress ..."):
reader_ppocr = init_ppocr(in_list_params[1])
# - MMOCR
with st.spinner("MMOCR reader initialization in progress ..."):
reader_mmocr = init_mmocr(in_list_params[2])
out_list_readers = [reader_easyocr, reader_ppocr, reader_mmocr]
return out_list_readers
###
@st.experimental_memo(show_spinner=False)
def load_image(in_image_file):
"""Load input file and open it
Args:
in_image_file (string or Streamlit UploadedFile): image to consider
Returns:
string : locally saved image path
PIL.Image : input file opened with Pillow
matrix : input file opened with Opencv
"""
if isinstance(in_image_file, str):
out_image_path = "img."+in_image_file.split('.')[-1]
else:
out_image_path = "img."+in_image_file.name.split('.')[-1]
img = Image.open(in_image_file)
img_saved = img.save(out_image_path)
# Read image
out_image_orig = Image.open(out_image_path)
out_image_cv2 = cv2.cvtColor(cv2.imread(out_image_path), cv2.COLOR_BGR2RGB)
return out_image_path, out_image_orig, out_image_cv2
###
@st.experimental_memo(show_spinner=False)
def easyocr_detect(_in_reader, in_image_path, in_params):
"""Detection with EasyOCR
Args:
_in_reader (EasyOCR reader) : the previously initialized instance
in_image_path (string ) : locally saved image path
in_params (list) : list with the parameters for detection
Returns:
list : list of the boxes coordinates
exception on error, string 'OK' otherwise
"""
try:
dict_param = in_params[1]
detection_result = _in_reader.detect(in_image_path,
#width_ths=0.7,
#mag_ratio=1.5
**dict_param
)
easyocr_coordinates = detection_result[0][0]
# The format of the coordinate is as follows: [x_min, x_max, y_min, y_max]
# Format boxes coordinates for draw
out_easyocr_boxes_coordinates = list(map(easyocr_coord_convert, easyocr_coordinates))
out_status = 'OK'
except Exception as e:
out_easyocr_boxes_coordinates = []
out_status = e
return out_easyocr_boxes_coordinates, out_status
###
@st.experimental_memo(show_spinner=False)
def ppocr_detect(_in_reader, in_image_path):
"""Detection with PPOCR
Args:
_in_reader (PPOCR reader) : the previously initialized instance
in_image_path (string ) : locally saved image path
Returns:
list : list of the boxes coordinates
exception on error, string 'OK' otherwise
"""
# PPOCR detection method
try:
out_ppocr_boxes_coordinates = _in_reader.ocr(in_image_path, rec=False)
out_status = 'OK'
except Exception as e:
out_ppocr_boxes_coordinates = []
out_status = e
return out_ppocr_boxes_coordinates, out_status
###
@st.experimental_memo(show_spinner=False)
def mmocr_detect(_in_reader, in_image_path):
"""Detection with MMOCR
Args:
_in_reader (EasyORC reader) : the previously initialized instance
in_image_path (string) : locally saved image path
in_params (list) : list with the parameters
Returns:
list : list of the boxes coordinates
exception on error, string 'OK' otherwise
"""
# MMOCR detection method
out_mmocr_boxes_coordinates = []
try:
det_result = _in_reader.readtext(in_image_path, details=True)
bboxes_list = [res['boundary_result'] for res in det_result]
for bboxes in bboxes_list:
for bbox in bboxes:
if len(bbox) > 9:
min_x = min(bbox[0:-1:2])
min_y = min(bbox[1:-1:2])
max_x = max(bbox[0:-1:2])
max_y = max(bbox[1:-1:2])
#box = [min_x, min_y, max_x, min_y, max_x, max_y, min_x, max_y]
else:
min_x = min(bbox[0:-1:2])
min_y = min(bbox[1::2])
max_x = max(bbox[0:-1:2])
max_y = max(bbox[1::2])
box4 = [ [min_x, min_y], [max_x, min_y], [max_x, max_y], [min_x, max_y] ]
out_mmocr_boxes_coordinates.append(box4)
out_status = 'OK'
except Exception as e:
out_status = e
return out_mmocr_boxes_coordinates, out_status
###
def cropped_1box(in_box, in_img):
"""Construction of an cropped image corresponding to an area of the initial image
Args:
in_box (list) : box with coordinates
in_img (matrix) : image
Returns:
matrix : cropped image
"""
box_ar = np.array(in_box).astype(np.int64)
x_min = box_ar[:, 0].min()
x_max = box_ar[:, 0].max()
y_min = box_ar[:, 1].min()
y_max = box_ar[:, 1].max()
out_cropped = in_img[y_min:y_max, x_min:x_max]
return out_cropped
###
@st.experimental_memo(show_spinner=False)
def tesserocr_detect(_in_img, in_params):
"""Detection with Tesseract
Args:
_in_img (PIL.Image) : image to consider
in_params (list) : list with the parameters for detection
Returns:
list : list of the boxes coordinates
exception on error, string 'OK' otherwise
"""
try:
dict_param = in_params[1]
df_res = pytesseract.image_to_data(_in_img, **dict_param, output_type=Output.DATAFRAME)
df_res['box'] = df_res.apply(lambda d: [[d['left'], d['top']], \
[d['left'] + d['width'], d['top']], \
[d['left'] + d['width'], d['top'] + d['height']], \
[d['left'], d['top'] + d['height']], \
], axis=1)
out_tesserocr_boxes_coordinates = df_res[df_res.word_num > 0]['box'].to_list()
out_status = 'OK'
except Exception as e:
out_tesserocr_boxes_coordinates = []
out_status = e
return out_tesserocr_boxes_coordinates, out_status
###
@st.experimental_memo(show_spinner=False)
def process_detect(in_image_path, _in_list_images, _in_list_readers, in_list_params, in_color):
"""Detection process for each OCR solution
Args:
in_image_path (string) : locally saved image path
_in_list_images (list) : list of original image
_in_list_readers (list) : list with previously initialized reader's instances
in_list_params (list) : list with dict parameters for each OCR solution
in_color (tuple) : color for boxes around text
Returns:
list: list of detection results images
list: list of boxes coordinates
"""
## ------- EasyOCR Text detection
with st.spinner('EasyOCR Text detection in progress ...'):
easyocr_boxes_coordinates,easyocr_status = easyocr_detect(_in_list_readers[0], \
in_image_path, in_list_params[0])
# Visualization
if easyocr_boxes_coordinates:
easyocr_image_detect = draw_detected(_in_list_images[0], easyocr_boxes_coordinates, \
in_color, 'None', 3)
else:
easyocr_boxes_coordinates = easyocr_status
##
## ------- PPOCR Text detection
with st.spinner('PPOCR Text detection in progress ...'):
ppocr_boxes_coordinates, ppocr_status = ppocr_detect(_in_list_readers[1], in_image_path)
# Visualization
if ppocr_boxes_coordinates:
ppocr_image_detect = draw_detected(_in_list_images[0], ppocr_boxes_coordinates, \
in_color, 'None', 7)
else:
ppocr_image_detect = ppocr_status
##
## ------- MMOCR Text detection
with st.spinner('MMOCR Text detection in progress ...'):
mmocr_boxes_coordinates, mmocr_status = mmocr_detect(_in_list_readers[2], in_image_path)
# Visualization
if mmocr_boxes_coordinates:
mmocr_image_detect = draw_detected(_in_list_images[0], mmocr_boxes_coordinates, \
in_color, 'None', 7)
else:
mmocr_image_detect = mmocr_status
##
## ------- Tesseract Text detection
with st.spinner('Tesseract Text detection in progress ...'):
tesserocr_boxes_coordinates, tesserocr_status = tesserocr_detect(_in_list_images[0], \
in_list_params[3])
# Visualization
if tesserocr_boxes_coordinates:
tesserocr_image_detect = draw_detected(_in_list_images[0],tesserocr_boxes_coordinates,\
in_color, 'None', 7)
else:
tesserocr_image_detect = tesserocr_status
##
#
out_list_images = _in_list_images + [easyocr_image_detect, ppocr_image_detect, \
mmocr_image_detect, tesserocr_image_detect]
out_list_coordinates = [easyocr_boxes_coordinates, ppocr_boxes_coordinates, \
mmocr_boxes_coordinates, tesserocr_boxes_coordinates]
#
return out_list_images, out_list_coordinates
###
def draw_detected(in_image, in_boxes_coordinates, in_color, posit='None', in_thickness=4):
"""Draw boxes around detected text
Args:
in_image (PIL.Image) : original image
in_boxes_coordinates (list) : boxes coordinates, from top to bottom and from left to right
[ [ [x_min, y_min], [x_max, y_min], [x_max, y_max], [x_min, y_max] ],
[ ... ]
]
in_color (tuple) : color for boxes around text
posit (str, optional) : position for text. Defaults to 'None'.
in_thickness (int, optional): thickness of the box. Defaults to 4.
Returns:
PIL.Image : original image with detected areas
"""
work_img = in_image.copy()
font = cv2.FONT_HERSHEY_SIMPLEX
for ind_box, box in enumerate(in_boxes_coordinates):
box = np.reshape(np.array(box), [-1, 1, 2]).astype(np.int64)
work_img = cv2.polylines(np.array(work_img), [box], True, in_color, in_thickness)
if posit != 'None':
if posit == 'top_left':
pos = tuple(box[0][0])
elif posit == 'top_right':
pos = tuple(box[1][0])
work_img = cv2.putText(work_img, str(ind_box+1), pos, font, 5.5, color, \
in_thickness,cv2.LINE_AA)
out_image_drawn = Image.fromarray(work_img)
return out_image_drawn
###
@st.experimental_memo(show_spinner=False)
def get_cropped(in_boxes_coordinates, in_image_cv):
"""Construct list of cropped images corresponding of the input boxes coordinates list
Args:
in_boxes_coordinates (list) : list of boxes coordinates
in_image_cv (matrix) : original image
Returns:
list : list with cropped images
"""
out_list_images = []
for box in in_boxes_coordinates:
cropped = cropped_1box(box, in_image_cv)
out_list_images.append(cropped)
return out_list_images
###
def process_recog(in_list_readers, in_image_cv, in_boxes_coordinates, in_list_dict_params):
"""Recognition process for each OCR solution
Args:
in_list_readers (list) : list with previously initialized reader's instances
in_image_cv (matrix) : original image
in_boxes_coordinates (list) : list of boxes coordinates
in_list_dict_params (list) : list with dict parameters for each OCR solution
Returns:
data frame : results for each OCR solution, except Tesseract
data frame : results for Tesseract
list : status for each recognition (exception or 'OK')
"""
out_df_results = pd.DataFrame([])
list_text_easyocr = []
list_confidence_easyocr = []
list_text_ppocr = []
list_confidence_ppocr = []
list_text_mmocr = []
list_confidence_mmocr = []
# Create cropped images from detection
list_cropped_images = get_cropped(in_boxes_coordinates, in_image_cv)
# Recognize with EasyOCR
with st.spinner('EasyOCR Text recognition in progress ...'):
list_text_easyocr, list_confidence_easyocr, status_easyocr = \
easyocr_recog(list_cropped_images, in_list_readers[0], in_list_dict_params[0])
##
# Recognize with PPOCR
with st.spinner('PPOCR Text recognition in progress ...'):
list_text_ppocr, list_confidence_ppocr, status_ppocr = \
ppocr_recog(list_cropped_images, in_list_dict_params[1])
##
# Recognize with MMOCR
with st.spinner('MMOCR Text recognition in progress ...'):
list_text_mmocr, list_confidence_mmocr, status_mmocr = \
mmocr_recog(list_cropped_images, in_list_dict_params[2])
##
# Recognize with Tesseract
with st.spinner('Tesseract Text recognition in progress ...'):
out_df_results_tesseract, status_tesseract = \
tesserocr_recog(in_image_cv, in_list_dict_params[3], len(list_cropped_images))
##
# Create results data frame
out_df_results = pd.DataFrame({'cropped_image': list_cropped_images,
'text_easyocr': list_text_easyocr,
'confidence_easyocr': list_confidence_easyocr,
'text_ppocr': list_text_ppocr,
'confidence_ppocr': list_confidence_ppocr,
'text_mmocr': list_text_mmocr,
'confidence_mmocr': list_confidence_mmocr
}
)
out_list_reco_status = [status_easyocr, status_ppocr, status_mmocr, status_tesseract]
return out_df_results, out_df_results_tesseract, out_list_reco_status
###
@st.experimental_memo(suppress_st_warning=True, show_spinner=False)
def easyocr_recog(in_list_images, _in_reader_easyocr, in_params):
"""Recognition with EasyOCR
Args:
in_list_images (list) : list of cropped images
_in_reader_easyocr (EasyOCR reader) : the previously initialized instance
in_params (dict) : parameters for recognition
Returns:
list : list of recognized text
list : list of recognition confidence
string/Exception : recognition status
"""
progress_bar = st.progress(0)
out_list_text_easyocr = []
out_list_confidence_easyocr = []
## ------- EasyOCR Text recognition
try:
step = 0*len(in_list_images) # first recognition process
nb_steps = 4 * len(in_list_images)
for ind_img, cropped in enumerate(in_list_images):
result = _in_reader_easyocr.recognize(cropped, **in_params)
try:
out_list_text_easyocr.append(result[0][1])
out_list_confidence_easyocr.append(np.round(100*result[0][2], 1))
except:
out_list_text_easyocr.append('Not recognize')
out_list_confidence_easyocr.append(100.)
progress_bar.progress((step+ind_img+1)/nb_steps)
out_status = 'OK'
except Exception as e:
out_status = e
progress_bar.empty()
return out_list_text_easyocr, out_list_confidence_easyocr, out_status
###
@st.experimental_memo(suppress_st_warning=True, show_spinner=False)
def ppocr_recog(in_list_images, in_params):
"""Recognition with PPOCR
Args:
in_list_images (list) : list of cropped images
in_params (dict) : parameters for recognition
Returns:
list : list of recognized text
list : list of recognition confidence
string/Exception : recognition status
"""
## ------- PPOCR Text recognition
out_list_text_ppocr = []
out_list_confidence_ppocr = []
try:
reader_ppocr = PaddleOCR(**in_params)
step = 1*len(in_list_images) # second recognition process
nb_steps = 4 * len(in_list_images)
progress_bar = st.progress(step/nb_steps)
for ind_img, cropped in enumerate(in_list_images):
result = reader_ppocr.ocr(cropped, det=False, cls=False)
try:
out_list_text_ppocr.append(result[0][0])
out_list_confidence_ppocr.append(np.round(100*result[0][1], 1))
except:
out_list_text_ppocr.append('Not recognize')
out_list_confidence_ppocr.append(100.)
progress_bar.progress((step+ind_img+1)/nb_steps)
out_status = 'OK'
except Exception as e:
out_status = e
progress_bar.empty()
return out_list_text_ppocr, out_list_confidence_ppocr, out_status
###
@st.experimental_memo(suppress_st_warning=True, show_spinner=False)
def mmocr_recog(in_list_images, in_params):
"""Recognition with MMOCR
Args:
in_list_images (list) : list of cropped images
in_params (dict) : parameters for recognition
Returns:
list : list of recognized text
list : list of recognition confidence
string/Exception : recognition status
"""
## ------- MMOCR Text recognition
out_list_text_mmocr = []
out_list_confidence_mmocr = []
try:
reader_mmocr = MMOCR(det=None, **in_params)
step = 2*len(in_list_images) # third recognition process
nb_steps = 4 * len(in_list_images)
progress_bar = st.progress(step/nb_steps)
for ind_img, cropped in enumerate(in_list_images):
result = reader_mmocr.readtext(cropped, details=True)
try:
out_list_text_mmocr.append(result[0]['text'])
out_list_confidence_mmocr.append(np.round(100* \
(np.array(result[0]['score']).mean()), 1))
except:
out_list_text_mmocr.append('Not recognize')
out_list_confidence_mmocr.append(100.)
progress_bar.progress((step+ind_img+1)/nb_steps)
out_status = 'OK'
except Exception as e:
out_status = e
progress_bar.empty()
return out_list_text_mmocr, out_list_confidence_mmocr, out_status
###
@st.experimental_memo(suppress_st_warning=True, show_spinner=False)
def tesserocr_recog(in_img, in_params, in_nb_images):
"""Recognition with Tesseract
Args:
in_image_cv (matrix) : original image
in_params (dict) : parameters for recognition
in_nb_images : nb cropped images (used for progress bar)
Returns:
Pandas data frame : recognition results
string/Exception : recognition status
"""
## ------- Tesseract Text recognition
step = 3*in_nb_images # fourth recognition process
nb_steps = 4 * in_nb_images
progress_bar = st.progress(step/nb_steps)
try:
out_df_result = pytesseract.image_to_data(in_img, **in_params,output_type=Output.DATAFRAME)
out_df_result['box'] = out_df_result.apply(lambda d: [[d['left'], d['top']], \
[d['left'] + d['width'], d['top']], \
[d['left']+d['width'], d['top']+d['height']], \
[d['left'], d['top'] + d['height']], \
], axis=1)
out_df_result['cropped'] = out_df_result['box'].apply(lambda b: cropped_1box(b, in_img))
out_df_result = out_df_result[(out_df_result.word_num > 0) & (out_df_result.text != ' ')] \
.reset_index(drop=True)
out_status = 'OK'
except Exception as e:
out_df_result = pd.DataFrame([])
out_status = e
progress_bar.progress(1.)
return out_df_result, out_status
###
def draw_reco_images(in_image, in_boxes_coordinates, in_list_texts, in_list_confid, \
in_dict_back_colors, in_df_results_tesseract, in_reader_type_list, \
in_font_scale=3, in_conf_threshold=65):
"""Draw recognized text on original image, for each OCR solution used
Args:
in_image (matrix) : original image
in_boxes_coordinates (list) : list of boxes coordinates
in_list_texts (list): list of recognized text for each recognizer (except Tesseract)
in_list_confid (list): list of recognition confidence for each recognizer (except Tesseract)
in_df_results_tesseract (Pandas data frame): Tesseract recognition results
in_font_scale (int, optional): text font scale. Defaults to 3.
Returns:
shows the results container
"""
img = in_image.copy()
nb_readers = len(in_reader_type_list)
list_reco_images = [img.copy() for i in range(nb_readers)]
for num, box_ in enumerate(in_boxes_coordinates):
box = np.array(box_).astype(np.int64)
# For each box : draw the results of each recognizer
for ind_r in range(nb_readers-1):
confid = np.round(in_list_confid[ind_r][num], 0)
rgb_color = ImageColor.getcolor(in_dict_back_colors[confid], "RGB")
if confid < in_conf_threshold:
text_color = (0, 0, 0)
else:
text_color = (255, 255, 255)
list_reco_images[ind_r] = cv2.rectangle(list_reco_images[ind_r], \
(box[0][0], box[0][1]), \
(box[2][0], box[2][1]), rgb_color, -1)
list_reco_images[ind_r] = cv2.putText(list_reco_images[ind_r], \
in_list_texts[ind_r][num], \
(box[0][0],int(np.round((box[0][1]+box[2][1])/2,0))), \
cv2.FONT_HERSHEY_DUPLEX, in_font_scale, text_color, 2)
# Add Tesseract process
if not in_df_results_tesseract.empty:
ind_tessocr = nb_readers-1
for num, box_ in enumerate(in_df_results_tesseract['box'].to_list()):
box = np.array(box_).astype(np.int64)
confid = np.round(in_df_results_tesseract.iloc[num]['conf'], 0)
rgb_color = ImageColor.getcolor(in_dict_back_colors[confid], "RGB")
if confid < in_conf_threshold:
text_color = (0, 0, 0)
else:
text_color = (255, 255, 255)
list_reco_images[ind_tessocr] = \
cv2.rectangle(list_reco_images[ind_tessocr], (box[0][0], box[0][1]), \
(box[2][0], box[2][1]), rgb_color, -1)
list_reco_images[ind_tessocr] = \
cv2.putText(list_reco_images[ind_tessocr], \
in_df_results_tesseract.iloc[num]['text'], \
(box[0][0],int(np.round((box[0][1]+box[2][1])/2,0))), \
cv2.FONT_HERSHEY_DUPLEX, in_font_scale, text_color, 2)
with show_reco.container():
# Draw the results, 2 images per line
reco_lines = math.ceil(len(in_reader_type_list) / 2)
column_width = 500
for ind_lig in range(0, reco_lines+1, 2):
cols = st.columns(2)
for ind_col in range(2):
ind = ind_lig + ind_col
if ind <= len(in_reader_type_list):
if in_reader_type_list[ind] == 'Tesseract':
column_title = '<p style="font-size: 20px;color:rgb(0,0,0); \
">Recognition with ' + in_reader_type_list[ind] + \
'<sp style="font-size: 17px"> (with its own detector) \
</sp></p>'
else:
column_title = '<p style="font-size: 20px;color:rgb(0,0,0); \
">Recognition with ' + \
in_reader_type_list[ind]+ '</p>'
cols[ind_col].markdown(column_title, unsafe_allow_html=True)
if st.session_state.list_reco_status[ind] == 'OK':
cols[ind_col].image(list_reco_images[ind], \
width=column_width, use_column_width=True)
else:
cols[ind_col].write(list_reco_status[ind], \
use_column_width=True)
st.markdown(' π‘ Wrong font size? you can adjust it below:')
###
def highlight():
"""Draw recognized text on original image, for each OCR solution used
Args:
in_image (matrix) : original image
in_boxes_coordinates (list) : list of boxes coordinates
in_list_texts (list): list of recognized text for each recognizer (except Tesseract)
in_list_confid (list): list of recognition confidence for each recognizer (except Tesseract)
in_df_results_tesseract (Pandas data frame): Tesseract recognition results
in_font_scale (int, optional): text font scale. Defaults to 3.
Returns:
shows the results container
"""
show_detect.empty()
with show_detect.container():
columns_size = [1 for x in reader_type_list]
column_width = [400 for x in reader_type_list]
columns_color = ["rgb(0,0,0)" for x in reader_type_list]
columns_size[reader_type_dict[st.session_state.detect_reader]] = 2
column_width[reader_type_dict[st.session_state.detect_reader]] = 500
columns_color[reader_type_dict[st.session_state.detect_reader]] = "rgb(228,26,28)"
columns = st.columns(columns_size, ) #gap='medium')
for ind_col, col in enumerate(columns):
column_title = '<p style="font-size: 20px;color:'+columns_color[ind_col] + \
';">Detection with ' + reader_type_list[ind_col]+ '</p>'
col.markdown(column_title, unsafe_allow_html=True)
if isinstance(list_images[ind_col+2], PIL.Image.Image):
col.image(list_images[ind_col+2], width=column_width[ind_col], \
use_column_width=True)
else:
col.write(list_images[ind_col+2], use_column_width=True)
st.session_state.columns_size = columns_size
st.session_state.column_width = column_width
st.session_state.columns_color = columns_color
###
@st.cache(show_spinner=False)
def get_demo():
"""Get the demo files
Returns:
PIL.Image : input file opened with Pillow
PIL.Image : input file opened with Pillow
"""
out_img_demo_1 = Image.open("img_demo_1.jpg")
out_img_demo_2 = Image.open("img_demo_2.jpg")
return out_img_demo_1, out_img_demo_2
###################################################################################################
## MAIN
###################################################################################################
##----------- Initializations ---------------------------------------------------------------------
#print("PID : ", os.getpid())
st.set_page_config(page_title='OCR Comparator', layout ="wide")
st.markdown(f'''
<style>
section[data-testid="stSidebar"] .css-ng1t4o {{width: 14rem;}}
section[data-testid="stSidebar"] .css-1d391kg {{width: 14rem;}}
</style>
''',unsafe_allow_html=True)
st.title("OCR solutions comparator")
st.markdown("##### *EasyOCR, PPOCR, MMOCR, Tesseract*")
#st.markdown("#### PID : " + str(os.getpid()))
# Initializations
with st.spinner("Initializations in progress ..."):
reader_type_list, reader_type_dict, color, list_dict_lang, \
cols_size, dict_back_colors, fig_colorscale = initializations()
img_demo_1, img_demo_2 = get_demo()
##----------- Choose language & image -------------------------------------------------------------
st.markdown("#### Choose languages for the text recognition:")
lang_col = st.columns(4)
easyocr_key_lang = lang_col[0].selectbox(reader_type_list[0]+" :", list_dict_lang[0].keys(), 26)
easyocr_lang = list_dict_lang[0][easyocr_key_lang]
ppocr_key_lang = lang_col[1].selectbox(reader_type_list[1]+" :", list_dict_lang[1].keys(), 22)
ppocr_lang = list_dict_lang[1][ppocr_key_lang]
mmocr_key_lang = lang_col[2].selectbox(reader_type_list[2]+" :", list_dict_lang[2].keys(), 0)
mmocr_lang = list_dict_lang[2][mmocr_key_lang]
tesserocr_key_lang = lang_col[3].selectbox(reader_type_list[3]+" :", list_dict_lang[3].keys(), 35)
tesserocr_lang = list_dict_lang[3][tesserocr_key_lang]
st.markdown("#### Choose picture:")
cols_pict = st.columns([1, 1, 2, 2])
img_typ = cols_pict[0].radio("", ['Upload file', 'Take a picture', 'Use a demo file'], index=0)
if img_typ == 'Upload file':
image_file = cols_pict[2].file_uploader("Upload a file:", type=["png","jpg","jpeg"])
if img_typ == 'Take a picture':
image_file = cols_pict[2].camera_input("Take a picture:")
if img_typ == 'Use a demo file':
cols_pict[1].markdown('###### Choose a demo file:')
demo_used = cols_pict[1].radio('', ['File 1', 'File 2'], index=0)
cols_pict[2].markdown('###### File 1')
cols_pict[2].image(img_demo_1, use_column_width=True)
cols_pict[3].markdown('###### File 2')
cols_pict[3].image(img_demo_2, use_column_width=True)
if demo_used == 'File 1':
image_file = 'img_demo_1.jpg'
else:
image_file = 'img_demo_2.jpg'
##----------- Process input image -----------------------------------------------------------------
if image_file is not None:
image_path, image_orig, image_cv2 = load_image(image_file)
list_images = [image_orig, image_cv2]
##----------- Form with original image & hyperparameters for detectors ----------------------------
with st.form("form1"):
col1, col2 = st.columns(2, ) #gap="medium")
col1.markdown("##### Original image")
col1.image(list_images[0], width=500, use_column_width=True)
col2.markdown("##### Hyperparameters values for detection")
with col2.expander("Choose detection hyperparameters for " + reader_type_list[0], \
expanded=False):
t0_min_size = st.slider("min_size", 1, 20, 10, step=1, \
help="min_size (int, default = 10) - Filter text box smaller than \
minimum value in pixel")
t0_text_threshold = st.slider("text_threshold", 0.1, 1., 0.7, step=0.1, \
help="text_threshold (float, default = 0.7) - Text confidence threshold")
t0_low_text = st.slider("low_text", 0.1, 1., 0.4, step=0.1, \
help="low_text (float, default = 0.4) - Text low-bound score")
t0_link_threshold = st.slider("link_threshold", 0.1, 1., 0.4, step=0.1, \
help="link_threshold (float, default = 0.4) - Link confidence threshold")
t0_canvas_size = st.slider("canvas_size", 2000, 5000, 2560, step=10, \
help='''canvas_size (int, default = 2560) \n
Maximum e size. Image bigger than this value will be resized down''')
t0_mag_ratio = st.slider("mag_ratio", 0.1, 5., 1., step=0.1, \
help="mag_ratio (float, default = 1) - Image magnification ratio")
t0_slope_ths = st.slider("slope_ths", 0.01, 1., 0.1, step=0.01, \
help='''slope_ths (float, default = 0.1) - Maximum slope \
(delta y/delta x) to considered merging. \n
Low valuans tiled boxes will not be merged.''')
t0_ycenter_ths = st.slider("ycenter_ths", 0.1, 1., 0.5, step=0.1, \
help='''ycenter_ths (float, default = 0.5) - Maximum shift in y direction. \n
Boxes wiifferent level should not be merged.''')
t0_height_ths = st.slider("height_ths", 0.1, 1., 0.5, step=0.1, \
help='''height_ths (float, default = 0.5) - Maximum different in box height. \n
Boxes wiery different text size should not be merged.''')
t0_width_ths = st.slider("width_ths", 0.1, 1., 0.5, step=0.1, \
help="width_ths (float, default = 0.5) - Maximum horizontal \
distance to merge boxes.")
t0_add_margin = st.slider("add_margin", 0.1, 1., 0.1, step=0.1, \
help='''add_margin (float, default = 0.1) - \
Extend bounding boxes in all direction by certain value. \n
This is rtant for language with complex script (E.g. Thai).''')
t0_optimal_num_chars = st.slider("optimal_num_chars", None, 100, None, step=10, \
help="optimal_num_chars (int, default = None) - If specified, bounding boxes \
with estimated number of characters near this value are returned first.")
with col2.expander("Choose detection hyperparameters for " + reader_type_list[1], \
expanded=False):
t1_det_algorithm = st.selectbox('det_algorithm', ['DB'], \
help='Type of detection algorithm selected. (default = DB)')
t1_det_max_side_len = st.slider('det_max_side_len', 500, 2000, 960, step=10, \
help='''The maximum size of the long side of the image. (default = 960)\n
Limit thximum image height and width.\n
When theg side exceeds this value, the long side will be resized to this size, and the short side \
will be ed proportionally.''')
t1_det_db_thresh = st.slider('det_db_thresh', 0.1, 1., 0.3, step=0.1, \
help='''Binarization threshold value of DB output map. (default = 0.3) \n
Used to er the binarized image of DB prediction, setting 0.-0.3 has no obvious effect on the result.''')
t1_det_db_box_thresh = st.slider('det_db_box_thresh', 0.1, 1., 0.6, step=0.1, \
help='''The threshold value of the DB output box. (default = 0.6) \n
DB post-essing filter box threshold, if there is a missing box detected, it can be reduced as appropriate. \n
Boxes sclower than this value will be discard.''')
t1_det_db_unclip_ratio = st.slider('det_db_unclip_ratio', 1., 3.0, 1.6, step=0.1, \
help='''The expanded ratio of DB output box. (default = 1.6) \n
Indicatee compactness of the text box, the smaller the value, the closer the text box to the text.''')
t1_det_east_score_thresh = st.slider('det_east_cover_thresh', 0.1, 1., 0.8, step=0.1, \
help="Binarization threshold value of EAST output map. (default = 0.8)")
t1_det_east_cover_thresh = st.slider('det_east_cover_thresh', 0.1, 1., 0.1, step=0.1, \
help='''The threshold value of the EAST output box. (default = 0.1) \n
Boxes sclower than this value will be discarded.''')
t1_det_east_nms_thresh = st.slider('det_east_nms_thresh', 0.1, 1., 0.2, step=0.1, \
help="The NMS threshold value of EAST model output box. (default = 0.2)")
t1_det_db_score_mode = st.selectbox('det_db_score_mode', ['fast', 'slow'], \
help='''slow: use polygon box to calculate bbox score, fast: use rectangle box \
to calculate. (default = fast) \n
Use rectlar box to calculate faster, and polygonal box more accurate for curved text area.''')
with col2.expander("Choose detection hyperparameters for " + reader_type_list[2], \
expanded=False):
t2_det = st.selectbox('det', ['DB_r18','DB_r50','DBPP_r50','DRRG','FCE_IC15', \
'FCE_CTW_DCNv2','MaskRCNN_CTW','MaskRCNN_IC15', \
'MaskRCNN_IC17', 'PANet_CTW','PANet_IC15','PS_CTW',\
'PS_IC15','Tesseract','TextSnake'], 10, \
help='Text detection algorithm. (default = PANet_IC15)')
st.write("###### *More about text detection models* π \
[here](https://mmocr.readthedocs.io/en/latest/textdet_models.html)")
t2_merge_xdist = st.slider('merge_xdist', 1, 50, 20, step=1, \
help='The maximum x-axis distance to merge boxes. (defaut=20)')
with col2.expander("Choose detection hyperparameters for " + reader_type_list[3], \
expanded=False):
t3_psm = st.selectbox('Page segmentation mode (psm)', \
[' - Default', \
' 4 Assume a single column of text of variable sizes', \
' 5 Assume a single uniform block of vertically aligned text', \
' 6 Assume a single uniform block of text', \
' 7 Treat the image as a single text line', \
' 8 Treat the image as a single word', \
' 9 Treat the image as a single word in a circle', \
'10 Treat the image as a single character', \
'11 Sparse text. Find as much text as possible in no \
particular order', \
'13 Raw line. Treat the image as a single text line, \
bypassing hacks that are Tesseract-specific'])
t3_oem = st.selectbox('OCR engine mode', ['0 Legacy engine only', \
'1 Neural nets LSTM engine only', \
'2 Legacy + LSTM engines', \
'3 Default, based on what is available'], 3)
t3_whitelist = st.text_input('Limit tesseract to recognize only this characters :', \
placeholder='Limit tesseract to recognize only this characters', \
help='Example for numbers only : 0123456789')
submit_detect = st.form_submit_button("Launch detection")
##----------- Process text detection --------------------------------------------------------------
if submit_detect:
# Process text detection
if t0_optimal_num_chars == 0:
t0_optimal_num_chars = None
# Construct the config Tesseract parameter
t3_config = ''
psm = t3_psm[:2]
if psm != ' -':
t3_config += '--psm ' + psm.strip()
oem = t3_oem[:1]
if oem != '3':
t3_config += ' --oem ' + oem
if t3_whitelist != '':
t3_config += ' -c tessedit_char_whitelist=' + t3_whitelist
list_params_det = \
[[easyocr_lang, \
{'min_size': t0_min_size, 'text_threshold': t0_text_threshold, \
'low_text': t0_low_text, 'link_threshold': t0_link_threshold, \
'canvas_size': t0_canvas_size, 'mag_ratio': t0_mag_ratio, \
'slope_ths': t0_slope_ths, 'ycenter_ths': t0_ycenter_ths, \
'height_ths': t0_height_ths, 'width_ths': t0_width_ths, \
'add_margin': t0_add_margin, 'optimal_num_chars': t0_optimal_num_chars \
}], \
[ppocr_lang, \
{'det_algorithm': t1_det_algorithm, 'det_max_side_len': t1_det_max_side_len, \
'det_db_thresh': t1_det_db_thresh, 'det_db_box_thresh': t1_det_db_box_thresh, \
'det_db_unclip_ratio': t1_det_db_unclip_ratio, \
'det_east_score_thresh': t1_det_east_score_thresh, \
'det_east_cover_thresh': t1_det_east_cover_thresh, \
'det_east_nms_thresh': t1_det_east_nms_thresh, \
'det_db_score_mode': t1_det_db_score_mode}],
[mmocr_lang, {'det': t2_det, 'merge_xdist': t2_merge_xdist}],
[tesserocr_lang, {'lang': tesserocr_lang, 'config': t3_config}]
]
show_info1 = st.empty()
show_info1.info("Readers initializations in progress (it may take a while) ...")
list_readers = init_readers(list_params_det)
show_info1.info("Text detection in progress ...")
list_images, list_coordinates = process_detect(image_path, list_images, list_readers, \
list_params_det, color)
show_info1.empty()
if 'list_readers' not in st.session_state:
st.session_state.list_readers = list_readers
if 'list_coordinates' not in st.session_state:
st.session_state.list_coordinates = list_coordinates
if 'list_images' not in st.session_state:
st.session_state.list_images = list_images
if 'list_params_det' not in st.session_state:
st.session_state.list_params_det = list_params_det
if 'columns_size' not in st.session_state:
st.session_state.columns_size = [2] + [1 for x in reader_type_list[1:]]
if 'column_width' not in st.session_state:
st.session_state.column_width = [500] + [400 for x in reader_type_list[1:]]
if 'columns_color' not in st.session_state:
st.session_state.columns_color = ["rgb(228,26,28)"] + \
["rgb(0,0,0)" for x in reader_type_list[1:]]
if 'list_coordinates' in st.session_state:
list_coordinates = st.session_state.list_coordinates
list_images = st.session_state.list_images
list_readers = st.session_state.list_readers
list_params_det = st.session_state.list_params_det
##----------- Text detection results --------------------------------------------------------------
st.subheader("Text detection")
show_detect = st.empty()
list_ok_detect = []
with show_detect.container():
columns = st.columns(st.session_state.columns_size, ) #gap='medium')
for no_col, col in enumerate(columns):
column_title = '<p style="font-size: 20px;color:' + \
st.session_state.columns_color[no_col] + \
';">Detection with ' + reader_type_list[no_col]+ '</p>'
col.markdown(column_title, unsafe_allow_html=True)
if isinstance(list_images[no_col+2], PIL.Image.Image):
col.image(list_images[no_col+2], width=st.session_state.column_width[no_col], \
use_column_width=True)
list_ok_detect.append(reader_type_list[no_col])
else:
col.write(list_images[no_col+2], use_column_width=True)
st.subheader("Text recognition")
st.markdown("##### Using detection performed above by:")
st.radio('Choose the detecter:', list_ok_detect, key='detect_reader', \
horizontal=True, on_change=highlight)
##----------- Form with hyperparameters for recognition -----------------------
st.markdown("##### Hyperparameters values for recognition:")
with st.form("form2"):
with st.expander("Choose recognition hyperparameters for " + reader_type_list[0], \
expanded=False):
t0_decoder = st.selectbox('decoder', ['greedy', 'beamsearch', 'wordbeamsearch'], \
help="decoder (string, default = 'greedy') - options are 'greedy', \
'beamsearch' and 'wordbeamsearch.")
t0_beamWidth = st.slider('beamWidth', 2, 20, 5, step=1, \
help="beamWidth (int, default = 5) - How many beam to keep when decoder = \
'beamsearch' or 'wordbeamsearch'.")
t0_batch_size = st.slider('batch_size', 1, 10, 1, step=1, \
help="batch_size (int, default = 1) - batch_size>1 will make EasyOCR faster \
but use more memory.")
t0_workers = st.slider('workers', 0, 10, 0, step=1, \
help="workers (int, default = 0) - Number thread used in of dataloader.")
t0_allowlist = st.text_input('allowlist', value="", max_chars=None, \
placeholder='Force EasyOCR to recognize only this subset of characters', \
help='''allowlist (string) - Force EasyOCR to recognize only subset of characters.\n
Usefor specific problem (E.g. license plate, etc.)''')
t0_blocklist = st.text_input('blocklist', value="", max_chars=None, \
placeholder='Block subset of character (will be ignored if allowlist is given)', \
help='''blocklist (string) - Block subset of character. This argument will be \
ignored if allowlist is given.''')
t0_detail = st.radio('detail', [0, 1], 1, horizontal=True, \
help="detail (int, default = 1) - Set this to 0 for simple output")
t0_paragraph = st.radio('paragraph', [True, False], 1, horizontal=True, \
help='paragraph (bool, default = False) - Combine result into paragraph')
t0_contrast_ths = st.slider('contrast_ths', 0.05, 1., 0.1, step=0.01, \
help='''contrast_ths (float, default = 0.1) - Text box with contrast lower than \
this value will be passed into model 2 times.\n
Firs with original image and second with contrast adjusted to 'adjust_contrast' value.\n
The with more confident level will be returned as a result.''')
t0_adjust_contrast = st.slider('adjust_contrast', 0.1, 1., 0.5, step=0.1, \
help = 'adjust_contrast (float, default = 0.5) - target contrast level for low \
contrast text box')
with st.expander("Choose recognition hyperparameters for " + reader_type_list[1], \
expanded=False):
t1_rec_algorithm = st.selectbox('rec_algorithm', ['CRNN', 'SVTR_LCNet'], 0, \
help="Type of recognition algorithm selected. (default=CRNN)")
t1_rec_batch_num = st.slider('rec_batch_num', 1, 50, step=1, \
help="When performing recognition, the batchsize of forward images. \
(default=30)")
t1_max_text_length = st.slider('max_text_length', 3, 250, 25, step=1, \
help="The maximum text length that the recognition algorithm can recognize. \
(default=25)")
t1_use_space_char = st.radio('use_space_char', [True, False], 0, horizontal=True, \
help="Whether to recognize spaces. (default=TRUE)")
t1_drop_score = st.slider('drop_score', 0., 1., 0.25, step=.05, \
help="Filter the output by score (from the recognition model), and those \
below this score will not be returned. (default=0.5)")
with st.expander("Choose recognition hyperparameters for " + reader_type_list[2], \
expanded=False):
t2_recog = st.selectbox('recog', ['ABINet','CRNN','CRNN_TPS','MASTER', \
'NRTR_1/16-1/8','NRTR_1/8-1/4','RobustScanner','SAR','SAR_CN', \
'SATRN','SATRN_sm','SEG','Tesseract'], 7, \
help='Text recognition algorithm. (default = SAR)')
st.write("###### *More about text recognition models* π \
[here](https://mmocr.readthedocs.io/en/latest/textrecog_models.html)")
with st.expander("Choose recognition hyperparameters for " + reader_type_list[3], \
expanded=False):
t3r_psm = st.selectbox('Page segmentation mode (psm)', \
[' - Default', \
' 4 Assume a single column of text of variable sizes', \
' 5 Assume a single uniform block of vertically aligned \
text', \
' 6 Assume a single uniform block of text', \
' 7 Treat the image as a single text line', \
' 8 Treat the image as a single word', \
' 9 Treat the image as a single word in a circle', \
'10 Treat the image as a single character', \
'11 Sparse text. Find as much text as possible in no \
particular order', \
'13 Raw line. Treat the image as a single text line, \
bypassing hacks that are Tesseract-specific'])
t3r_oem = st.selectbox('OCR engine mode', ['0 Legacy engine only', \
'1 Neural nets LSTM engine only', \
'2 Legacy + LSTM engines', \
'3 Default, based on what is available'], 3)
t3r_whitelist = st.text_input('Limit tesseract to recognize only this \
characters :', \
placeholder='Limit tesseract to recognize only this characters', \
help='Example for numbers only : 0123456789')
submit_reco = st.form_submit_button("Launch recognition")
if submit_reco:
process_detect.clear()
##----------- Process recognition ------------------------------------------
reader_ind = reader_type_dict[st.session_state.detect_reader]
list_boxes = list_coordinates[reader_ind]
# Construct the config Tesseract parameter
t3r_config = ''
psm = t3r_psm[:2]
if psm != ' -':
t3r_config += '--psm ' + psm.strip()
oem = t3r_oem[:1]
if oem != '3':
t3r_config += ' --oem ' + oem
if t3r_whitelist != '':
t3r_config += ' -c tessedit_char_whitelist=' + t3r_whitelist
list_params_rec = \
[{'decoder': t0_decoder, 'beamWidth': t0_beamWidth, \
'batch_size': t0_batch_size, 'workers': t0_workers, \
'allowlist': t0_allowlist, 'blocklist': t0_blocklist, \
'detail': t0_detail, 'paragraph': t0_paragraph, \
'contrast_ths': t0_contrast_ths, 'adjust_contrast': t0_adjust_contrast
},
{ **list_params_det[1][1], **{'rec_algorithm': t1_rec_algorithm, \
'rec_batch_num': t1_rec_batch_num, 'max_text_length': t1_max_text_length, \
'use_space_char': t1_use_space_char, 'drop_score': t1_drop_score}, \
**{'lang': list_params_det[1][0]}
},
{'recog': t2_recog},
{'lang': tesserocr_lang, 'config': t3r_config}
]
show_info2 = st.empty()
with show_info2.container():
st.info("Text recognition in progress ...")
df_results, df_results_tesseract, list_reco_status = \
process_recog(list_readers, list_images[1], list_boxes, list_params_rec)
show_info2.empty()
st.session_state.df_results = df_results
st.session_state.list_boxes = list_boxes
st.session_state.df_results_tesseract = df_results_tesseract
st.session_state.list_reco_status = list_reco_status
if 'df_results' in st.session_state:
##----------- Show recognition results ------------------------------------------------------------
results_cols = st.session_state.df_results.columns
list_col_text = np.arange(1, len(cols_size), 2)
list_col_confid = np.arange(2, len(cols_size), 2)
dict_draw_reco = {'in_image': st.session_state.list_images[1], \
'in_boxes_coordinates': st.session_state.list_boxes, \
'in_list_texts': [st.session_state.df_results[x].to_list() \
for x in results_cols[list_col_text]], \
'in_list_confid': [st.session_state.df_results[x].to_list() \
for x in results_cols[list_col_confid]], \
'in_dict_back_colors': dict_back_colors, \
'in_df_results_tesseract' : st.session_state.df_results_tesseract, \
'in_reader_type_list': reader_type_list
}
show_reco = st.empty()
with st.form("form3"):
st.plotly_chart(fig_colorscale, use_container_width=True)
col_font, col_threshold = st.columns(2)
col_font.slider('Font scale', 1, 7, 4, step=1, key="font_scale_sld")
col_threshold.slider('% confidence threshold for text color change', 40, 100, 64, \
step=1, key="conf_threshold_sld")
col_threshold.write("(text color is black below this % confidence threshold, \
and white above)")
draw_reco_images(**dict_draw_reco)
submit_resize = st.form_submit_button("Refresh")
if submit_resize:
draw_reco_images(**dict_draw_reco, \
in_font_scale=st.session_state.font_scale_sld, \
in_conf_threshold=st.session_state.conf_threshold_sld)
st.subheader("Recognition details")
with st.expander("Detailed areas for EasyOCR, PPOCR, MMOCR", expanded=True):
cols = st.columns(cols_size)
cols[0].markdown('#### Detected area')
for i in range(1, (len(reader_type_list)-1)*2, 2):
cols[i].markdown('#### with ' + reader_type_list[i//2])
for row in st.session_state.df_results.itertuples():
#cols = st.columns(1 + len(reader_type_list)*2)
cols = st.columns(cols_size)
cols[0].image(row.cropped_image, width=150)
for ind_col in range(1, len(cols), 2):
cols[ind_col].write(getattr(row, results_cols[ind_col]))
cols[ind_col+1].write("("+str( \
getattr(row, results_cols[ind_col+1]))+"%)")
st.download_button(
label="Download results as CSV file",
data=convert_df(st.session_state.df_results),
file_name='OCR_comparator_results.csv',
mime='text/csv',
)
if not st.session_state.df_results_tesseract.empty:
with st.expander("Detailed areas for Tesseract", expanded=False):
cols = st.columns([2,2,1])
cols[0].markdown('#### Detected area')
cols[1].markdown('#### with Tesseract')
for row in st.session_state.df_results_tesseract.itertuples():
cols = st.columns([2,2,1])
cols[0].image(row.cropped, width=150)
cols[1].write(getattr(row, 'text'))
cols[2].write("("+str(getattr(row, 'conf'))+"%)")
st.download_button(
label="Download Tesseract results as CSV file",
data=convert_df(st.session_state.df_results),
file_name='OCR_comparator_Tesseract_results.csv',
mime='text/csv',
)
|