# -*- encoding: utf-8 -*- # @Author: SWHL # @Contact: liekkaskono@163.com import copy import cv2 import numpy as np import streamlit as st from PIL import Image from rapid_layout import RapidLayout from rapid_orientation import RapidOrientation from rapid_table import RapidTable orientation_engine = RapidOrientation() layout_engine = RapidLayout() table_engine = RapidTable() def vis_layout(img: np.ndarray, layout_res: list) -> None: tmp_img = copy.deepcopy(img) for v in layout_res: bbox = np.round(v['bbox']).astype(np.int32) label = v['label'] start_point = (bbox[0], bbox[1]) end_point = (bbox[2], bbox[3]) cv2.rectangle(tmp_img, start_point, end_point, (0, 255, 0), 2) cv2.putText(tmp_img, label, start_point, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2) return tmp_img def vis_table(table_res) -> str: style_res = '''''' prefix_table, suffix_table = table_res.split('') new_table_res = f'{prefix_table}{style_res}{suffix_table}' return new_table_res if __name__ == '__main__': st.markdown("

Rapid Structure

", unsafe_allow_html=True) st.markdown("""

""", unsafe_allow_html=True) img_suffix = ["png", "jpg", "jpeg"] st.markdown('##### [文档图像方向分类](https://github.com/RapidAI/RapidStructure/blob/main/docs/README_Orientation.md)') img_file_buffer = st.file_uploader("Upload an image", type=img_suffix, key='orientation', label_visibility='collapsed') col1, col2 = st.columns([5, 5]) img_empty = col1.empty() if img_file_buffer: image = Image.open(img_file_buffer) img = np.array(image) img_empty.image(image, use_column_width=True) img_array = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) orientation_res, elapse = orientation_engine(img) col2.markdown(f'- 方向分类结果:{orientation_res}° \n - 耗费时间:{elapse:.4f}s') st.markdown('##### [文档图像版面分析](https://github.com/RapidAI/RapidStructure/blob/main/docs/README_Layout.md)') img_file_buffer = st.file_uploader("Upload an image", type=img_suffix, key='layout', label_visibility='collapsed') layout_col1, layout_col2 = st.columns([5, 5]) img_empty = layout_col1.empty() if img_file_buffer: image = Image.open(img_file_buffer) img = np.array(image) img_empty.image(image, use_column_width=True) img_array = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) layout_res, _ = layout_engine(img) drawed_img = vis_layout(img, layout_res) layout_col2.image(drawed_img, use_column_width=True) st.markdown('##### [表格还原](https://github.com/RapidAI/RapidStructure/blob/main/docs/README_Table.md)') img_file_buffer = st.file_uploader("Upload an image", type=img_suffix, key='table', label_visibility='collapsed') table_col1, table_col2 = st.columns([5, 5]) img_empty = table_col1.empty() if img_file_buffer: image = Image.open(img_file_buffer) img = np.array(image) img_empty.image(image, use_column_width=True) img_array = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) table_html_str, _ = table_engine(img) table_html_str = vis_table(table_html_str) table_col2.markdown(table_html_str, unsafe_allow_html=True)