소스 검색

refactor: replace PyTorch OCR engine with lightweight OnnxEngine

jerry 1 개월 전
부모
커밋
5fa82afa44
3개의 변경된 파일과 111개의 추가작업 그리고 75개의 파일을 삭제
  1. 1 1
      config/test_conf.yaml
  2. 1 3
      requirements.txt
  3. 109 71
      toolkit/ocr_engine.py

+ 1 - 1
config/test_conf.yaml

@@ -149,7 +149,7 @@ plugins:
       weight: 10
     free_config:
       domain: ireland.blsspainglobal.com
-      ocr_model: data/ctc.pth
+      ocr_model: data/ctc.onnx
       apt_configs:
         slot.dub.es.tourist:
           location: Dublin

+ 1 - 3
requirements.txt

@@ -5,7 +5,6 @@ cryptography
 curl-cffi>=0.7.0
 openai
 ddddocr
-easyocr
 fastapi
 numpy
 pydantic
@@ -15,5 +14,4 @@ psutil
 loguru
 pyyaml
 # mitmproxy>=10.0.0
-torch --index-url https://download.pytorch.org/whl/cpu
-torchvision --index-url https://download.pytorch.org/whl/cpu
+onnxruntime

+ 109 - 71
toolkit/ocr_engine.py

@@ -1,78 +1,55 @@
 import os
-
-# Suppress ONNX Runtime warnings
-os.environ["ORT_LOGGING_LEVEL"] = "3"
 import string
 import io
-import torch
-import ddddocr
-from torch import nn
-from collections import OrderedDict
-from torchvision import transforms
-from PIL import Image, ImageFilter
 from io import BytesIO
+import numpy as np
+from PIL import Image, ImageFilter
+import ddddocr
 
-# ================= PyTorch Model Architecture =================
-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
-
-# ================= Engine Wrapper =================
-class PyTorchEngine:
-    def __init__(self, model_path):
+# 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.model = Model(self.num_classes, input_shape=(3, self.hight, self.width))
         self.ready = False
-        self.transforms_func = transforms.Compose([
-            transforms.Resize((self.hight, self.width)),
-            transforms.ToTensor()
-        ])
-        if os.path.exists(model_path):
-            self.model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
-            self.model.eval()
+        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"[PyTorch] Loaded: {model_path}")
+            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])
@@ -85,21 +62,82 @@ class PyTorchEngine:
         return ''.join([x for x in s if x != self.characters[0]])
 
     def inference_bytes(self, image_bytes):
-        if not self.ready: return "Error: Model not loaded"
+        if not self.ready or self.session is None:
+            return "Error: Model not loaded"
         try:
-            image = Image.open(BytesIO(image_bytes)).convert('RGB')
-            image = self.transforms_func(image)
-            with torch.no_grad():
-                output = self.model(image.unsqueeze(0).cpu())
-            output_argmax = output.detach().permute(1, 0, 2).argmax(dim=-1)
+            # 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: