ocr_engine.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import os
  2. import string
  3. import io
  4. from io import BytesIO
  5. import numpy as np
  6. from PIL import Image, ImageFilter
  7. import ddddocr
  8. # Suppress ONNX Runtime warnings
  9. os.environ["ORT_LOGGING_LEVEL"] = "3"
  10. try:
  11. import onnxruntime as ort
  12. except ImportError:
  13. ort = None
  14. # ================= ONNX OCR Engine (Lightweight, No PyTorch/NVIDIA required) =================
  15. class OnnxEngine:
  16. def __init__(self, model_path: str = None):
  17. self.num_classes = 12
  18. self.characters = '-' + string.digits + '$'
  19. self.width = 150
  20. self.hight = 80
  21. self.ready = False
  22. self.session = None
  23. self.input_name = None
  24. if not model_path:
  25. # Default to data/ctc.onnx if available
  26. default_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data", "ctc.onnx")
  27. if os.path.exists(default_path):
  28. model_path = default_path
  29. # If .pth is provided, check for sibling .onnx file
  30. if model_path and model_path.endswith('.pth'):
  31. candidate = model_path[:-4] + '.onnx'
  32. if os.path.exists(candidate):
  33. model_path = candidate
  34. if model_path and os.path.exists(model_path):
  35. if ort is None:
  36. print(f"[OnnxEngine] Warning: onnxruntime not installed, cannot load {model_path}")
  37. return
  38. opts = ort.SessionOptions()
  39. opts.log_severity_level = 3
  40. self.session = ort.InferenceSession(model_path, sess_options=opts, providers=['CPUExecutionProvider'])
  41. self.input_name = self.session.get_inputs()[0].name
  42. self.ready = True
  43. print(f"[OnnxEngine] Loaded: {model_path}")
  44. else:
  45. print(f"[OnnxEngine] Model file not found: {model_path}")
  46. def decode(self, sequence):
  47. a = ''.join([self.characters[x] for x in sequence])
  48. s = []
  49. last = None
  50. for x in a:
  51. if x != last:
  52. s.append(x)
  53. last = x
  54. return ''.join([x for x in s if x != self.characters[0]])
  55. def inference_bytes(self, image_bytes):
  56. if not self.ready or self.session is None:
  57. return "Error: Model not loaded"
  58. try:
  59. # Preprocessing equivalent to torchvision transforms (Resize((80, 150)), ToTensor())
  60. image = Image.open(BytesIO(image_bytes)).convert('RGB').resize((self.width, self.hight))
  61. img_np = np.array(image, dtype=np.float32) / 255.0
  62. img_np = np.transpose(img_np, (2, 0, 1)) # (3, 80, 150)
  63. img_np = np.expand_dims(img_np, axis=0) # (1, 3, 80, 150)
  64. ort_outs = self.session.run(None, {self.input_name: img_np})[0]
  65. output_argmax = np.argmax(np.transpose(ort_outs, (1, 0, 2)), axis=-1)
  66. return self.decode(output_argmax[0])
  67. except Exception as e:
  68. return f"Error: {str(e)}"
  69. # Backwards compatibility alias for plugins importing PyTorchEngine
  70. PyTorchEngine = OnnxEngine
  71. # Optional: Legacy PyTorch Model if torch is installed in dev environment
  72. try:
  73. import torch
  74. from torch import nn
  75. from collections import OrderedDict
  76. from torchvision import transforms
  77. class Model(nn.Module):
  78. def __init__(self, n_classes, input_shape=(3, 64, 128)):
  79. super(Model, self).__init__()
  80. self.input_shape = input_shape
  81. channels = [32, 64, 128, 256, 256]
  82. layers = [2, 2, 2, 2, 2]
  83. kernels = [3, 3, 3, 3, 3]
  84. pools = [2, 2, 2, 2, (2, 1)]
  85. modules = OrderedDict()
  86. def cba(name, in_channels, out_channels, kernel_size):
  87. modules[f'conv{name}'] = nn.Conv2d(in_channels, out_channels, kernel_size,
  88. padding=(1, 1) if kernel_size == 3 else 0)
  89. modules[f'bn{name}'] = nn.BatchNorm2d(out_channels)
  90. modules[f'relu{name}'] = nn.ReLU(inplace=True)
  91. last_channel = 3
  92. for block, (n_channel, n_layer, n_kernel, k_pool) in enumerate(zip(channels, layers, kernels, pools)):
  93. for layer in range(1, n_layer + 1):
  94. cba(f'{block+1}{layer}', last_channel, n_channel, n_kernel)
  95. last_channel = n_channel
  96. modules[f'pool{block + 1}'] = nn.MaxPool2d(k_pool)
  97. modules[f'dropout'] = nn.Dropout(0.25, inplace=True)
  98. self.cnn = nn.Sequential(modules)
  99. self.lstm = nn.LSTM(input_size=self.infer_features(), hidden_size=128, num_layers=2, bidirectional=True)
  100. self.fc = nn.Linear(in_features=256, out_features=n_classes)
  101. def infer_features(self):
  102. x = torch.zeros((1,)+self.input_shape)
  103. x = self.cnn(x)
  104. x = x.reshape(x.shape[0], -1, x.shape[-1])
  105. return x.shape[1]
  106. def forward(self, x):
  107. x = self.cnn(x)
  108. x = x.reshape(x.shape[0], -1, x.shape[-1])
  109. x = x.permute(2, 0, 1)
  110. x, _ = self.lstm(x)
  111. x = self.fc(x)
  112. return x
  113. except ImportError:
  114. Model = None
  115. # ================= ddddocr Engine =================
  116. class DddOcrEngine:
  117. def __init__(self):
  118. self.ocr = ddddocr.DdddOcr(show_ad=False, beta=True)
  119. def inference_bytes(self, image_bytes):
  120. try:
  121. return self.ocr.classification(image_bytes)
  122. except Exception as e:
  123. return f"Error: {e}"
  124. def inference_captcha(self, image_bytes):
  125. try:
  126. image = Image.open(io.BytesIO(image_bytes))
  127. gray_img = image.convert("L").filter(ImageFilter.MedianFilter(size=3))
  128. binary_img = gray_img.point(lambda p: 255 if p > 128 else 0)
  129. buf = io.BytesIO()
  130. binary_img.save(buf, format="PNG")
  131. return self.ocr.classification(buf.getvalue())
  132. except Exception as e:
  133. return f"Error: {e}"