C# 语法
/*声明c++dll接口*/
[DllImport("yolact_mask", EntryPoint = "get_yolact_mask", CharSet = CharSet.Ansi,CallingConvention = CallingConvention.Cdecl)]
/*调用c++dll接口方法*/
extern static IntPtr get_yolact_mask(IntPtr byte_img, int byte_size);
/*得到一个byte[]*/
byte[] byte_img = bgImageRenderer.GetByteImage(datum.cvInputData);
/*获取byte长度*/
int size = byte_img.Length;
/*托管指针*/
IntPtr buffer = Marshal.AllocHGlobal(size);
Marshal.Copy(byte_img, 0, buffer, size);
/*buferr传给c++ dll接口 ,返回一个byte指针,还原byte[]*/
IntPtr back_buffer = get_yolact_mask(buffer, size);
/*获取byte长度*/
int total_buff_size = (int)get_buff_size();
if (total_buff_size != 0)
{
byte_frame = new byte[height * width * 4];
Marshal.Copy(buffer, byte_frame, 0, total_buff_size);
}
}
/*清理内存*/
Marshal.FreeHGlobal(buffer);
C++语法
#include "stdafx.h"
#include<iostream>
#include<Python.h>
#include<string.h>
#include<windows.h>
#include<boost/thread.hpp>
#include<boost/python.hpp>
#include<winsock.h>
#include<WinUser.h>
#include<numpy/arrayobject.h>
using namespace std;
PyObject * pModule;
PyObject * pOpenCamera;
/*初始化cpython*/
#define _csharp_bool extern "C" __declspec(dllexport) const bool
_csharp_bool init_yolact_py(wchar_t * img_path){
Py_SetPath(img_path);
if (Py_IsInitialized()) {
return true;
}
Py_Initialize();
bool re_bool;
if (!Py_IsInitialized()) {
printf("初始化失败!\n");
re_bool = false;
}
else {
printf("初始化成功!\n");
re_bool = true;
};
return re_bool;
}
//python环境初始化摄像头
#define _csharp_bool extern "C" __declspec(dllexport) const bool
_csharp_bool init_camer_py(int w,int h) {
pModule = PyImport_ImportModule("camera");
pOpenCamera = PyObject_GetAttrString(pModule, "init_camera");
bool re_bool;
if (PyCallable_Check(pOpenCamera)) {
printf("初始化成功!\n");
re_bool = true;
}
else {
printf("初始化失败!\n");
re_bool = false;
};
return re_bool;
}
#define _csharp_double extern "C" __declspec(dllexport) const double *
_csharp_double get_yolact_mask(char * img_byte, int bt_size) {
import_array();
PyObject * pValue = Py_BuildValue("(y#)", img_byte, bt_size);
PyObject * pyResult = PyEval_CallObject(pOpenCamera, pValue);
double * resDataArr = NULL;
if (pyResult) {
pyResultArr = (PyArrayObject *)pyResult;
resDataArr = (double *)PyArray_DATA(pyResultArr);
//int dimNum = PyArray_NDIM(pyResultArr); //返回数组的维度数
buff_size = 0;
npy_intp *pdim = PyArray_DIMS(pyResultArr); //返回数组各维度上的元素个数值
buff_size = (int*)pdim[0];
}
Py_CLEAR(pValue);
return resDataArr;
}
#define _csharp_int extern "C" __declspec(dllexport) const int*
_csharp_int get_buff_size() {
return buff_size;
}
//释放pyResultArr,防止内存泄漏
#define _csharp_bool extern "C" __declspec(dllexport) const bool
_csharp_bool destory_pyResultArr(void) {
Py_CLEAR(pyResultArr);
return true;
}
//python环境释放对象和关闭摄像头
#define _csharp_bool extern "C" __declspec(dllexport) const bool
_csharp_bool destory_yolact_py(void) {
Py_CLEAR(pModule);
Py_CLEAR(pOpenCamera);
return true;
}
python语法
def init_camera(img_byte):
frame = np.frombuffer(img_byte, dtype=np.uint8)
frame = frame.reshape(720, 1280, 3)
frame = cv.flip(frame, 1)
cv.imshow("YOLACT2", frame )
cv.waitKey(1)
return frame .reshape(-1)
#未使用
def destory_ap(args):
sys.exit(0)
return True
C# 传递byte[]到python 遇到的坑,记录一下备忘。