C# byte[] 通过 c++ dll 传递到python环境 并返回 byte[] 还原

5 篇文章 0 订阅
5 篇文章 0 订阅

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 遇到的坑,记录一下备忘。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值