Spaces:
Running
Running
File size: 6,099 Bytes
e919229 d3a7100 134c129 e919229 134c129 d3a7100 86e03fd d3a7100 86e03fd b6832d1 86e03fd d3a7100 86e03fd d3a7100 86e03fd d3a7100 86e03fd e7ae4a7 86e03fd d3a7100 e919229 134c129 86e03fd |
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 |
# -*- 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
from streamlit_image_select import image_select
orientation_engine = RapidOrientation()
layout_engine = RapidLayout()
table_engine = RapidTable()
def orientation_part():
st.markdown('##### 文档图像方向分类')
img = image_select(
label="示例图(点击选择)",
images=[
'images/orientation/img_rot0_demo.jpg',
'images/orientation/rot_90.jpg',
'images/orientation/img_rot180_demo.jpg',
'images/orientation/rot_270.jpg'
],
captions=['0', '90', '180', '270'],
key='cls'
)
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()
image, orientation_res, elapse = get_cls_res(img)
if img_file_buffer:
image, orientation_res, elapse = get_cls_res(img_file_buffer)
img_empty.image(image, use_column_width=True)
col2.markdown(f'- 方向分类结果:{orientation_res}° \n - 耗费时间:{elapse:.4f}s')
def get_cls_res(img_file_buffer):
image = Image.open(img_file_buffer)
img = np.array(image)
img_array = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
orientation_res, elapse = orientation_engine(img_array)
return image, orientation_res, elapse
def layout_part():
st.markdown('##### 文档图像版面分析')
img = image_select(
label="示例图(点击选择)",
images=[
'images/layout/layout.jpg',
'images/layout/layout1.jpg',
'images/layout/layout2.jpg',
],
key='layout_select'
)
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()
image, drawed_img = get_layout_res(img)
if img_file_buffer:
image, drawed_img = get_layout_res(img_file_buffer)
img_empty.image(image, use_column_width=True)
layout_col2.image(drawed_img, use_column_width=True)
def get_layout_res(img_file_buffer):
image = Image.open(img_file_buffer)
img = np.array(image)
img_array = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
layout_res, _ = layout_engine(img_array)
drawed_img = vis_layout(img, layout_res)
return image, drawed_img
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, 0, 0), 2)
cv2.putText(tmp_img, label, start_point,
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
return tmp_img
def table_part():
st.markdown('##### 表格还原')
img = image_select(
label="示例图(点击选择)",
images=[
'images/table/table.jpg',
'images/table/table1.png',
'images/table/table2.png',
],
key='table_select'
)
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()
image, table_html_str = get_table_res(img)
if img_file_buffer:
image, table_html_str = get_table_res(img_file_buffer)
img_empty.image(image, use_column_width=True)
table_col2.markdown(table_html_str, unsafe_allow_html=True)
def get_table_res(img_file_buffer):
image = Image.open(img_file_buffer)
img = np.array(image)
img_array = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
table_html_str, _, _ = table_engine(img_array)
table_html_str = vis_table(table_html_str)
return image, table_html_str
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"]
orientation_part()
layout_part()
table_part()
|