vs22用dll调用python代码方法

本来是用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(&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值