Skip to content

解密模型包到内存 - YoloDecryptModelEx

函数简介

解密到内存,各输出指针由插件分配。

接口名称

YoloDecryptModelEx

DLL 调用

int YoloDecryptModelEx(long ola, long packageData, int packageSize, string password, long* outModelData, long* outModelSize, long* outNcnnParamData, long* outNcnnParamSize, long* outLabelData, long* outLabelSize, int* modelType, int* inferenceType);

参数说明

参数名类型说明
ola长整数型OLAPlug 对象指针,由 CreateCOLAPlugInterFace 生成。
packageData长整数型加密包地址。
packageSize整数型包长度。
password字符串密码。
outModelData长整数型*明文主模型。
outModelSize长整数型*主模型长度。
outNcnnParamData长整数型*NCNN param。
outNcnnParamSize长整数型*param 长度。
outLabelData长整数型*类别名表。
outLabelSize长整数型*名表长度。
modelType整数型*输出:模型后端类型,见下表。
inferenceType整数型*输出:推理任务类型,见下表。

示例

以下示例演示 读入加密包 → 解密到内存 → 使用明文缓冲 → FreeMemoryPtr 释放各输出指针。无 NCNN param / 无 labels 时对应输出地址为 0。

SDK 调用

cpp
#include "OLAPlugServer.h"
#include <fstream>
#include <vector>

OLAPlugServer ola;

std::ifstream ifs("models/yolov8n.olam", std::ios::binary | std::ios::ate);
if (!ifs) return;
std::vector<unsigned char> pkgBuf((size_t)ifs.tellg());
ifs.seekg(0);
ifs.read((char*)pkgBuf.data(), pkgBuf.size());

long outModel = 0, outModelSize = 0;
long outParam = 0, outParamSize = 0;
long outLabel = 0, outLabelSize = 0;
int modelType = 0, inferenceType = 0;
int ret = ola.YoloDecryptModelEx(
    (long)pkgBuf.data(), (int)pkgBuf.size(), "pwd",
    outModel, outModelSize, outParam, outParamSize,
    outLabel, outLabelSize, modelType, inferenceType);
if (ret == 1) {
    // outModel / outModelSize 为明文主模型;可继续 YoloLoadModelMemoryEx
    if (outModel) ola.FreeMemoryPtr(outModel);
    if (outParam) ola.FreeMemoryPtr(outParam);
    if (outLabel) ola.FreeMemoryPtr(outLabel);
}
csharp
using System.IO;
using System.Runtime.InteropServices;
using OLAPlug;

var ola = new OLAPlugServer();
byte[] pkgBytes = File.ReadAllBytes(@"models\yolov8n.olam");
GCHandle pin = GCHandle.Alloc(pkgBytes, GCHandleType.Pinned);
try {
    int ret = ola.YoloDecryptModelEx(
        pin.AddrOfPinnedObject().ToInt64(), pkgBytes.Length, "pwd",
        out long outModel, out long outModelSize,
        out long outParam, out long outParamSize,
        out long outLabel, out long outLabelSize,
        out int modelType, out int inferenceType);
    if (ret == 1) {
        if (outModel != 0) ola.FreeMemoryPtr(outModel);
        if (outParam != 0) ola.FreeMemoryPtr(outParam);
        if (outLabel != 0) ola.FreeMemoryPtr(outLabel);
    }
} finally { pin.Free(); }
python
from OLAPlugServer import OLAPlugServer
import ctypes

ola = OLAPlugServer()
with open("models/yolov8n.olam", "rb") as f:
    pkg_bytes = f.read()
pkg_buf = ctypes.create_string_buffer(pkg_bytes, len(pkg_bytes))

ret, out_model, out_model_size, out_param, out_param_size, out_label, out_label_size, model_type, inference_type = ola.YoloDecryptModelEx(
    ctypes.addressof(pkg_buf), len(pkg_bytes), "pwd")
if ret == 1:
    if out_model:
        ola.FreeMemoryPtr(out_model)
    if out_param:
        ola.FreeMemoryPtr(out_param)
    if out_label:
        ola.FreeMemoryPtr(out_label)
java
import com.olaplug.OLAPlugServer;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import java.nio.file.Files;
import java.nio.file.Paths;

OLAPlugServer ola = new OLAPlugServer();
byte[] pkgBytes = Files.readAllBytes(Paths.get("models/yolov8n.olam"));
Memory pkgMem = new Memory(pkgBytes.length);
pkgMem.write(0, pkgBytes, 0, pkgBytes.length);

var result = ola.YoloDecryptModelEx(Pointer.nativeValue(pkgMem), pkgBytes.length, "pwd");
if (result != null && result.ret == 1) {
    if (result.outModelData != 0) ola.FreeMemoryPtr(result.outModelData);
    if (result.outNcnnParamData != 0) ola.FreeMemoryPtr(result.outNcnnParamData);
    if (result.outLabelData != 0) ola.FreeMemoryPtr(result.outLabelData);
}
go
import (
    "os"
    "unsafe"
    "github.com/ola/olaplug/olaplug"
)

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()

pkgBytes, err := os.ReadFile("models/yolov8n.olam")
if err != nil {
    return
}

var outModel, outModelSize, outParam, outParamSize, outLabel, outLabelSize int64
var modelType, inferenceType int32
ret := ola.YoloDecryptModelEx(
    uintptr(unsafe.Pointer(&pkgBytes[0])), int32(len(pkgBytes)), "pwd",
    &outModel, &outModelSize, &outParam, &outParamSize,
    &outLabel, &outLabelSize, &modelType, &inferenceType)
if ret == 1 {
    if outModel != 0 {
        ola.FreeMemoryPtr(outModel)
    }
    if outParam != 0 {
        ola.FreeMemoryPtr(outParam)
    }
    if outLabel != 0 {
        ola.FreeMemoryPtr(outLabel)
    }
}
rust
use olaplug::OLAPlugServer;
use std::fs;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let pkg_bytes = fs::read("models/yolov8n.olam").unwrap();

let mut out_model: i64 = 0;
let mut out_model_size: i64 = 0;
let mut out_param: i64 = 0;
let mut out_param_size: i64 = 0;
let mut out_label: i64 = 0;
let mut out_label_size: i64 = 0;
let mut model_type: i32 = 0;
let mut inference_type: i32 = 0;

let ret = ola.yolo_decrypt_model_ex(
    pkg_bytes.as_ptr() as i64, pkg_bytes.len() as i32, "pwd",
    &mut out_model, &mut out_model_size,
    &mut out_param, &mut out_param_size,
    &mut out_label, &mut out_label_size,
    &mut model_type, &mut inference_type);
if ret == 1 {
    if out_model != 0 { ola.free_memory_ptr(out_model); }
    if out_param != 0 { ola.free_memory_ptr(out_param); }
    if out_label != 0 { ola.free_memory_ptr(out_label); }
}
cpp
var ola = com("OlaPlug.OlaSoft")
var pkgSize = ola.GetFileSize("models/yolov8n.olam")
var pkgPtr = ola.ReadBytesFromFile("models/yolov8n.olam", 0, 0)
if(pkgPtr && pkgSize > 0) {
    var outModel = 0, outModelSize = 0
    var outParam = 0, outParamSize = 0
    var outLabel = 0, outLabelSize = 0
    var modelType = 0, inferenceType = 0
    var ret = ola.YoloDecryptModelEx(
        pkgPtr, pkgSize, "pwd",
        outModel, outModelSize, outParam, outParamSize,
        outLabel, outLabelSize, modelType, inferenceType)
    ola.FreeMemoryPtr(pkgPtr)
    if(ret == 1) {
        if(outModel) ola.FreeMemoryPtr(outModel)
        if(outParam) ola.FreeMemoryPtr(outParam)
        if(outLabel) ola.FreeMemoryPtr(outLabel)
    }
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
pkgSize = ola.GetFileSize("models/yolov8n.olam")
pkgPtr = ola.ReadBytesFromFile("models/yolov8n.olam", 0, 0)
If pkgPtr <> 0 And pkgSize > 0 Then
    outModel = 0 : outModelSize = 0
    outParam = 0 : outParamSize = 0
    outLabel = 0 : outLabelSize = 0
    modelType = 0 : inferenceType = 0
    ret = ola.YoloDecryptModelEx(pkgPtr, pkgSize, "pwd", outModel, outModelSize, outParam, outParamSize, outLabel, outLabelSize, modelType, inferenceType)
    ola.FreeMemoryPtr(pkgPtr)
    If ret = 1 Then
        If outModel <> 0 Then ola.FreeMemoryPtr(outModel)
        If outParam <> 0 Then ola.FreeMemoryPtr(outParam)
        If outLabel <> 0 Then ola.FreeMemoryPtr(outLabel)
    End If
End If
text
.局部变量 ola, OLAPlug
.局部变量 pkgData, 字节集
.局部变量 ret, 整数型
.局部变量 outModel, 长整数型
.局部变量 outModelSize, 长整数型
.局部变量 outParam, 长整数型
.局部变量 outParamSize, 长整数型
.局部变量 outLabel, 长整数型
.局部变量 outLabelSize, 长整数型
.局部变量 modelType, 整数型
.局部变量 inferenceType, 整数型

ola.创建 ()
pkgData = 读入文件 (“models/yolov8n.olam”, )
.如果真 (取字节集长度 (pkgData) > 0)
    ret = ola.YoloDecryptModelEx (
        取变量数据地址 (pkgData), 取字节集长度 (pkgData), “pwd”,
        outModel, outModelSize, outParam, outParamSize,
        outLabel, outLabelSize, modelType, inferenceType)
    .如果真 (ret = 1)
        .如果真 (outModel ≠ 0)
            ola.FreeMemoryPtr (outModel)
        .如果真结束
        .如果真 (outParam ≠ 0)
            ola.FreeMemoryPtr (outParam)
        .如果真结束
        .如果真 (outLabel ≠ 0)
            ola.FreeMemoryPtr (outLabel)
        .如果真结束
    .如果真结束
.如果真结束
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var pkgData = io.load("models/yolov8n.olam");
var outModel = 0, outModelSize = 0;
var outParam = 0, outParamSize = 0;
var outLabel = 0, outLabelSize = 0;
var modelType = 0, inferenceType = 0;
var ret = ola.YoloDecryptModelEx(
    pkgData, #pkgData, "pwd",
    outModel, outModelSize, outParam, outParamSize,
    outLabel, outLabelSize, modelType, inferenceType);
if(ret == 1){
    if(outModel) ola.FreeMemoryPtr(outModel);
    if(outParam) ola.FreeMemoryPtr(outParam);
    if(outLabel) ola.FreeMemoryPtr(outLabel);
}
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
变量 pkgData <类型 = 字节集类>

pkgData = 读入文件("models/yolov8n.olam", )
如果真 (取字节集长度(pkgData) > 0)
{
    长整数 outModel = 0, outModelSize = 0
    长整数 outParam = 0, outParamSize = 0
    长整数 outLabel = 0, outLabelSize = 0
    整数 modelType = 0, inferenceType = 0
    整数 ret = ola.YoloDecryptModelEx(
        取字节集指针(pkgData), 取字节集长度(pkgData), "pwd",
        outModel, outModelSize, outParam, outParamSize,
        outLabel, outLabelSize, modelType, inferenceType)
    如果真 (ret = 1)
    {
        如果真 (outModel ≠ 0) { ola.FreeMemoryPtr(outModel) }
        如果真 (outParam ≠ 0) { ola.FreeMemoryPtr(outParam) }
        如果真 (outLabel ≠ 0) { ola.FreeMemoryPtr(outLabel) }
    }
}
cpp
#include "OLAPlugServer.h"
#include <fstream>
#include <vector>

OLAPlugServer ola;
std::ifstream ifs("models/yolov8n.olam", std::ios::binary | std::ios::ate);
long pkgSize = (long)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> pkgBuf(pkgSize);
ifs.read((char*)pkgBuf.data(), pkgSize);

long outModel = 0, outModelSize = 0;
long outParam = 0, outParamSize = 0;
long outLabel = 0, outLabelSize = 0;
int modelType = 0, inferenceType = 0;
int ret = ola.YoloDecryptModelEx(
    (long)pkgBuf.data(), pkgSize, "pwd",
    outModel, outModelSize, outParam, outParamSize,
    outLabel, outLabelSize, modelType, inferenceType);
if (ret == 1) {
    if (outModel) ola.FreeMemoryPtr(outModel);
    if (outParam) ola.FreeMemoryPtr(outParam);
    if (outLabel) ola.FreeMemoryPtr(outLabel);
}

原生 DLL 调用

cpp
#include <fstream>
#include <vector>

long instance = CreateCOLAPlugInterFace();

std::ifstream ifs("models/yolov8n.olam", std::ios::binary | std::ios::ate);
auto pkgSize = (int)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> pkgBuf(pkgSize);
ifs.read((char*)pkgBuf.data(), pkgSize);

long outModel = 0, outModelSize = 0;
long outParam = 0, outParamSize = 0;
long outLabel = 0, outLabelSize = 0;
int modelType = 0, inferenceType = 0;
int ret = YoloDecryptModelEx(
    instance, (long)pkgBuf.data(), pkgSize, "pwd",
    &outModel, &outModelSize, &outParam, &outParamSize,
    &outLabel, &outLabelSize, &modelType, &inferenceType);
if (ret == 1) {
    if (outModel) FreeMemoryPtr(instance, outModel);
    if (outParam) FreeMemoryPtr(instance, outParam);
    if (outLabel) FreeMemoryPtr(instance, outLabel);
}
csharp
using System.IO;
using System.Runtime.InteropServices;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int YoloDecryptModelEx(long ola, long packageData, int packageSize, string password,
    out long outModelData, out long outModelSize,
    out long outNcnnParamData, out long outNcnnParamSize,
    out long outLabelData, out long outLabelSize,
    out int modelType, out int inferenceType);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeMemoryPtr(long ola, long ptr);

long instance = CreateCOLAPlugInterFace();
byte[] pkgBytes = File.ReadAllBytes(@"models\yolov8n.olam");
GCHandle pin = GCHandle.Alloc(pkgBytes, GCHandleType.Pinned);
try {
    int ret = YoloDecryptModelEx(instance,
        pin.AddrOfPinnedObject().ToInt64(), pkgBytes.Length, "pwd",
        out long outModel, out long outModelSize,
        out long outParam, out long outParamSize,
        out long outLabel, out long outLabelSize,
        out int modelType, out int inferenceType);
    if (ret == 1) {
        if (outModel != 0) FreeMemoryPtr(instance, outModel);
        if (outParam != 0) FreeMemoryPtr(instance, outParam);
        if (outLabel != 0) FreeMemoryPtr(instance, outLabel);
    }
} finally { pin.Free(); }
python
from ctypes import CDLL, c_int, c_int64, byref, create_string_buffer, addressof

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
ola.YoloDecryptModelEx.restype = c_int
instance = ola.CreateCOLAPlugInterFace()

with open("models/yolov8n.olam", "rb") as f:
    pkg_bytes = f.read()
buf = create_string_buffer(pkg_bytes, len(pkg_bytes))

out_model = c_int64(0)
out_model_size = c_int64(0)
out_param = c_int64(0)
out_param_size = c_int64(0)
out_label = c_int64(0)
out_label_size = c_int64(0)
model_type = c_int(0)
inference_type = c_int(0)
ret = ola.YoloDecryptModelEx(
    instance, c_int64(addressof(buf)), len(pkg_bytes), b"pwd",
    byref(out_model), byref(out_model_size),
    byref(out_param), byref(out_param_size),
    byref(out_label), byref(out_label_size),
    byref(model_type), byref(inference_type))
if ret == 1:
    if out_model.value:
        ola.FreeMemoryPtr(instance, out_model)
    if out_param.value:
        ola.FreeMemoryPtr(instance, out_param)
    if out_label.value:
        ola.FreeMemoryPtr(instance, out_label)

返回值

返回值说明
1成功。
负数失败;绝对值为错误码。与 GetLastError() 一致。

完整错误码表见 YOLO 错误码说明

注意事项

项目说明
模块权限需要插件已开通 YOLO 模块权限(Reg、Login的FeatureList中包含YOLO特性)。
释放内存插件分配的内存须调用 FreeMemoryPtr 释放。

modelType 模型后端类型

含义
0TensorRT Engine(.engine)
1ONNX(.onnx)
2NCNN(.bin + .param 双文件)

inferenceType 推理任务类型

含义
0Detect 目标检测
1Classify 图像分类
2Segment 实例分割
3Pose 姿态估计
4Obb 旋转框检测