unity 通过 dll 传递字符串给python,实现小冰颜值鉴定

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

传入一个路径字符串(C#):

public void PassXiaoIce()
{
   try
   {
     string img_path = @"D:\XLS_AI_PRO\FaceTest\photo\11.jpg";
     Debug.Log(img_path.Length);
     string str = GetIceJson(img_path,img_path.Length);
     Debug.Log("result_str:" + str);//返回鉴定结果
    }
    catch
    {
     Debug.LogError("初始化异常!");
    }
}


//图片路径,返回颜值鉴定结果。
public String GetIceJson(string img_path, int psize)
{
    IntPtr temp = FaceBind.GetResultIce(img_path, psize);
    string str = Marshal.PtrToStringAnsi(temp).ToString();
    temp = IntPtr.Zero;
    return str;
}

DLL接口(C#):

[DllImport("XiaoICE", EntryPoint = "get_result_ice", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
static extern IntPtr get_result_ice([MarshalAs(UnmanagedType.LPStr)]string g_path,int size);

DLL实现代码(C++)

static char str_json[2048];
#define _csharp_double extern "C" __declspec(dllexport) const char * 
_csharp_double get_result_ice(wchar_t * img_byte, int bt_size) {
	import_array();
	PyObject * pValue = Py_BuildValue("(s#)", img_byte, bt_size);
	PyObject * pyResult = PyEval_CallObject(pGetXiaoIce, pValue);
    memset(str_json, 0, sizeof(str_json));	  
	if (pyResult) {
    	const char * str_js;
		PyArg_Parse(pyResult,"s",&str_js);
        strcpy_s(str_json, str_js);
	}
	else {
	    strcpy_s(str_json, "error!");
	}
	Py_CLEAR(pValue);
	Py_CLEAR(pyResult);
	return str_json;
}

 调用入口(python)

#传入图片路径,返回颜值鉴定结果
def identification(img_path):
    score = Score()
    re_str = score.start(img_path)
    return re_str 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值