本来是用github上下载的YOLOv11的网络训练了自己的数据集,达到了一个个人还算比较满意的目标检测的效果。
然后因为项目的一体性,需要和另一边vs22中的代码结合在一起,能够一块并行执行调用。然后在kimi的推荐下,我采用了用dll动态链接库的方法来调取。具体的dll调用代码如下所示:
首先是vs中新建的dll项目,dllmain.cpp中的代码:
上面设置如下所示:
#pragma warning(disable:4996)
#include "pch.h"
#include <iostream>
#include <Python.h>
//#include "predict.c"
//#include "changshi.c"
#include <windows.h>
#include <wchar.h>
#ifdef IMPORT_DLL
#else
#define IMPORT_DLL extern "C" _declspec(dllimport) //指的是允许将其给外部调用
#define IMPORT_DLL extern "C" _declspec(dllexport)
#endif
然后是我设置过的函数:
IMPORT_DLL int picture_predict(char* b, char* c, char* d);
int picture_predict(char* pyfile_route, char* test_route, char* pt_route) {
//设置python解释器的路径
Py_SetPythonHome(L"D:/ProgramData/anaconda3/envs/yolov11");
//临时保密
//wchar_t a[10] = L"/python.exe";
//wcscat(evironments, a);
//Py_SetProgramName(evironments);
wchar_t programName[512] = L"D:/ProgramData/anaconda3/envs/yolov11/python.exe";
Py_SetProgramName(programName);
printf("%s \n%s \n%s \n", pyfile_route, pt_route, test_route);
// 初始化 Python 解释器并对其进行验证
Py_Initialize();
if (!Py_IsInitialized()) {
PyErr_Print();
return 1;
}
PyRun_SimpleString(&#