Spaces:
Sleeping
Sleeping
Add different choices for input image
Browse files- img_demo.jpg → img_demo_1.jpg +0 -0
- img_demo_2.png +0 -0
- pages/App.py +40 -20
img_demo.jpg → img_demo_1.jpg
RENAMED
|
File without changes
|
img_demo_2.png
ADDED
|
pages/App.py
CHANGED
|
@@ -49,6 +49,7 @@ def easyocr_coord_convert(in_list_coord):
|
|
| 49 |
coord = in_list_coord
|
| 50 |
return [[coord[0], coord[2]], [coord[1], coord[2]], [coord[1], coord[3]], [coord[0], coord[3]]]
|
| 51 |
|
|
|
|
| 52 |
@st.cache(show_spinner=False)
|
| 53 |
def initializations():
|
| 54 |
"""Initializations for the app
|
|
@@ -194,7 +195,6 @@ def init_easyocr(in_params):
|
|
| 194 |
|
| 195 |
###
|
| 196 |
@st.cache(show_spinner=False)
|
| 197 |
-
#@st.experimental_memo(show_spinner=False)
|
| 198 |
def init_ppocr(in_params):
|
| 199 |
"""Initialization of PPOCR reader
|
| 200 |
|
|
@@ -250,7 +250,6 @@ def init_readers(in_list_params):
|
|
| 250 |
return out_list_readers
|
| 251 |
|
| 252 |
###
|
| 253 |
-
#@st.cache(show_spinner=False)
|
| 254 |
@st.experimental_memo(show_spinner=False)
|
| 255 |
def load_image(in_image_file):
|
| 256 |
"""Load input file and open it
|
|
@@ -277,7 +276,6 @@ def load_image(in_image_file):
|
|
| 277 |
return out_image_path, out_image_orig, out_image_cv2
|
| 278 |
|
| 279 |
###
|
| 280 |
-
#@st.cache(show_spinner=False)
|
| 281 |
@st.experimental_memo(show_spinner=False)
|
| 282 |
def easyocr_detect(_in_reader, in_image_path, in_params):
|
| 283 |
"""Detection with EasyOCR
|
|
@@ -311,7 +309,6 @@ def easyocr_detect(_in_reader, in_image_path, in_params):
|
|
| 311 |
return out_easyocr_boxes_coordinates, out_status
|
| 312 |
|
| 313 |
###
|
| 314 |
-
#@st.cache(show_spinner=False)
|
| 315 |
@st.experimental_memo(show_spinner=False)
|
| 316 |
def ppocr_detect(_in_reader, in_image_path):
|
| 317 |
"""Detection with PPOCR
|
|
@@ -335,8 +332,6 @@ def ppocr_detect(_in_reader, in_image_path):
|
|
| 335 |
return out_ppocr_boxes_coordinates, out_status
|
| 336 |
|
| 337 |
###
|
| 338 |
-
#@st.cache(show_spinner=False, hash_funcs={torch.nn.parameter.Parameter: lambda _: None})
|
| 339 |
-
#@st.cache(show_spinner=False)
|
| 340 |
@st.experimental_memo(show_spinner=False)
|
| 341 |
def mmocr_detect(_in_reader, in_image_path):
|
| 342 |
"""Detection with MMOCR
|
|
@@ -529,8 +524,7 @@ def draw_detected(in_image, in_boxes_coordinates, in_color, posit='None', in_thi
|
|
| 529 |
|
| 530 |
return out_image_drawn
|
| 531 |
|
| 532 |
-
|
| 533 |
-
#@st.cache(show_spinner=False)
|
| 534 |
@st.experimental_memo(show_spinner=False)
|
| 535 |
def get_cropped(in_boxes_coordinates, in_image_cv):
|
| 536 |
"""Construct list of cropped images corresponding of the input boxes coordinates list
|
|
@@ -652,7 +646,7 @@ def easyocr_recog(in_list_images, _in_reader_easyocr, in_params):
|
|
| 652 |
|
| 653 |
return out_list_text_easyocr, out_list_confidence_easyocr, out_status
|
| 654 |
|
| 655 |
-
|
| 656 |
@st.experimental_memo(suppress_st_warning=True, show_spinner=False)
|
| 657 |
def ppocr_recog(in_list_images, in_params):
|
| 658 |
"""Recognition with PPOCR
|
|
@@ -691,7 +685,7 @@ def ppocr_recog(in_list_images, in_params):
|
|
| 691 |
|
| 692 |
return out_list_text_ppocr, out_list_confidence_ppocr, out_status
|
| 693 |
|
| 694 |
-
|
| 695 |
@st.experimental_memo(suppress_st_warning=True, show_spinner=False)
|
| 696 |
def mmocr_recog(in_list_images, in_params):
|
| 697 |
"""Recognition with MMOCR
|
|
@@ -731,8 +725,7 @@ def mmocr_recog(in_list_images, in_params):
|
|
| 731 |
|
| 732 |
return out_list_text_mmocr, out_list_confidence_mmocr, out_status
|
| 733 |
|
| 734 |
-
|
| 735 |
-
#@st.cache(show_spinner=False)
|
| 736 |
@st.experimental_memo(suppress_st_warning=True, show_spinner=False)
|
| 737 |
def tesserocr_recog(in_img, in_params, in_nb_images):
|
| 738 |
"""Recognition with Tesseract
|
|
@@ -859,7 +852,7 @@ def draw_reco_images(in_image, in_boxes_coordinates, in_list_texts, in_list_conf
|
|
| 859 |
cols[ind_col].write(list_reco_status[ind], \
|
| 860 |
use_column_width=True)
|
| 861 |
|
| 862 |
-
|
| 863 |
def highlight():
|
| 864 |
"""Draw recognized text on original image, for each OCR solution used
|
| 865 |
|
|
@@ -897,6 +890,21 @@ def highlight():
|
|
| 897 |
st.session_state.column_width = column_width
|
| 898 |
st.session_state.columns_color = columns_color
|
| 899 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 900 |
###################################################################################################
|
| 901 |
## MAIN
|
| 902 |
###################################################################################################
|
|
@@ -912,6 +920,7 @@ st.markdown("##### *EasyOCR, PPOCR, MMOCR, Tesseract*")
|
|
| 912 |
with st.spinner("Initializations in progress ..."):
|
| 913 |
reader_type_list, reader_type_dict, color, list_dict_lang, \
|
| 914 |
cols_size, dict_back_colors, fig_colorscale = initializations()
|
|
|
|
| 915 |
|
| 916 |
##----------- Choose language & image -------------------------------------------------------------
|
| 917 |
st.markdown("#### Choose languages for the text recognition:")
|
|
@@ -924,13 +933,24 @@ mmocr_key_lang = lang_col[2].selectbox(reader_type_list[2]+" :", list_dict_lang[
|
|
| 924 |
mmocr_lang = list_dict_lang[2][mmocr_key_lang]
|
| 925 |
tesserocr_key_lang = lang_col[3].selectbox(reader_type_list[3]+" :", list_dict_lang[3].keys(), 35)
|
| 926 |
tesserocr_lang = list_dict_lang[3][tesserocr_key_lang]
|
| 927 |
-
st.markdown("#### Upload image:")
|
| 928 |
|
| 929 |
-
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 934 |
|
| 935 |
##----------- Process input image -----------------------------------------------------------------
|
| 936 |
if image_file is not None:
|
|
@@ -1146,8 +1166,8 @@ Use rectlar box to calculate faster, and polygonal box more accurate for curved
|
|
| 1146 |
horizontal=True, on_change=highlight)
|
| 1147 |
|
| 1148 |
##----------- Form with hyperparameters for recognition -----------------------
|
|
|
|
| 1149 |
with st.form("form2"):
|
| 1150 |
-
st.markdown("##### Hyperparameters values for recognition")
|
| 1151 |
with st.expander("Choose recognition hyperparameters for " + reader_type_list[0], \
|
| 1152 |
expanded=False):
|
| 1153 |
t0_decoder = st.selectbox('decoder', ['greedy', 'beamsearch', 'wordbeamsearch'], \
|
|
|
|
| 49 |
coord = in_list_coord
|
| 50 |
return [[coord[0], coord[2]], [coord[1], coord[2]], [coord[1], coord[3]], [coord[0], coord[3]]]
|
| 51 |
|
| 52 |
+
###
|
| 53 |
@st.cache(show_spinner=False)
|
| 54 |
def initializations():
|
| 55 |
"""Initializations for the app
|
|
|
|
| 195 |
|
| 196 |
###
|
| 197 |
@st.cache(show_spinner=False)
|
|
|
|
| 198 |
def init_ppocr(in_params):
|
| 199 |
"""Initialization of PPOCR reader
|
| 200 |
|
|
|
|
| 250 |
return out_list_readers
|
| 251 |
|
| 252 |
###
|
|
|
|
| 253 |
@st.experimental_memo(show_spinner=False)
|
| 254 |
def load_image(in_image_file):
|
| 255 |
"""Load input file and open it
|
|
|
|
| 276 |
return out_image_path, out_image_orig, out_image_cv2
|
| 277 |
|
| 278 |
###
|
|
|
|
| 279 |
@st.experimental_memo(show_spinner=False)
|
| 280 |
def easyocr_detect(_in_reader, in_image_path, in_params):
|
| 281 |
"""Detection with EasyOCR
|
|
|
|
| 309 |
return out_easyocr_boxes_coordinates, out_status
|
| 310 |
|
| 311 |
###
|
|
|
|
| 312 |
@st.experimental_memo(show_spinner=False)
|
| 313 |
def ppocr_detect(_in_reader, in_image_path):
|
| 314 |
"""Detection with PPOCR
|
|
|
|
| 332 |
return out_ppocr_boxes_coordinates, out_status
|
| 333 |
|
| 334 |
###
|
|
|
|
|
|
|
| 335 |
@st.experimental_memo(show_spinner=False)
|
| 336 |
def mmocr_detect(_in_reader, in_image_path):
|
| 337 |
"""Detection with MMOCR
|
|
|
|
| 524 |
|
| 525 |
return out_image_drawn
|
| 526 |
|
| 527 |
+
###
|
|
|
|
| 528 |
@st.experimental_memo(show_spinner=False)
|
| 529 |
def get_cropped(in_boxes_coordinates, in_image_cv):
|
| 530 |
"""Construct list of cropped images corresponding of the input boxes coordinates list
|
|
|
|
| 646 |
|
| 647 |
return out_list_text_easyocr, out_list_confidence_easyocr, out_status
|
| 648 |
|
| 649 |
+
###
|
| 650 |
@st.experimental_memo(suppress_st_warning=True, show_spinner=False)
|
| 651 |
def ppocr_recog(in_list_images, in_params):
|
| 652 |
"""Recognition with PPOCR
|
|
|
|
| 685 |
|
| 686 |
return out_list_text_ppocr, out_list_confidence_ppocr, out_status
|
| 687 |
|
| 688 |
+
###
|
| 689 |
@st.experimental_memo(suppress_st_warning=True, show_spinner=False)
|
| 690 |
def mmocr_recog(in_list_images, in_params):
|
| 691 |
"""Recognition with MMOCR
|
|
|
|
| 725 |
|
| 726 |
return out_list_text_mmocr, out_list_confidence_mmocr, out_status
|
| 727 |
|
| 728 |
+
###
|
|
|
|
| 729 |
@st.experimental_memo(suppress_st_warning=True, show_spinner=False)
|
| 730 |
def tesserocr_recog(in_img, in_params, in_nb_images):
|
| 731 |
"""Recognition with Tesseract
|
|
|
|
| 852 |
cols[ind_col].write(list_reco_status[ind], \
|
| 853 |
use_column_width=True)
|
| 854 |
|
| 855 |
+
###
|
| 856 |
def highlight():
|
| 857 |
"""Draw recognized text on original image, for each OCR solution used
|
| 858 |
|
|
|
|
| 890 |
st.session_state.column_width = column_width
|
| 891 |
st.session_state.columns_color = columns_color
|
| 892 |
|
| 893 |
+
###
|
| 894 |
+
@st.cache(show_spinner=False)
|
| 895 |
+
def get_demo():
|
| 896 |
+
"""Get the demo files
|
| 897 |
+
|
| 898 |
+
Returns:
|
| 899 |
+
PIL.Image : input file opened with Pillow
|
| 900 |
+
PIL.Image : input file opened with Pillow
|
| 901 |
+
"""
|
| 902 |
+
|
| 903 |
+
out_img_demo_1 = Image.open("img_demo_1.jpg")
|
| 904 |
+
out_img_demo_2 = Image.open("img_demo_2.png")
|
| 905 |
+
|
| 906 |
+
return out_img_demo_1, out_img_demo_2
|
| 907 |
+
|
| 908 |
###################################################################################################
|
| 909 |
## MAIN
|
| 910 |
###################################################################################################
|
|
|
|
| 920 |
with st.spinner("Initializations in progress ..."):
|
| 921 |
reader_type_list, reader_type_dict, color, list_dict_lang, \
|
| 922 |
cols_size, dict_back_colors, fig_colorscale = initializations()
|
| 923 |
+
img_demo_1, img_demo_2 = get_demo()
|
| 924 |
|
| 925 |
##----------- Choose language & image -------------------------------------------------------------
|
| 926 |
st.markdown("#### Choose languages for the text recognition:")
|
|
|
|
| 933 |
mmocr_lang = list_dict_lang[2][mmocr_key_lang]
|
| 934 |
tesserocr_key_lang = lang_col[3].selectbox(reader_type_list[3]+" :", list_dict_lang[3].keys(), 35)
|
| 935 |
tesserocr_lang = list_dict_lang[3][tesserocr_key_lang]
|
|
|
|
| 936 |
|
| 937 |
+
img_typ = st.radio("#### Choose image:", ['Upload file', 'Take a picture', 'Use a demo file'], index=0)
|
| 938 |
+
|
| 939 |
+
if img_typ == 'Upload file':
|
| 940 |
+
image_file = st.file_uploader("Upload a file:", type=["png","jpg","jpeg"])
|
| 941 |
+
if img_typ == 'Take a picture':
|
| 942 |
+
image_file = st.camera_input("Take a picture:")
|
| 943 |
+
if img_typ == 'Use a demo file':
|
| 944 |
+
cols_demo = st.columns([1, 2, 2])
|
| 945 |
+
demo_used = cols_demo[0].radio('##### Choose a demo file:', ['File 1', 'File 2'], index=0)
|
| 946 |
+
cols_demo[1].markdown('##### File 1')
|
| 947 |
+
cols_demo[1].image(img_demo_1, use_column_width=True)
|
| 948 |
+
cols_demo[2].markdown('##### File 2')
|
| 949 |
+
cols_demo[2].image(img_demo_2, use_column_width=True)
|
| 950 |
+
if demo_used == 'File 1':
|
| 951 |
+
image_file = 'img_demo_1.jpg'
|
| 952 |
+
else:
|
| 953 |
+
image_file = 'img_demo_2.png'
|
| 954 |
|
| 955 |
##----------- Process input image -----------------------------------------------------------------
|
| 956 |
if image_file is not None:
|
|
|
|
| 1166 |
horizontal=True, on_change=highlight)
|
| 1167 |
|
| 1168 |
##----------- Form with hyperparameters for recognition -----------------------
|
| 1169 |
+
st.markdown("##### Hyperparameters values for recognition")
|
| 1170 |
with st.form("form2"):
|
|
|
|
| 1171 |
with st.expander("Choose recognition hyperparameters for " + reader_type_list[0], \
|
| 1172 |
expanded=False):
|
| 1173 |
t0_decoder = st.selectbox('decoder', ['greedy', 'beamsearch', 'wordbeamsearch'], \
|