加入了web.py,但是server还没实现

This commit is contained in:
三洋三洋
2024-01-22 09:42:20 +00:00
parent 126026cb48
commit 703ac7441c
2 changed files with 80 additions and 19 deletions

View File

@@ -3,7 +3,7 @@ IMAGE_MEAN = 0.9545467
IMAGE_STD = 0.15394445 IMAGE_STD = 0.15394445
# ========================= TeXify模型用的参数 ============================= # # ========================= ocr模型用的参数 ============================= #
# 输入图片的最大最小的宽和高 # 输入图片的最大最小的宽和高
MIN_HEIGHT = 32 MIN_HEIGHT = 32
@@ -12,7 +12,7 @@ MIN_WIDTH = 32
MAX_WIDTH = 1280 MAX_WIDTH = 1280
# LaTex-OCR中分别是 32、192、32、672 # LaTex-OCR中分别是 32、192、32、672
# TeXify模型所用数据集中图片所用的Density渲染值 # ocr模型所用数据集中图片所用的Density渲染值实际上图片用的渲染Density不是80而是100
TEXIFY_INPUT_DENSITY = 80 TEXIFY_INPUT_DENSITY = 80
# ============================================================================= # # ============================================================================= #
@@ -25,7 +25,7 @@ RESIZER_INPUT_DENSITY = 200
LABEL_RATIO = 1.0 * TEXIFY_INPUT_DENSITY / RESIZER_INPUT_DENSITY LABEL_RATIO = 1.0 * TEXIFY_INPUT_DENSITY / RESIZER_INPUT_DENSITY
NUM_CLASSES = 1 # 模型使用回归预测最后会接一个sigmoid预测01 NUM_CLASSES = 1 # 模型使用回归预测
NUM_CHANNELS = 1 # 输入单通道图片(灰度图) NUM_CHANNELS = 1 # 输入单通道图片(灰度图)
# Resizer在训练时图片所固定的的大小 # Resizer在训练时图片所固定的的大小

View File

@@ -1,33 +1,94 @@
import streamlit as st import streamlit as st
import time import io
import base64
import requests
from stqdm import stqdm from PIL import Image
def post_image(server_url, img_rb):
response = requests.post(server_url, files={'image': img_rb})
return response.text
# ============================ pages =============================== #
# 使用 Markdown 和 HTML 将标题居中 # 使用 Markdown 和 HTML 将标题居中
with st.columns(3)[1]: # with st.columns(3)[1]:
st.title(":rainbow[TexTeller] :sparkles:") # st.title(":rainbow[TexTeller] :sparkles:")
# HTML字符串包含内联CSS用于彩色和居中
# html_string = """
# <h1 style="color: orange; text-align: center;">
# ✨ TexTeller ✨
# </h1>
# """
html_string = """
<h1 style="color: orange; text-align: center;">
🔥👁️ OCR
</h1>
"""
st.markdown(html_string, unsafe_allow_html=True)
if "start" not in st.session_state: if "start" not in st.session_state:
st.balloons() st.balloons()
st.session_state["start"] = 1 st.session_state["start"] = 1
# 上传图片
uploaded_file = st.file_uploader("",type=['jpg', 'png']) uploaded_file = st.file_uploader("",type=['jpg', 'png'])
st.divider()
# 显示上传图片
if uploaded_file: if uploaded_file:
st.image(uploaded_file, caption="Input image") # 打开上传图片
img = Image.open(uploaded_file)
# st.image(uploaded_file, caption=f"Input image ({img.height}✖️{img.width})")
for _ in stqdm(range(10), st_container=st.sidebar): # 将 BytesIO 对象转换为 Base64 编码
time.sleep(0.1) def get_image_base64(img_file):
buffered = io.BytesIO()
img_file.seek(0) # 重置文件指针位置
img = Image.open(img_file)
img.save(buffered, format="PNG")
return base64.b64encode(buffered.getvalue()).decode()
with st.spinner('Wait for it...'): img_base64 = get_image_base64(uploaded_file)
time.sleep(5)
st.success('Done!') # 使用Markdown和HTML创建一个居中的图片容器
st.markdown(f"""
<style>
.centered-container {{
text-align: center;
}}
.centered-image {{
display: block;
margin-left: auto;
margin-right: auto;
max-width: 700px;
}}
</style>
<div class="centered-container">
<img src="data:image/png;base64,{img_base64}" class="centered-image" alt="Input image">
<p style="color:gray;">Input image ({img.height}✖️{img.width})</p>
</div>
""", unsafe_allow_html=True)
st.write("")
st.write("")
with st.empty(): # 预测
for seconds in range(60): with st.spinner("Predicting..."):
st.write(f"{seconds} seconds have passed") # 预测结果
time.sleep(1) server_url = 'http://localhost:8000/'
st.write("✔️ 1 minute over!") uploaded_file.seek(0)
TeXTeller_result = post_image(server_url, uploaded_file)
TeXTeller_result = r"\begin{align*}" + '\n' + TeXTeller_result + '\n' + r'\end{align*}'
# tab1, tab2 = st.tabs(["✨TeXTeller✨", "pix2tex:gray[(9.6K⭐)]"])
tab1, tab2 = st.tabs(["🔥👁️", "pix2tex:gray[(9.6K⭐)]"])
# with st.container(border=True):
with tab1:
st.latex(TeXTeller_result)
st.write("")
st.code(TeXTeller_result, language='latex')
st.success('Done!')
# ============================ pages =============================== #