import os import string import io from io import BytesIO import numpy as np from PIL import Image, ImageFilter import ddddocr # Suppress ONNX Runtime warnings os.environ["ORT_LOGGING_LEVEL"] = "3" try: import onnxruntime as ort except ImportError: ort = None # ================= ONNX OCR Engine (Lightweight, No PyTorch/NVIDIA required) ================= class OnnxEngine: def __init__(self, model_path: str = None): self.num_classes = 12 self.characters = '-' + string.digits + '$' self.width = 150 self.hight = 80 self.ready = False self.session = None self.input_name = None if not model_path: # Default to data/ctc.onnx if available default_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data", "ctc.onnx") if os.path.exists(default_path): model_path = default_path # If .pth is provided, check for sibling .onnx file if model_path and model_path.endswith('.pth'): candidate = model_path[:-4] + '.onnx' if os.path.exists(candidate): model_path = candidate if model_path and os.path.exists(model_path): if ort is None: print(f"[OnnxEngine] Warning: onnxruntime not installed, cannot load {model_path}") return opts = ort.SessionOptions() opts.log_severity_level = 3 self.session = ort.InferenceSession(model_path, sess_options=opts, providers=['CPUExecutionProvider']) self.input_name = self.session.get_inputs()[0].name self.ready = True print(f"[OnnxEngine] Loaded: {model_path}") else: print(f"[OnnxEngine] Model file not found: {model_path}") def decode(self, sequence): a = ''.join([self.characters[x] for x in sequence]) s = [] last = None for x in a: if x != last: s.append(x) last = x return ''.join([x for x in s if x != self.characters[0]]) def inference_bytes(self, image_bytes): if not self.ready or self.session is None: return "Error: Model not loaded" try: # Preprocessing equivalent to torchvision transforms (Resize((80, 150)), ToTensor()) image = Image.open(BytesIO(image_bytes)).convert('RGB').resize((self.width, self.hight)) img_np = np.array(image, dtype=np.float32) / 255.0 img_np = np.transpose(img_np, (2, 0, 1)) # (3, 80, 150) img_np = np.expand_dims(img_np, axis=0) # (1, 3, 80, 150) ort_outs = self.session.run(None, {self.input_name: img_np})[0] output_argmax = np.argmax(np.transpose(ort_outs, (1, 0, 2)), axis=-1) return self.decode(output_argmax[0]) except Exception as e: return f"Error: {str(e)}" # Backwards compatibility alias for plugins importing PyTorchEngine PyTorchEngine = OnnxEngine # Optional: Legacy PyTorch Model if torch is installed in dev environment try: import torch from torch import nn from collections import OrderedDict from torchvision import transforms class Model(nn.Module): def __init__(self, n_classes, input_shape=(3, 64, 128)): super(Model, self).__init__() self.input_shape = input_shape channels = [32, 64, 128, 256, 256] layers = [2, 2, 2, 2, 2] kernels = [3, 3, 3, 3, 3] pools = [2, 2, 2, 2, (2, 1)] modules = OrderedDict() def cba(name, in_channels, out_channels, kernel_size): modules[f'conv{name}'] = nn.Conv2d(in_channels, out_channels, kernel_size, padding=(1, 1) if kernel_size == 3 else 0) modules[f'bn{name}'] = nn.BatchNorm2d(out_channels) modules[f'relu{name}'] = nn.ReLU(inplace=True) last_channel = 3 for block, (n_channel, n_layer, n_kernel, k_pool) in enumerate(zip(channels, layers, kernels, pools)): for layer in range(1, n_layer + 1): cba(f'{block+1}{layer}', last_channel, n_channel, n_kernel) last_channel = n_channel modules[f'pool{block + 1}'] = nn.MaxPool2d(k_pool) modules[f'dropout'] = nn.Dropout(0.25, inplace=True) self.cnn = nn.Sequential(modules) self.lstm = nn.LSTM(input_size=self.infer_features(), hidden_size=128, num_layers=2, bidirectional=True) self.fc = nn.Linear(in_features=256, out_features=n_classes) def infer_features(self): x = torch.zeros((1,)+self.input_shape) x = self.cnn(x) x = x.reshape(x.shape[0], -1, x.shape[-1]) return x.shape[1] def forward(self, x): x = self.cnn(x) x = x.reshape(x.shape[0], -1, x.shape[-1]) x = x.permute(2, 0, 1) x, _ = self.lstm(x) x = self.fc(x) return x except ImportError: Model = None # ================= ddddocr Engine ================= class DddOcrEngine: def __init__(self): self.ocr = ddddocr.DdddOcr(show_ad=False, beta=True) def inference_bytes(self, image_bytes): try: return self.ocr.classification(image_bytes) except Exception as e: return f"Error: {e}" def inference_captcha(self, image_bytes): try: image = Image.open(io.BytesIO(image_bytes)) gray_img = image.convert("L").filter(ImageFilter.MedianFilter(size=3)) binary_img = gray_img.point(lambda p: 255 if p > 128 else 0) buf = io.BytesIO() binary_img.save(buf, format="PNG") return self.ocr.classification(buf.getvalue()) except Exception as e: return f"Error: {e}"