目录:
OCR (Optical Character Recognition, 光学字符识别)
: 一种将打印体或手写文字转换成可编辑文本的技术,即将图像中的文字进行识别,并以文本的形式返回。

OCR识别技术主要依赖于
图像处理和模式识别算法,通过捕捉文档中字符的特征,如笔画、形状、大小、间距等,与预设的文字库进行对比,从而辨识相应文字信息。
2025年10月20日,DeepSeek正式开源了DeepSeek-OCR模型,并同步发布了相关的运行脚本、测试代码、DeepEncoder源码以及技术报告等全面资料。在实际应用层面,它是目前开源社区少数具备端到端文档解析、语义理解和结构化生成能力的轻量级多模态模型,参数仅约3B, 却能在A100单卡上达到高达2500 tokens/s的推理速度,极大降低了企业和研究者在多模态RAG系统中的部署门槛。 DeepSeek-OCR的架构图如下所示:

DeepSeek-OCR采用了统一的端到端视觉语言模型(VLM)结构,由编码器和解码器构成。编码器(DeepEncoder)负责提取图像特征、进行分词处理以及压缩视觉表示。解码器则基于图像分词结果和提示词生成所需输出。DeepEncoder参数规模约3.8亿,主要由包含8000万参数的SAM-base与含3亿参数的CLIP-large串联组成。解码器采用3B MOE架构,拥有5.7亿激活参数。具体来说,DeepSeek-OCR模型可实现以下几大核心功能:
在NVIDIA GPU上使用Huggingface的Transformers库进行推理。经过本地部署后进行测试,实际使用的虚拟环境配置如下:
pip install flash-attn==2.7.3 --no-build-isolation (flash-attn是一个高效的GPU注意力加速库,常用于加快大模型中Transformer注意力计算的速度)(–no-build-isolation 创建一个临时的隔离虚拟环境)
from huggingface_hub import snapshot_download
import os
os.environ['HTTP_PROXY'] = 'http://127.0.0.1:7890' #使用代理。(仅在本地确实有代理服务,如clash等)时才能生效
os.environ['HTTPS_PROXY'] = 'http://127.0.0.1:7890'
snapshot_download(
repo_id="deepseek-ai/DeepSeek-OCR",
local_dir="../../Pretrained-LLM/models/DeepSeek-OCR", #指定下载路径
local_dir_use_symlinks=False,
resume_download=True,
revision="main"
)

from transformers import AutoModel, AutoTokenizer
import torch
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
model_path = '../../Pretrained-LLM/models/DeepSeek-OCR'
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModel.from_pretrained(model_path, trust_remote_code=True, use_safetensors=True)
model = model.eval().cuda().to(torch.bfloat16)
# prompt = "<image>\nFree OCR. "
prompt = "<image>\n<|grounding|>Transform the document to markdown. "
image_file = 'news.png'
output_path = './ouput'
# infer(self, tokenizer, prompt='', image_file='', output_path = ' ', base_size = 1024, image_size = 640, crop_mode = True, test_compress = False, save_results = False):
# Tiny: base_size = 512, image_size = 512, crop_mode = False
# Small: base_size = 640, image_size = 640, crop_mode = False
# Base: base_size = 1024, image_size = 1024, crop_mode = False
# Large: base_size = 1280, image_size = 1280, crop_mode = False
# Gundam: base_size = 1024, image_size = 640, crop_mode = True
res = model.infer(tokenizer, prompt=prompt, image_file=image_file, output_path = output_path, base_size = 1024, image_size = 640, crop_mode=True, save_results = True, test_compress = True)
执行代码后,终端将显示以下信息:
=====================
BASE: torch.Size([1, 256, 1280])
PATCHES: torch.Size([6, 100, 1280])
=====================
The attention layers in this model are transitioning from computing the RoPE embeddings internally through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed `position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `
position_ids` will be removed and `position_embeddings` will be mandatory.
<|ref|>sub_title<|/ref|><|det|>[[5, 12, 152, 60]]<|/det|>
## 观点·深度》》
<|ref|>image<|/ref|><|det|>[[5, 95, 528, 560]]<|/det|>
<|ref|>sub_title<|/ref|><|det|>[[5, 608, 352, 656]]<|/det|>
## 用精品标注时代文艺创作的高度
<|ref|>text<|/ref|><|det|>[[5, 707, 525, 920]]<|/det|>
近日揭晓的第十八届文华奖,作为文化和旅游部设立的专业舞台艺术领域政府最高奖项,通过优化奖项设置、放宽首演时间、扩大申报渠道等改革,进一步完善评奖机制,强化“十年磨一戏”的精品意识,捧出了舞台艺术繁荣发展的累累硕果。
<|ref|>sub_title<|/ref|><|det|>[[570, 99, 812, 140]]<|/det|>
## 年轻干部不妨多些“自找苦吃”
<|ref|>text<|/ref|><|det|>[[570, 162, 952, 247]]<|/det|>
干部成长成熟成才的路上,从没有坦途捷径可走,只能是一步一个脚印爬坡过坎。
<|ref|>sub_title<|/ref|><|det|>[[570, 303, 965, 344]]<|/det|>
## 财评:“两新”精准,彰显宏观调控前瞻性有效性
<|ref|>text<|/ref|><|det|>[[570, 366, 947, 450]]<|/det|>
当前,“两新”政策的实施正在推动经济发展质量、结构、效益同步提升。
<|ref|>sub_title<|/ref|><|det|>[[570, 506, 789, 547]]<|/det|>
## 银行网点增减应以便民为本
<|ref|>text<|/ref|><|det|>[[570, 569, 965, 650]]<|/det|>
网点增减是银行出于经营管理考量的自主选择,但其底层逻辑应遵循便民惠民、降本增效。
<|ref|>sub_title<|/ref|><|det|>[[570, 708, 789, 748]]<|/det|>
## 算好“过紧日子”的两本账
<|ref|>text<|/ref|><|det|>[[570, 770, 946, 855]]<|/det|>
“过紧日子”不是捂紧钱包不花钱,而是该省的省、该花的花。
<|ref|>text<|/ref|><|det|>[[592, 920, 720, 954]]<|/det|>
查看更多深度评论
==================================================
image size: (902, 565)
valid image tokens: 760
output texts tokens (valid): 471
compression ratio: 0.62
==================================================
===============save results:===============
image: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 22429.43it/s]
other: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 12/12 [00:00<00:00, 310689.19it/s]
同时会生成output文件夹,其中包含文档中的图像、识别后的markdown文本文件,以及如下目标检测图像result_with_boxes.jpg。

扫码加好友,拉您进群



收藏
