Skip to content

从内存加载明文模型 - YoloLoadModelMemoryEx

函数简介

从内存加载明文主模型,可选 NCNN .param 与类别名表。

接口名称

YoloLoadModelMemoryEx

DLL 调用

long YoloLoadModelMemoryEx(long ola, long modelData, int modelSize, long ncnnParamData, int ncnnParamSize, long labelData, int labelSize, int modelType, int inferenceType, int inferenceDevice);

参数说明

参数名类型说明
ola长整数型OLAPlug 对象指针,由 CreateCOLAPlugInterFace 生成。
modelData长整数型主模型内存地址。
modelSize整数型主模型字节数。
ncnnParamData长整数型NCNN .param 地址,无则 0。
ncnnParamSize整数型NCNN .param 长度。
labelData长整数型类别名称表内存地址;无则 0。传原始文件字节,编码自动识别,见 类别名称表说明
labelSize整数型类别名称表字节长度(精确字节数,不含多余 \0);无则 0。
modelType整数型模型后端类型,见下表。
inferenceType整数型推理任务类型,见下表。
inferenceDevice整数型inferenceDevice-1 表示 CPU;0 及以上表示 GPU 索引。GPU 不可用时可能自动回退 CPU(以实际 ExecutionProvider 为准)。

示例

以下示例演示 读入主模型(及可选 labels)→ 取得内存地址 → 从内存加载 → 释放模型句柄 的完整流程。以 ONNX 单文件为例;NCNN 需额外传入 .param 缓冲。

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 handle = ola.YoloLoadModelMemoryEx(
    (long)modelBuf.data(), (int)modelBuf.size(),
    0, 0,
    labelBuf.empty() ? 0 : (long)labelBuf.data(), (int)labelBuf.size(),
    1, 0, -1);  // ONNX, Detect, CPU
if (handle != 0) {
    ola.YoloReleaseModel(handle);
}
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;
    long handle = ola.YoloLoadModelMemoryEx(
        pinModel.AddrOfPinnedObject().ToInt64(), modelBytes.Length,
        0, 0,
        labelAddr, labelBytes.Length,
        1, 0, -1);
    if (handle != 0) ola.YoloReleaseModel(handle);
} 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)

handle = ola.YoloLoadModelMemoryEx(
    ctypes.addressof(model_buf), len(model_bytes),
    0, 0,
    label_addr, label_size,
    1, 0, -1)
if handle:
    ola.YoloReleaseModel(handle)
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;
}

long handle = ola.YoloLoadModelMemoryEx(
    Pointer.nativeValue(modelMem), modelBytes.length,
    0, 0, labelAddr, labelSize, 1, 0, -1);
if (handle != 0) ola.YoloReleaseModel(handle);
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
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);  // 可选

handle := ola.YoloLoadModelMemoryEx(
    (long)modelBuf.data(), (int)modelBuf.size(),
    0, 0,
    labelBuf.empty() ? 0 : (long)labelBuf.data(), (int)labelBuf.size(),
    1, 0, -1);  // ONNX, Detect, CPU
if handle != 0 {
    ola.YoloReleaseModel(handle)
}
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
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);  // 可选

let handle = ola.yolo_load_model_memory_ex((long)modelBuf.data(), (int)modelBuf.size(),;
    0, 0,
    labelBuf.empty() ? 0 : (long)labelBuf.data(), (int)labelBuf.size(),
    1, 0, -1);  // ONNX, Detect, CPU
if handle != 0 {
    ola.yolo_release_model(handle);
}
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 handle = ola.YoloLoadModelMemoryEx(modelPtr, modelSize, 0, 0, labelPtr, labelSize, 1, 0, -1)
    ola.FreeMemoryPtr(modelPtr)
    if(labelPtr) ola.FreeMemoryPtr(labelPtr)
    if(handle) ola.YoloReleaseModel(handle)
}
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
    handle = ola.YoloLoadModelMemoryEx(modelPtr, modelSize, 0, 0, labelPtr, labelSize, 1, 0, -1)
    ola.FreeMemoryPtr(modelPtr)
    If labelPtr <> 0 Then ola.FreeMemoryPtr(labelPtr)
    If handle <> 0 Then ola.YoloReleaseModel(handle)
End If
text
.局部变量 ola, OLAPlug
.局部变量 modelData, 字节集
.局部变量 labelData, 字节集
.局部变量 handle, 长整数型

ola.创建 ()
' ① 读入主模型到字节集
modelData = 读入文件 (“models/yolov8n.onnx”, )
.如果真 (取字节集长度 (modelData) > 0)
    ' ② 可选:读入类别名表
    labelData = 读入文件 (“models/coco.names”, )
    ' ③ 取变量数据地址 → 内存首地址
    handle = ola.YoloLoadModelMemoryEx (
        取变量数据地址 (modelData), 取字节集长度 (modelData),
        0, 0,
        选择 (取字节集长度 (labelData) > 0, 取变量数据地址 (labelData), 0),
        取字节集长度 (labelData),
        1, 0, -1)
    .如果真 (handle ≠ 0)
        ola.YoloReleaseModel (handle)
    .如果真结束
.如果真结束
' 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 handle = ola.YoloLoadModelMemoryEx(
    modelData, #modelData, 0, 0, labelAddr, labelSize, 1, 0, -1);
if(handle) ola.YoloReleaseModel(handle);
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
变量 modelData <类型 = 字节集类>
变量 labelData <类型 = 字节集类>

' ① 读入主模型
modelData = 读入文件("models/yolov8n.onnx", )
如果真 (取字节集长度(modelData) > 0)
{
    ' ② 可选:读入类别名表
    labelData = 读入文件("models/coco.names", )
    ' ③ 取字节集指针 → 内存首地址
    长整数 handle = ola.YoloLoadModelMemoryEx(
        取字节集指针(modelData), 取字节集长度(modelData),
        0, 0,
        选择(取字节集长度(labelData) > 0, 取字节集指针(labelData), 0),
        取字节集长度(labelData),
        1, 0, -1)
    如果真 (handle ≠ 0)
    {
        ola.YoloReleaseModel(handle)
    }
}
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 handle = ola.YoloLoadModelMemoryEx(
    (long)modelBuf.data(), modelSize, 0, 0,
    labelBuf.empty() ? 0 : (long)labelBuf.data(), (int)labelBuf.size(),
    1, 0, -1);
if (handle != 0) ola.YoloReleaseModel(handle);

原生 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 handle = YoloLoadModelMemoryEx(
    instance, (long)modelBuf.data(), modelSize, 0, 0, 0, 0, 1, 0, -1);
if (handle != 0) YoloReleaseModel(instance, handle);
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 long YoloLoadModelMemoryEx(long ola, long modelData, int modelSize,
    long ncnnParamData, int ncnnParamSize, long labelData, int labelSize,
    int modelType, int inferenceType, int inferenceDevice);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int YoloReleaseModel(long ola, long handle);

long instance = CreateCOLAPlugInterFace();
byte[] modelBytes = File.ReadAllBytes(@"models\yolov8n.onnx");
GCHandle pin = GCHandle.Alloc(modelBytes, GCHandleType.Pinned);
try {
    long handle = YoloLoadModelMemoryEx(instance,
        pin.AddrOfPinnedObject().ToInt64(), modelBytes.Length,
        0, 0, 0, 0, 1, 0, -1);
    if (handle != 0) YoloReleaseModel(instance, handle);
} finally { pin.Free(); }
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

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

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

handle = ola.YoloLoadModelMemoryEx(
    instance, c_int64(id(buf.raw)), len(model_bytes),
    0, 0, 0, 0, 1, 0, -1)
if handle:
    ola.YoloReleaseModel(instance, handle)

返回值

返回值说明
> 0成功,返回模型句柄。
负数失败;绝对值为错误码。与 GetLastError() 一致。

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

判断成功请用 handle > 0,不要用 != 0

注意事项

项目说明
模块权限需要插件已开通 YOLO 模块权限(Reg、Login的FeatureList中包含YOLO特性)。
字节流 → 地址易语言取变量数据地址(字节集)火山取字节集指针(字节集)C#GCHandle.Alloc 固定后取地址;Pythoncreate_string_buffer + addressof
ReadBytesFromFile返回指针可直接传入;加载成功后须 FreeMemoryPtr 释放读盘缓冲。
缓冲区格式不得为加密包格式;加密包请用 YoloLoadModelMemory
NCNNmodelType=2 时须同时传入 .bin.param 两块内存。
类别名表推荐 labelSize 为文件精确字节数;支持 | 分隔、多行及 UTF-8/GBK 等编码,见 类别名称表说明
释放句柄不再使用时须 YoloReleaseModel 释放。

modelType 模型后端类型

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

inferenceType 推理任务类型

含义
0Detect 目标检测
1Classify 图像分类
2Segment 实例分割
3Pose 姿态估计
4Obb 旋转框检测
  • inferenceDevice-1 表示 CPU;0 及以上表示 GPU 索引。GPU 不可用时可能自动回退 CPU(以实际 ExecutionProvider 为准)。