Skip to content

YOLOv8 目标检测接口

版本:1.26.0613


existsYolov8Name

cpp
EXPORT_DLL_API int existsYolov8Name(const char* name);

功能:检查指定名称的 YOLOv8 模型是否已加载。

参数类型说明
name输入模型名称

返回值示例1(存在) / 0(不存在)

调用示例

cpp
if (existsYolov8Name("detector")) {
    printf("模型已加载\n");
}

loadYolov8Model

cpp
EXPORT_DLL_API int loadYolov8Model(const char* name, const char* modelPath);

功能:加载 YOLOv8 ONNX 模型。若同名模型已存在则直接返回成功(不会重复加载)。

参数类型说明
name输入模型名称(唯一标识)
modelPath输入模型文件路径(.onnx)

返回值示例

说明
1加载成功
0文件不存在(同时检查当前目录)
4加载异常,可通过 getLastError() 查看详情

调用示例

cpp
int ret = loadYolov8Model("detector", "yolov8n.onnx");
if (ret == 1) {
    printf("模型加载成功\n");
} else {
    printf("加载失败: %s\n", getLastError());
}

reLoadYolov8Model

cpp
EXPORT_DLL_API int reLoadYolov8Model(const char* name, const char* modelPath);

功能:重新加载模型(先卸载旧模型再加载新模型)。

参数类型说明
name输入模型名称
modelPath输入新模型文件路径

返回值示例

说明
1成功
3原模型不存在
0文件不存在
4加载异常

调用示例

cpp
int ret = reLoadYolov8Model("detector", "yolov8n_v2.onnx");
if (ret == 1) {
    printf("重载成功\n");
}

getYolov8Info

cpp
EXPORT_DLL_API const char* getYolov8Info(const char* name);

功能:获取指定模型的详细信息。

参数类型说明
name输入模型名称

返回值示例

json
{
  "inputNames": ["images"],
  "outputNames": ["output0"],
  "imgsz": [640, 640],
  "inputTensor": [1, 3, 640, 640],
  "ch": 3,
  "height": 640,
  "width": 640,
  "nc": 80,
  "task": "detect",
  "stride": 32,
  "iou": 0.45,
  "sim": 0.50,
  "name": "detector"
}

模型不存在返回 "{}",异常返回 "4"

调用示例

cpp
const char* info = getYolov8Info("detector");
printf("模型信息: %s\n", info);

getAllYolov8Name

cpp
EXPORT_DLL_API const char* getAllYolov8Name();

功能:获取所有已加载模型的名称列表。

返回值示例["model1","model2"],无模型时返回 "[]"

调用示例

cpp
const char* names = getAllYolov8Name();
printf("已加载模型: %s\n", names);

setYolov8Sim

cpp
EXPORT_DLL_API int setYolov8Sim(const char* name, float sim);

功能:设置模型推理置信度阈值(confidence threshold)。低于此值的检测结果将被过滤。

参数类型说明
name输入模型名称
sim输入置信度阈值(0~1)

返回值示例1(成功) / 0(模型不存在)

调用示例

cpp
setYolov8Sim("detector", 0.6f);

setYolov8Iou

cpp
EXPORT_DLL_API int setYolov8Iou(const char* name, float iou);

功能:设置 IoU 阈值(非极大值抑制 NMS)。重叠面积超过此值的框将被合并。

参数类型说明
name输入模型名称
iou输入IoU 阈值(0~1)

返回值示例1(成功) / 0(模型不存在)

调用示例

cpp
setYolov8Iou("detector", 0.5f);

yolov8Detect

cpp
EXPORT_DLL_API const char* yolov8Detect(const char* name, const char* imgPath);

功能:对图片执行 YOLOv8 目标检测,返回检测框 JSON。

参数类型说明
name输入模型名称
imgPath输入待检测图片路径

返回值示例

json
// 检测到目标
[
  {"id": 0, "name": "person", "x": 100, "y": 200, "w": 50, "h": 80, "sim": 0.95},
  {"id": 56, "name": "chair", "x": 300, "y": 400, "w": 30, "h": 40, "sim": 0.88}
]
// 特殊返回值
"0"    // 图片不存在
"2"    // 模型不存在
"4"    // 检测异常
"[]"   // 无检测结果

调用示例

cpp
const char* result = yolov8Detect("detector", "screen.png");
printf("检测结果: %s\n", result);

yolov8DetectDraw

cpp
EXPORT_DLL_API const char* yolov8DetectDraw(const char* name, const char* imgPath, const char* outPath);

功能:对图片执行 YOLOv8 目标检测,并将检测框绘制到图片上保存或显示。

参数类型说明
name输入模型名称
imgPath输入待检测图片路径
outPath输入结果图片输出路径。传 nullptr 时弹出窗口显示

返回值示例:同 yolov8Detect

json
[
  {"id": 0, "name": "person", "x": 100, "y": 200, "w": 50, "h": 80, "sim": 0.95},
  {"id": 56, "name": "chair", "x": 300, "y": 400, "w": 30, "h": 40, "sim": 0.88}
]

调用示例

cpp
const char* result = yolov8DetectDraw("detector", "screen.png", "result.png");
printf("检测结果: %s\n", result);

yolov8DetectPointer

cpp
EXPORT_DLL_API const char* yolov8DetectPointer(const char* name, const uint8_t* imgData, int imgLen);

功能:对图片执行 YOLOv8 目标检测,图片从内存数据加载而非文件路径。

参数类型说明
name输入模型名称
imgData输入图片内存数据指针(const uint8_t*,指向 PNG/JPG 等原始字节)
imgLen输入图片数据长度(字节数)

返回值示例:同 yolov8Detect

json
[
  {"id": 0, "name": "person", "x": 100, "y": 200, "w": 50, "h": 80, "sim": 0.95},
  {"id": 56, "name": "chair", "x": 300, "y": 400, "w": 30, "h": 40, "sim": 0.88}
]

调用示例

cpp
FILE* f = fopen("screen.png", "rb");
fseek(f, 0, SEEK_END);
int len = ftell(f);
uint8_t* buf = (uint8_t*)malloc(len);
fseek(f, 0, SEEK_SET);
fread(buf, 1, len, f);
fclose(f);

const char* result = yolov8DetectPointer("detector", buf, len);
free(buf);
printf("检测结果: %s\n", result);

yolov8DetectDrawPointer

cpp
EXPORT_DLL_API const char* yolov8DetectDrawPointer(const char* name, const uint8_t* imgData, int imgLen, const char* outPath);

功能:对图片执行 YOLOv8 目标检测并绘制结果框,图片从内存数据加载。

参数类型说明
name输入模型名称
imgData输入图片内存数据指针
imgLen输入图片数据长度(字节数)
outPath输入结果图片输出路径。传 nullptr 时弹出窗口显示

返回值示例:同 yolov8Detect

json
[
  {"id": 0, "name": "person", "x": 100, "y": 200, "w": 50, "h": 80, "sim": 0.95},
  {"id": 56, "name": "chair", "x": 300, "y": 400, "w": 30, "h": 40, "sim": 0.88}
]

调用示例

cpp
const char* result = yolov8DetectDrawPointer("detector", buf, len, "result.png");
printf("检测结果: %s\n", result);