Skip to content

Commit

Permalink
增加分析按钮,文本匹配记录记录
Browse files Browse the repository at this point in the history
  • Loading branch information
Topfunplus committed Dec 19, 2024
1 parent c19152b commit eb661f7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
35 changes: 34 additions & 1 deletion ocr_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@
from surya.settings import settings
from surya.tables import batch_table_recognition
from surya.postprocessing.util import rescale_bboxes, rescale_bbox

# 导入自定义的stSession
from surya.util.session import Session


# 导入正则表达式模块
import re

# 缓存资源 建立Session对象
st_session = Session(st=st, key="ocr_app", value=None)

Expand Down Expand Up @@ -307,12 +313,20 @@ def display_ocr_results(rec_img, pred):

# 如果是文本格式 这里可以接入ollama 进行文本分析 给出建议
with text_tab:
# index是每一个识别出的文本的索引,p是每一个识别出的文本
for index, p in enumerate(pred.text_lines):
cols = st.columns([4, 1]) # 创建两列,第一列宽度为4,第二列宽度为1
with cols[0]:
st.text(p.text) # 在第一列显示文本
with cols[1]:
st.button("分析", key=f"analyze_button_{index}") # 在第二列显示按钮
is_checked = st.button("分析", key=f"analyze_button_{
index}") # 在第二列显示按钮

# 如果按钮被点击 那么就要展示一个对话框,然后跟ollama对接
if is_checked:
st.write("分析中...")
# TODO 存储到session中 其他的地方可以直接拿到
st_session.set(f"text-{p.text}", p.text)


# 如果session中已经有了ocr的信息
Expand Down Expand Up @@ -368,3 +382,22 @@ def display_ocr_results(rec_img, pred):
# 显示上传的图片
with col2:
st.image(pil_image, caption="上传的图片", use_container_width=True)

is_have = st_session.get('text-', is_reg=True)

print('is_have:', is_have)

if is_have:

if len(is_have) == 0:
st.write('渲染错误:没有匹配到相关的文本')

else:
# 使用 Markdown 美化标题
st.markdown("### 分析文本记录")
st.markdown("---") # 添加分割线
# 遍历所有的文本记录 方便展示
for text in is_have:
st.write(text)
else:
print("没有匹配到相关的文本")
7 changes: 6 additions & 1 deletion surya/model/detection/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,12 @@ def forward(self, x):

# EfficientViT预训练模型
'''
arxiv: https://arxiv.org/abs/2205.14756
arxiv: https://arxiv.org/abs/2205.14756
name : 具有级联组注意力机制的内存高效视觉转换器
github : https://github.com/microsoft/Cream/tree/main/EfficientViT
EfficientViT是一种高效的视觉Transformer
(Vision Transformer)架构。Transformer模型最初
用于自然语言处理任务,但近年来也被应用于计算机视觉任务,
Expand Down
31 changes: 26 additions & 5 deletions surya/util/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Session封装类
基于Streamlit API 实现用于存储用户的会话信息
'''
import re


class Session:
Expand All @@ -10,15 +11,35 @@ def __init__(self, st, key, value):
self._key = key
self._value = value

def get(self, str):

if str not in self._st.session_state:
return None

# 获取所有键
def keys(self):
return self._st.session_state.keys()
# 获取 支持正则匹配

def get(self, str, is_reg=False):
# 如果开启正则匹配
if is_reg:
# 正则
pattern = re.compile(r"^" + re.escape(str))
print('pattern', pattern)
# 满足条件的集合 用于返回所有的键
result = []
# 遍历所有的键
for key in self.keys():
if pattern.match(key):
result.append(self._st.session_state[key])

print("regex::get", result)
return result
# 不开启正则匹配 就直接查找返回
else:
if str not in self._st.session_state:
return None

return self._st.session_state[f'{str}']

def set(self, str, value):
print('session::set', str, value)
self._st.session_state[f'{str}'] = value

def remove(self, str):
Expand Down

0 comments on commit eb661f7

Please sign in to comment.