import os
import shutil
import uuid
import fitz # PyMuPDF
import gradio as gr
import torch
from PIL import Image, ImageEnhance
from transformers import AutoConfig, AutoModel, AutoTokenizer
from got_ocr import got_ocr
# 初始化模型和分词器
# model_name = "stepfun-ai/GOT-OCR2_0"
model_name = "ucaslcl/GOT-OCR2_0"
device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
config = AutoConfig.from_pretrained(model_name, trust_remote_code=True)
model = AutoModel.from_pretrained(model_name, trust_remote_code=True, low_cpu_mem_usage=True, device_map="cuda", use_safetensors=True)
model = model.eval().to(device)
model.config.pad_token_id = tokenizer.eos_token_id
UPLOAD_FOLDER = "./uploads"
RESULTS_FOLDER = "./results"
# 确保必要的文件夹存在
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(RESULTS_FOLDER, exist_ok=True)
def pdf_to_images(pdf_path):
images = []
pdf_document = fitz.open(pdf_path)
for page_num in range(len(pdf_document)):
page = pdf_document.load_page(page_num)
# 进一步增加分辨率和缩放比例
zoom = 10 # 增加缩放比例到4
mat = fitz.Matrix(zoom, zoom)
pix = page.get_pixmap(matrix=mat, alpha=False)
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
# 增对比度
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(1.5) # 增加50%的对比度
images.append(img)
pdf_document.close()
return images
def process_pdf(pdf_file):
if pdf_file is None:
return None
temp_pdf_path = os.path.join(UPLOAD_FOLDER, f"{uuid.uuid4()}.pdf")
# 使用 shutil 复制上传的件到临时位置
shutil.copy(pdf_file.name, temp_pdf_path)
images = pdf_to_images(temp_pdf_path)
os.remove(temp_pdf_path)
# 将图像保存为临时文件并返回文件路径列表
image_paths = []
for i, img in enumerate(images):
img_path = os.path.join(RESULTS_FOLDER, f"page_{i+1}.png")
img.save(img_path, "PNG")
image_paths.append(img_path)
return image_paths
def on_image_select(evt: gr.SelectData):
if evt.index is not None:
return evt.index
return None
# 更新perform_ocr函数的输入参数
def perform_ocr(selected_index, image_gallery, got_mode, fine_grained_type, color, box):
if selected_index is None or len(image_gallery) == 0:
return "请先选择一张图片"
selected_image = image_gallery[selected_index][0]
# 根据选择的任务和参数调用GOT OCR
ocr_color = color if fine_grained_type == "color" else ""
ocr_box = box if fine_grained_type == "box" else ""
result, html_content = got_ocr(
model,
tokenizer,
selected_image,
got_mode=got_mode,
fine_grained_mode=fine_grained_type,
ocr_color=ocr_color,
ocr_box=ocr_box,
)
if html_content:
iframe_src = f"data:text/html;base64,{html_content}"
iframe = f''
download_link = f'下载完整结果'
return gr.Markdown(f"{download_link}\n\n{iframe}")
else:
return gr.Markdown(result)
def task_update(task):
if "fine-grained" in task:
return [gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)]
else:
return [gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)]
def fine_grained_update(fine_grained_type):
if fine_grained_type == "color":
return [gr.update(visible=True), gr.update(visible=False)]
elif fine_grained_type == "box":
return [gr.update(visible=False), gr.update(visible=True)]
else:
return [gr.update(visible=False), gr.update(visible=False)]
with gr.Blocks() as demo:
pdf_input = gr.File(label="上传PDF文件")
image_gallery = gr.Gallery(
label="PDF页面预览",
columns=3,
height=600,
object_fit="contain",
preview=True,
)
selected_index = gr.State(None)
pdf_input.upload(fn=process_pdf, inputs=pdf_input, outputs=image_gallery)
image_gallery.select(fn=on_image_select, inputs=[], outputs=selected_index)
####################
task_dropdown = gr.Dropdown(
choices=["plain texts OCR", "format texts OCR", "plain multi-crop OCR", "format multi-crop OCR", "plain fine-grained OCR", "format fine-grained OCR"],
label="选择GOT模式",
value="format texts OCR",
)
fine_grained_dropdown = gr.Dropdown(choices=["box", "color"], label="fine-grained类型", visible=False)
color_dropdown = gr.Dropdown(choices=["red", "green", "blue"], label="颜色列表", visible=False)
box_input = gr.Textbox(label="输入框: [x1,y1,x2,y2]", placeholder="例如: [0,0,100,100]", visible=False)
ocr_button = gr.Button("开始OCR识别")
ocr_result = gr.HTML(label="OCR结果") # 将Textbox更改为HTML组件
task_dropdown.change(task_update, inputs=[task_dropdown], outputs=[fine_grained_dropdown, color_dropdown, box_input])
fine_grained_dropdown.change(fine_grained_update, inputs=[fine_grained_dropdown], outputs=[color_dropdown, box_input])
# 更新ocr_button的click事件,传递所有必要的参数
ocr_button.click(
fn=perform_ocr,
inputs=[selected_index, image_gallery, task_dropdown, fine_grained_dropdown, color_dropdown, box_input],
outputs=ocr_result,
)
if __name__ == "__main__":
demo.launch()