Skip to content

加密模型包到内存 - YoloEncryptModelEx

函数简介

加密并输出整包到内存。

接口名称

YoloEncryptModelEx

DLL 调用

int YoloEncryptModelEx(long ola, long modelData, int modelSize, long ncnnParamData, int ncnnParamSize, long labelData, int labelSize, string password, int modelType, int inferenceType, long* outPackageData, long* outPackageSize);

参数说明

参数名类型说明
ola长整数型OLAPlug 对象指针,由 CreateCOLAPlugInterFace 生成。
modelData长整数型主模型地址。
modelSize整数型主模型长度。
ncnnParamData长整数型NCNN param 地址。
ncnnParamSize整数型param 长度。
labelData长整数型类别名称表内存地址;无则 0。原始字节,编码见 类别名称表说明
labelSize整数型类别名称表字节长度;无则 0。
password字符串密码。
modelType整数型模型后端类型,见下表。
inferenceType整数型推理任务类型,见下表。
outPackageData长整数型*输出:整包地址。
outPackageSize长整数型*输出:包总长度。

示例

以下示例演示 读入主模型(及可选 labels)→ 加密到内存 → 写盘或继续加载 → FreeMemoryPtr 释放输出包。以 ONNX 为例;NCNN 需额外传入 .param 缓冲且 modelType=2

SDK 调用

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

OLAPlugServer ola;

auto readFile = [](const char* path, std::vector<unsigned char>& out) -> bool {
    std::ifstream ifs(path, std::ios::binary | std::ios::ate);
    if (!ifs) return false;
    out.resize((size_t)ifs.tellg());
    ifs.seekg(0);
    ifs.read((char*)out.data(), out.size());
    return true;
};

std::vector<unsigned char> modelBuf, labelBuf;
if (!readFile("models/yolov8n.onnx", modelBuf)) return;
readFile("models/coco.names", labelBuf);  // 可选

long outPkg = 0, outSize = 0;
int ret = ola.YoloEncryptModelEx(
    (long)modelBuf.data(), (int)modelBuf.size(),
    0, 0,
    labelBuf.empty() ? 0 : (long)labelBuf.data(), (int)labelBuf.size(),
    "pwd", 1, 0,  // ONNX, Detect
    outPkg, outSize);
if (ret == 1 && outPkg != 0) {
    ola.WriteBytesToFile("models/yolov8n.olam", outPkg, (int)outSize);
    ola.FreeMemoryPtr(outPkg);
}
csharp
using System.IO;
using System.Runtime.InteropServices;
using OLAPlug;

var ola = new OLAPlugServer();
byte[] modelBytes = File.ReadAllBytes(@"models\yolov8n.onnx");
byte[] labelBytes = File.Exists(@"models\coco.names")
    ? File.ReadAllBytes(@"models\coco.names") : Array.Empty<byte>();

GCHandle pinModel = GCHandle.Alloc(modelBytes, GCHandleType.Pinned);
GCHandle? pinLabel = labelBytes.Length > 0
    ? GCHandle.Alloc(labelBytes, GCHandleType.Pinned) : null;
try {
    long labelAddr = pinLabel?.AddrOfPinnedObject().ToInt64() ?? 0;
    int ret = ola.YoloEncryptModelEx(
        pinModel.AddrOfPinnedObject().ToInt64(), modelBytes.Length,
        0, 0, labelAddr, labelBytes.Length,
        "pwd", 1, 0,
        out long outPkg, out long outSize);
    if (ret == 1 && outPkg != 0) {
        ola.WriteBytesToFile(@"models\yolov8n.olam", outPkg, (int)outSize);
        ola.FreeMemoryPtr(outPkg);
    }
} finally {
    pinModel.Free();
    pinLabel?.Free();
}
python
from OLAPlugServer import OLAPlugServer
import ctypes
import os

ola = OLAPlugServer()
with open("models/yolov8n.onnx", "rb") as f:
    model_bytes = f.read()
model_buf = ctypes.create_string_buffer(model_bytes, len(model_bytes))

label_addr, label_size = 0, 0
if os.path.exists("models/coco.names"):
    with open("models/coco.names", "rb") as f:
        label_bytes = f.read()
    label_buf = ctypes.create_string_buffer(label_bytes, len(label_bytes))
    label_addr = ctypes.addressof(label_buf)
    label_size = len(label_bytes)

ret, out_pkg, out_size = ola.YoloEncryptModelEx(
    ctypes.addressof(model_buf), len(model_bytes),
    0, 0, label_addr, label_size,
    "pwd", 1, 0)
if ret == 1 and out_pkg:
    ola.WriteBytesToFile("models/yolov8n.olam", out_pkg, out_size)
    ola.FreeMemoryPtr(out_pkg)
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[] modelBytes = Files.readAllBytes(Paths.get("models/yolov8n.onnx"));
Memory modelMem = new Memory(modelBytes.length);
modelMem.write(0, modelBytes, 0, modelBytes.length);

long labelAddr = 0; int labelSize = 0;
if (Files.exists(Paths.get("models/coco.names"))) {
    byte[] labelBytes = Files.readAllBytes(Paths.get("models/coco.names"));
    Memory labelMem = new Memory(labelBytes.length);
    labelMem.write(0, labelBytes, 0, labelBytes.length);
    labelAddr = Pointer.nativeValue(labelMem);
    labelSize = labelBytes.length;
}

var result = ola.YoloEncryptModelEx(
    Pointer.nativeValue(modelMem), modelBytes.length,
    0, 0, labelAddr, labelSize, "pwd", 1, 0);
if (result != null && result.ret == 1 && result.outPackageData != 0) {
    ola.WriteBytesToFile("models/yolov8n.olam", result.outPackageData, (int)result.outPackageSize);
    ola.FreeMemoryPtr(result.outPackageData);
}
go
import (
    "os"
    "unsafe"
    "github.com/ola/olaplug/olaplug"
)

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

modelBytes, err := os.ReadFile("models/yolov8n.onnx")
if err != nil {
    return
}
labelBytes, _ := os.ReadFile("models/coco.names")

var labelAddr uintptr
var labelSize int32
if len(labelBytes) > 0 {
    labelAddr = uintptr(unsafe.Pointer(&labelBytes[0]))
    labelSize = int32(len(labelBytes))
}

var outPkg, outSize int64
ret := ola.YoloEncryptModelEx(
    uintptr(unsafe.Pointer(&modelBytes[0])), int32(len(modelBytes)),
    0, 0, labelAddr, labelSize,
    "pwd", 1, 0, &outPkg, &outSize)
if ret == 1 && outPkg != 0 {
    ola.WriteBytesToFile("models/yolov8n.olam", outPkg, int32(outSize))
    ola.FreeMemoryPtr(outPkg)
}
rust
use olaplug::OLAPlugServer;
use std::fs;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let model_bytes = fs::read("models/yolov8n.onnx").unwrap();
let label_bytes = fs::read("models/coco.names").unwrap_or_default();

let (label_addr, label_size) = if label_bytes.is_empty() {
    (0i64, 0i32)
} else {
    (label_bytes.as_ptr() as i64, label_bytes.len() as i32)
};

let mut out_pkg: i64 = 0;
let mut out_size: i64 = 0;
let ret = ola.yolo_encrypt_model_ex(
    model_bytes.as_ptr() as i64, model_bytes.len() as i32,
    0, 0, label_addr, label_size,
    "pwd", 1, 0, &mut out_pkg, &mut out_size);
if ret == 1 && out_pkg != 0 {
    ola.write_bytes_to_file("models/yolov8n.olam", out_pkg, out_size as i32);
    ola.free_memory_ptr(out_pkg);
}
cpp
var ola = com("OlaPlug.OlaSoft")
var modelSize = ola.GetFileSize("models/yolov8n.onnx")
var modelPtr = ola.ReadBytesFromFile("models/yolov8n.onnx", 0, 0)
var labelPtr = ola.ReadBytesFromFile("models/coco.names", 0, 0)
var labelSize = ola.GetFileSize("models/coco.names")
if(modelPtr && modelSize > 0) {
    var outPkg = 0, outSize = 0
    var ret = ola.YoloEncryptModelEx(
        modelPtr, modelSize, 0, 0, labelPtr, labelSize,
        "pwd", 1, 0, outPkg, outSize)
    ola.FreeMemoryPtr(modelPtr)
    if(labelPtr) ola.FreeMemoryPtr(labelPtr)
    if(ret == 1 && outPkg) {
        ola.WriteBytesToFile("models/yolov8n.olam", outPkg, outSize)
        ola.FreeMemoryPtr(outPkg)
    }
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
modelSize = ola.GetFileSize("models/yolov8n.onnx")
modelPtr = ola.ReadBytesFromFile("models/yolov8n.onnx", 0, 0)
labelPtr = ola.ReadBytesFromFile("models/coco.names", 0, 0)
labelSize = ola.GetFileSize("models/coco.names")
If modelPtr <> 0 And modelSize > 0 Then
    outPkg = 0
    outSize = 0
    ret = ola.YoloEncryptModelEx(modelPtr, modelSize, 0, 0, labelPtr, labelSize, "pwd", 1, 0, outPkg, outSize)
    ola.FreeMemoryPtr(modelPtr)
    If labelPtr <> 0 Then ola.FreeMemoryPtr(labelPtr)
    If ret = 1 And outPkg <> 0 Then
        ola.WriteBytesToFile "models/yolov8n.olam", outPkg, outSize
        ola.FreeMemoryPtr(outPkg)
    End If
End If
text
.局部变量 ola, OLAPlug
.局部变量 modelData, 字节集
.局部变量 labelData, 字节集
.局部变量 ret, 整数型
.局部变量 outPkg, 长整数型
.局部变量 outSize, 长整数型

ola.创建 ()
modelData = 读入文件 (“models/yolov8n.onnx”, )
.如果真 (取字节集长度 (modelData) > 0)
    labelData = 读入文件 (“models/coco.names”, )
    ret = ola.YoloEncryptModelEx (
        取变量数据地址 (modelData), 取字节集长度 (modelData),
        0, 0,
        选择 (取字节集长度 (labelData) > 0, 取变量数据地址 (labelData), 0),
        取字节集长度 (labelData),
        “pwd”, 1, 0, outPkg, outSize)
    .如果真 (ret = 1 且 outPkg ≠ 0)
        ola.WriteBytesToFile (“models/yolov8n.olam”, outPkg, outSize)
        ola.FreeMemoryPtr (outPkg)
    .如果真结束
.如果真结束
' modelData / labelData 在调用完成前须保持有效
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var modelData = io.load("models/yolov8n.onnx");
var labelData = io.load("models/coco.names");
var labelAddr = labelData ? labelData : 0;
var labelSize = labelData ? #labelData : 0;
var outPkg = 0, outSize = 0;
var ret = ola.YoloEncryptModelEx(
    modelData, #modelData, 0, 0, labelAddr, labelSize,
    "pwd", 1, 0, outPkg, outSize);
if(ret == 1 && outPkg){
    ola.WriteBytesToFile("models/yolov8n.olam", outPkg, outSize);
    ola.FreeMemoryPtr(outPkg);
}
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
变量 modelData <类型 = 字节集类>
变量 labelData <类型 = 字节集类>

modelData = 读入文件("models/yolov8n.onnx", )
如果真 (取字节集长度(modelData) > 0)
{
    labelData = 读入文件("models/coco.names", )
    长整数 outPkg = 0, outSize = 0
    整数 ret = ola.YoloEncryptModelEx(
        取字节集指针(modelData), 取字节集长度(modelData),
        0, 0,
        选择(取字节集长度(labelData) > 0, 取字节集指针(labelData), 0),
        取字节集长度(labelData),
        "pwd", 1, 0, outPkg, outSize)
    如果真 (ret = 1 且 outPkg ≠ 0)
    {
        ola.WriteBytesToFile("models/yolov8n.olam", outPkg, outSize)
        ola.FreeMemoryPtr(outPkg)
    }
}
cpp
#include "OLAPlugServer.h"
#include <fstream>
#include <vector>

OLAPlugServer ola;
std::ifstream mfs("models/yolov8n.onnx", std::ios::binary | std::ios::ate);
long modelSize = (long)mfs.tellg();
mfs.seekg(0);
std::vector<unsigned char> modelBuf(modelSize);
mfs.read((char*)modelBuf.data(), modelSize);

std::vector<unsigned char> labelBuf;
std::ifstream lfs("models/coco.names", std::ios::binary | std::ios::ate);
if (lfs) {
    labelBuf.resize((size_t)lfs.tellg());
    lfs.seekg(0);
    lfs.read((char*)labelBuf.data(), labelBuf.size());
}

long outPkg = 0, outSize = 0;
int ret = ola.YoloEncryptModelEx(
    (long)modelBuf.data(), modelSize, 0, 0,
    labelBuf.empty() ? 0 : (long)labelBuf.data(), (int)labelBuf.size(),
    "pwd", 1, 0, outPkg, outSize);
if (ret == 1 && outPkg != 0) {
    ola.WriteBytesToFile("models/yolov8n.olam", outPkg, (int)outSize);
    ola.FreeMemoryPtr(outPkg);
}

原生 DLL 调用

cpp
#include <fstream>
#include <vector>

long instance = CreateCOLAPlugInterFace();

std::ifstream mfs("models/yolov8n.onnx", std::ios::binary | std::ios::ate);
auto modelSize = (int)mfs.tellg();
mfs.seekg(0);
std::vector<unsigned char> modelBuf(modelSize);
mfs.read((char*)modelBuf.data(), modelSize);

long outPkg = 0, outSize = 0;
int ret = YoloEncryptModelEx(
    instance, (long)modelBuf.data(), modelSize, 0, 0, 0, 0,
    "pwd", 1, 0, &outPkg, &outSize);
if (ret == 1 && outPkg != 0) {
    WriteBytesToFile(instance, "models/yolov8n.olam", outPkg, (int)outSize);
    FreeMemoryPtr(instance, outPkg);
}
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 YoloEncryptModelEx(long ola, long modelData, int modelSize,
    long ncnnParamData, int ncnnParamSize, long labelData, int labelSize,
    string password, int modelType, int inferenceType,
    out long outPackageData, out long outPackageSize);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int WriteBytesToFile(long ola, string filePath, long dataAddr, int dataSize);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeMemoryPtr(long ola, long ptr);

long instance = CreateCOLAPlugInterFace();
byte[] modelBytes = File.ReadAllBytes(@"models\yolov8n.onnx");
GCHandle pin = GCHandle.Alloc(modelBytes, GCHandleType.Pinned);
try {
    int ret = YoloEncryptModelEx(instance,
        pin.AddrOfPinnedObject().ToInt64(), modelBytes.Length,
        0, 0, 0, 0, "pwd", 1, 0, out long outPkg, out long outSize);
    if (ret == 1 && outPkg != 0) {
        WriteBytesToFile(instance, @"models\yolov8n.olam", outPkg, (int)outSize);
        FreeMemoryPtr(instance, outPkg);
    }
} 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.YoloEncryptModelEx.restype = c_int
instance = ola.CreateCOLAPlugInterFace()

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

out_pkg = c_int64(0)
out_size = c_int64(0)
ret = ola.YoloEncryptModelEx(
    instance, c_int64(addressof(buf)), len(model_bytes),
    0, 0, 0, 0, b"pwd", 1, 0, byref(out_pkg), byref(out_size))
if ret == 1 and out_pkg.value:
    ola.WriteBytesToFile(instance, b"models/yolov8n.olam", out_pkg, c_int(out_size.value))
    ola.FreeMemoryPtr(instance, out_pkg)

返回值

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

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

注意事项

项目说明
模块权限需要插件已开通 YOLO 模块权限(Reg、Login的FeatureList中包含YOLO特性)。
包内 names 归一化为 UTF-8 存储包内 names 归一化为 UTF-8 存储;labelData 编码规则见 类别名称表说明
释放内存插件分配的内存须调用 FreeMemoryPtr 释放。

modelType 模型后端类型

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

inferenceType 推理任务类型

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