Spaces:
Running
Running
File size: 4,811 Bytes
e919229 d3a7100 134c129 e919229 134c129 d3a7100 e919229 134c129 0c54c5e d3a7100 0c54c5e d3a7100 134c129 d3a7100 0c54c5e d3a7100 134c129 d3a7100 |
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 |
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
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 = '''<style>td {border-left: 1px solid;border-bottom:1px solid;}
table, th {border-top:1px solid;font-size: 10px;
border-collapse: collapse;border-right: 1px solid;}
</style>'''
prefix_table, suffix_table = table_res.split('<body>')
new_table_res = f'{prefix_table}{style_res}<body>{suffix_table}'
return new_table_res
if __name__ == '__main__':
st.markdown("<h1 style='text-align: center;'><a href='https://github.com/RapidAI/RapidStructure' style='text-decoration: none'>Rapid Structure</a></h1>", unsafe_allow_html=True)
st.markdown("""
<p align="left">
<a href=""><img src="https://img.shields.io/badge/Python->=3.7,<=3.10-aff.svg"></a>
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
<a href="https://pepy.tech/project/rapid-layout"><img src="https://static.pepy.tech/personalized-badge/rapid-layout?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=rapid-layout"></a>
<a href="https://pepy.tech/project/rapid-orientation"><img src="https://static.pepy.tech/personalized-badge/rapid-orientation?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=rapid-orientation"></a>
<a href="https://pepy.tech/project/rapid-table"><img src="https://static.pepy.tech/personalized-badge/rapid-table?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=rapid-table"></a>
</p>
""", 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)
|