split to 2 files: tensorrt_engine and onnx engine

This commit is contained in:
Alex Bezdieniezhnykh
2025-04-30 19:33:59 +03:00
parent 4bca61d859
commit a1ee077e0a
12 changed files with 227 additions and 206 deletions
+13 -5
View File
@@ -2,6 +2,7 @@ import json
import mimetypes
import os
import subprocess
import sys
import time
import cv2
@@ -11,10 +12,15 @@ cimport constants
from remote_command cimport RemoteCommand
from annotation cimport Detection, Annotation
from ai_config cimport AIRecognitionConfig
from inference_engine cimport OnnxEngine, TensorRTEngine
from hardware_service cimport HardwareService
from security cimport Security
if HardwareService.has_nvidia_gpu():
from tensorrt_engine cimport TensorRTEngine
else:
from onnx_engine import OnnxEngine
cdef class Inference:
def __init__(self, api_client, on_annotation):
self.api_client = api_client
@@ -31,17 +37,20 @@ cdef class Inference:
if not is_nvidia:
return
engine_filename = TensorRTEngine.get_engine_filename()
engine_filename = TensorRTEngine.get_engine_filename(0)
key = Security.get_model_encryption_key()
models_dir = constants.MODELS_FOLDER
if not os.path.exists(os.path.join(<str> models_dir, f'{engine_filename}.big')):
#TODO: Check cdn on engine exists, if there is, download
self.is_building_engine = True
time.sleep(5) # prevent simultaneously loading dll and models
time.sleep(8) # prevent simultaneously loading dll and models
onnx_model = self.api_client.load_big_small_resource(constants.AI_ONNX_MODEL_FILE, models_dir, key)
model_bytes = TensorRTEngine.convert_from_onnx(onnx_model)
self.api_client.upload_big_small_resource(model_bytes, <str> engine_filename, models_dir, key)
print('uploaded ')
self.is_building_engine = False
else:
print('tensor rt engine is here, no need to build')
cdef init_ai(self):
@@ -54,10 +63,9 @@ cdef class Inference:
if is_nvidia:
while self.is_building_engine:
time.sleep(1)
engine_filename = TensorRTEngine.get_engine_filename()
engine_filename = TensorRTEngine.get_engine_filename(0)
model_bytes = self.api_client.load_big_small_resource(engine_filename, models_dir, key)
self.engine = TensorRTEngine(model_bytes)
else:
model_bytes = self.api_client.load_big_small_resource(constants.AI_ONNX_MODEL_FILE, models_dir, key)
self.engine = OnnxEngine(model_bytes)