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

传入一个路径字符串(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 

你可能感兴趣的:(c#,c++,python)