Create ocr_module.py
Browse filesimport os
import json
import random
import re
from typing import Dict, List, Any
from PIL import Image
CRITICAL_OPERATORS = ["\\int", "\\sum", "=", "\\frac", "+", "-", "*", "\\times", "\\div"]
BRACKETS_LIMITS = ["(", ")", "[", "]", "\\{", "\\}", "^", "_"]
AMBIGUOUS_SYMBOLS = ["8", "B", "0", "O", "l", "1", "I", "S", "5", "Z", "2"]
def get_symbol_weight(symbol: str) -> float:
if symbol in CRITICAL_OPERATORS: return 1.5
elif symbol in BRACKETS_LIMITS: return 1.3
elif symbol in AMBIGUOUS_SYMBOLS: return 0.7
return 1.0
def calculate_weighted_confidence(latex_string: str, mock_logits: bool = True) -> float:
tokens = []
current_token = ""
for char in latex_string:
if char == '\\':
if current_token: tokens.append(current_token)
current_token = char
elif char.isalnum() and current_token.startswith('\\'):
current_token += char
else:
if current_token:
tokens.append(current_token)
current_token = ""
if char.strip(): tokens.append(char)
if current_token: tokens.append(current_token)
total_weighted_ci = 0.0
total_weights = 0.0
for token in tokens:
w_i = get_symbol_weight(token)
c_i = random.uniform(0.85, 0.99) if mock_logits else 0.95
total_weighted_ci += (w_i * c_i)
total_weights += w_i
if total_weights == 0: return 0.0
return round(total_weighted_ci / total_weights, 4)
class MVM2OCREngine:
def __init__(self):
try:
from pix2text import Pix2Text
self.p2t = Pix2Text.from_config()
self.model_loaded = True
except:
self.model_loaded = False
def clean_latex_output(self, text: str) -> str:
cjk_re = re.compile(r'[\u4e00-\u9fff\u3040-\u30ff\uac00-\ud7af]')
return cjk_re.sub('', text)
def process_image(self, image_path: str) -> Dict[str, Any]:
if not os.path.exists(image_path): return {"error": "File not found"}
if self.model_loaded:
try:
out = self.p2t.recognize(image_path)
if isinstance(out, str): raw_latex = out
else: raw_latex = "\n".join([item.get('text', '') for item in out])
except: raw_latex = "Error during OCR"
else: raw_latex = "No math detected (Simulated)."
raw_latex = self.clean_latex_output(raw_latex)
return {"latex_output": raw_latex, "weighted_confidence": calculate_weighted_confidence(raw_latex)}
- ocr_module.py +24 -0
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
import re
|
| 5 |
+
from typing import Dict, List, Any
|
| 6 |
+
from PIL import Image
|
| 7 |
+
CRITICAL_OPERATORS = ["\\int", "\\sum", "=", "\\frac", "+", "-", "*", "\\times", "\\div"]
|
| 8 |
+
BRACKETS_LIMITS = ["(", ")", "[", "]", "\\{", "\\}", "^", "_"]
|
| 9 |
+
AMBIGUOUS_SYMBOLS = ["8", "B", "0", "O", "l", "1", "I", "S", "5", "Z", "2"]
|
| 10 |
+
|
| 11 |
+
def get_symbol_weight(symbol: str) -> float:
|
| 12 |
+
if symbol in CRITICAL_OPERATORS: return 1.5
|
| 13 |
+
elif symbol in BRACKETS_LIMITS: return 1.3
|
| 14 |
+
elif symbol in AMBIGUOUS_SYMBOLS: return 0.7
|
| 15 |
+
return 1.0
|
| 16 |
+
|
| 17 |
+
def calculate_weighted_confidence(latex_string: str, mock_logits: bool = True) -> float:
|
| 18 |
+
tokens = []
|
| 19 |
+
current_token = ""
|
| 20 |
+
for char in latex_string:
|
| 21 |
+
if char == '\\':
|
| 22 |
+
if current_token: tokens.append(current_token)
|
| 23 |
+
current_token = char
|
| 24 |
+
|