MCPcopy Create free account
hub / github.com/BIT-DataLab/Edit-Banana / Pix2TextOCR

Class Pix2TextOCR

modules/text/ocr/pix2text.py:58–252  ·  view source on GitHub ↗

Pix2Text OCR 客户端 专门用于识别数学公式,返回 LaTeX 格式。 使用示例: ocr = Pix2TextOCR() result = ocr.analyze_image("input.png") for block in result.blocks: if block.is_latex: print(f"公式: {block.text}")

Source from the content-addressed store, hash-verified

56
57
58class Pix2TextOCR:
59 """
60 Pix2Text OCR 客户端
61
62 专门用于识别数学公式,返回 LaTeX 格式。
63
64 使用示例:
65 ocr = Pix2TextOCR()
66 result = ocr.analyze_image("input.png")
67 for block in result.blocks:
68 if block.is_latex:
69 print(f"公式: {block.text}")
70 """
71
72 def __init__(self, device: str = 'cuda', languages: tuple = ('en',)):
73 """
74 初始化 Pix2Text
75
76 Args:
77 device: 计算设备(cuda 使用 GPU3)
78 languages: 文字语言(影响非公式部分的识别)
79 """
80 print(f" Pix2Text 使用设备: {device}")
81
82 # 降低 MFD 检测阈值,提高公式检测率
83 self.p2t = Pix2Text.from_config(
84 device=device,
85 text_config={'device': device, 'languages': languages},
86 formula_config={'device': device},
87 # MFD 配置:降低置信度阈值
88 mfd_config={
89 'device': device,
90 'conf_threshold': 0.15, # 默认 0.25,降低以检测更多公式
91 'iou_threshold': 0.45, # 默认 0.45
92 },
93 )
94
95 def analyze_image(self, image_path: str) -> Pix2TextResult:
96 """
97 分析图像
98
99 Args:
100 image_path: 图像文件路径
101
102 Returns:
103 Pix2TextResult: 识别结果(包含文字和公式)
104 """
105 image_path = Path(image_path)
106 if not image_path.exists():
107 raise FileNotFoundError(f"图像文件不存在: {image_path}")
108
109 # 获取图像尺寸
110 from PIL import Image
111 with Image.open(image_path) as img:
112 image_width, image_height = img.size
113
114 # 识别(使用较低的 resized_shape 提高检测率)
115 # result = self.p2t.recognize(str(image_path), resized_shape=1600, return_text=False)

Callers 1

pix2text_ocrMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected