Nanonets OCR2 八大核心能力,重新定义OCR技术

发布于 2025-10-23 07:52
浏览
0收藏

Nanonets-OCR2以Qwen2.5-VL-3B型号作为视觉语言模型 (VLM) 的基础模型,为了训练新的视觉语言模型 (VLM) 以进行高精度光学字符识别 (OCR),汇集了超过 300 万页的数据集。

该数据集包含广泛的文档类型, 包括研究论文、财务报告、法律合同、医疗记录、税表、收据和 发票。它还包括包含嵌入图像、绘图、方程式、签名、水印、 复选框和复杂表。

此外,还纳入了流程图、组织结构图、手写 材料和多语言文档,以确保全面覆盖现实世界的文档变体。

横向对比

  1. 与dots.ocr对比:在复选框检测、流程图提取、图像描述、签名识别、表格提取、水印处理六大维度,Nanonets OCR 2表现更精准,尤其在复杂结构识别上优势显著;
  2. 与主流模型对决:以Gemini-2.5-Pro为评测基准,Nanonets OCR 2+(高阶版本)在“图像转Markdown”任务中,对阵Gemini 2.5 Flash、GPT-5、Nanonets-OCR-s等模型,胜率最高达57.6%,碾压低阶版本Nanonets-OCR2 1.5B(对方胜率仅13%);
  3. VQA能力评测:在IDP Leaderboard数据集测试中,Nanonets OCR 2+在Chart QA任务得79.20分,DocVQA任务得85.15分,3B版本DocVQA得分更高达89.43分,接近甚至超越Qwen 2.5-VL-72B Instruct等主流大模型。

主要能力

  • LaTeX 方程识别 自动将数学方程式和公式转换为格式正确的 LaTeX 语法。 内联数学表达式转换为 LaTeX 内联方程,而显示的方程是 转换为 LaTeX 显示方程。页码在<page_number>标签中预测。

Nanonets OCR2 八大核心能力,重新定义OCR技术-AI.x社区

  • 智能图像描述 使用结构化标记描述文档中的图像,使其易于 LLM 处理。如果 图标题存在,然后将其用作描述,否则模型将生成 描述。该模型可以描述单个或多个图像(徽标、图表、图形、二维码等) 就其内容、风格和上下文而言。模型预测图像描述  标签。
  • 签名检测和隔离 识别签名并将其与文档中的其他文本隔离开来,这对法律和业务至关重要 文档处理。该模型预测标记中的签名文本。如果 signature 不可读,则模型会将signature返回给 标记为已签名。
  • 水印提取 与签名检测类似,该模型可以检测和提取文档中的水印文本。模型预测标签内的水印文本。该模型在低电平下表现良好 高质量图像,如下所示:

Nanonets OCR2 八大核心能力,重新定义OCR技术-AI.x社区

  • 智能复选框处理 将表单复选框和单选按钮转换为标准化的 Unicode 符号,实现一致性。模型预测标记中的复选框状态。

Nanonets OCR2 八大核心能力,重新定义OCR技术-AI.x社区

  • 复杂表提取 从文档中提取复杂表并将其转换为 Markdown 和 html 表。
  • 流程图和组织结构图 该模型提取流程图和组织结构图的美人鱼代码。
  • 多语言 模型在多种语言的文档上进行训练,包括英语、中文、法语、西班牙语、 葡萄牙语、德语、意大利语、俄语、日语、韩语、阿拉伯语等等。

Nanonets OCR2 八大核心能力,重新定义OCR技术-AI.x社区

  • 视觉问答 该模型旨在直接提供文档中存在的答案;否则,它会回复“未提及”。

Nanonets OCR2 八大核心能力,重新定义OCR技术-AI.x社区

Nanonets-OCR2-3B实战

from PIL import Image
from transformers import AutoTokenizer, AutoProcessor, AutoModelForImageTextToText

model_path = "nanonets/Nanonets-OCR2-3B"

model = AutoModelForImageTextToText.from_pretrained(
    model_path, 
    torch_dtype="auto", 
    device_map="auto", 
    attn_implementatinotallow="flash_attention_2"
)
model.eval()

tokenizer = AutoTokenizer.from_pretrained(model_path)
processor = AutoProcessor.from_pretrained(model_path)


def ocr_page_with_nanonets_s(image_path, model, processor, max_new_tokens=4096):
    prompt = """Extract the text from the above document as if you were reading it naturally. Return the tables in html format. Return the equations in LaTeX representation. If there is an image in the document and image caption is not present, add a small description of the image inside the <img></img> tag; otherwise, add the image caption inside <img></img>. Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>. Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number> or <page_number>9/22</page_number>. Prefer using ☐ and ☑ for check boxes."""
    image = Image.open(image_path)
    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": [
            {"type": "image", "image": f"file://{image_path}"},
            {"type": "text", "text": prompt},
        ]},
    ]
    text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    inputs = processor(text=[text], images=[image], padding=True, return_tensors="pt")
    inputs = inputs.to(model.device)
    
    output_ids = model.generate(**inputs, max_new_tokens=max_new_tokens, do_sample=False)
    generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, output_ids)]
    
    output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)
    return output_text[0]

image_path = "/path/to/your/document.jpg"
result = ocr_page_with_nanonets_s(image_path, model, processor, max_new_tokens=15000)
print(result)

​https://github.com/NanoNets/docstrange​

​https://huggingface.co/nanonets/Nanonets-OCR2-3B​

本文转载自​CourseAI​,作者:CourseAI


已于2025-10-23 11:23:10修改
收藏
回复
举报
回复
相关推荐