提交 | 用户 | 时间
|
449017
|
1 |
\#include "stdafx.h" |
D |
2 |
\#include "pch.h" |
|
3 |
\#include "${pyName}.h" |
|
4 |
\#include <string> |
|
5 |
\#include "pyutils.h" |
|
6 |
\#include "jni.h" |
|
7 |
\#include "Python.h" |
|
8 |
\#include "PyGILThreadLock.h" |
|
9 |
\#include "convertutils.h" |
|
10 |
|
|
11 |
using namespace std; |
|
12 |
|
|
13 |
jobject ${pyName}_train(JNIEnv* env, jobjectArray data_train, jobject settings) |
|
14 |
{ |
|
15 |
try |
|
16 |
{ |
|
17 |
PyGILThreadLock lock; |
|
18 |
PyObject* pModule = create_py_module("${pyModule}.${pyName}"); |
|
19 |
if (pModule == NULL) |
|
20 |
{ |
|
21 |
cout << "pModule error" << endl; |
|
22 |
throw "模块调用失败!"; |
|
23 |
} |
|
24 |
|
|
25 |
//*************导入函数****************** |
|
26 |
PyObject* pFunc = PyObject_GetAttrString(pModule, "train"); |
|
27 |
if (pFunc == NULL) |
|
28 |
{ |
|
29 |
cout << "pfunc error" << endl; |
|
30 |
throw "函数调用失败!"; |
|
31 |
} |
|
32 |
PyObject* data_1 = ConvertJdarrToPydarr(env, data_train); |
|
33 |
PyObject* settings_ = ConvertJhmapToPydict(env, settings); |
|
34 |
|
|
35 |
//************构造参数数据 ******************** |
|
36 |
PyObject* pArg = PyTuple_New(2); |
|
37 |
PyTuple_SetItem(pArg, 0, data_1); |
|
38 |
PyTuple_SetItem(pArg, 1, settings_); |
|
39 |
|
|
40 |
/*PyTuple_SetItem(pArg, 1, parseCppData(data_in->feature_range));*/ |
|
41 |
|
|
42 |
|
|
43 |
//********************调用python方法计算***************** |
|
44 |
PyObject* pReturn = PyEval_CallObject(pFunc, pArg); |
|
45 |
if (pReturn == NULL) |
|
46 |
{ |
|
47 |
cout << "pReturn error" << endl; |
|
48 |
throw "函数返回值异常!"; |
|
49 |
} |
|
50 |
//*******************返回数据封装************************ |
|
51 |
// 开辟返回数据内存空间,转换数据,拆分数据 |
|
52 |
|
|
53 |
/*jobject result = jhmappydict_(env, pReturn);*/ |
|
54 |
jobject result = ConvertPydictToJhmap(env, pReturn); |
|
55 |
if (result == NULL) |
|
56 |
{ |
|
57 |
cout << "ConvertPydictToJhmap error" << endl; |
|
58 |
throw "函数返回值异常!"; |
|
59 |
} |
|
60 |
delete pModule, pFunc, data_1, settings_, pArg; |
|
61 |
return result; |
|
62 |
} |
|
63 |
catch (const char* msg) |
|
64 |
{ |
|
65 |
cout << "train error" << endl; |
|
66 |
return NULL; |
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
jobject ${pyName}_predict(JNIEnv* env, #{foreach} ($column in [1..$dataLength])jobjectArray data${column}, #{end}jobject model_path_,jobject settings) |
|
71 |
{ |
|
72 |
try |
|
73 |
{ |
|
74 |
|
|
75 |
PyGILThreadLock lock; |
|
76 |
PyObject* pModule = create_py_module("${pyModule}.${pyName}"); |
|
77 |
if (pModule == NULL) |
|
78 |
{ |
|
79 |
cout << "model error" << endl; |
|
80 |
throw "模块调用失败!"; |
|
81 |
} |
|
82 |
|
|
83 |
//*************导入函数****************** |
|
84 |
PyObject* pFunc = PyObject_GetAttrString(pModule, "predict"); |
|
85 |
if (pFunc == NULL) |
|
86 |
{ |
|
87 |
cout << "func error" << endl; |
|
88 |
throw "函数调用失败!"; |
|
89 |
} |
|
90 |
#{foreach} ($column in [1..$dataLength]) |
|
91 |
PyObject* data_${column} = ConvertJdarrToPydarr(env, data${column}); |
|
92 |
#{end} |
|
93 |
PyObject* model_path = ConvertJhmapToPydict(env, model_path_); |
|
94 |
PyObject* settings_1 = ConvertJhmapToPydict(env, settings); |
|
95 |
|
|
96 |
|
|
97 |
//************构造参数数据 ******************** |
|
98 |
#set($a=$dataLength + 2) |
|
99 |
PyObject* pArg = PyTuple_New(${a}); |
|
100 |
#{foreach} ($column in [1..$dataLength]) |
|
101 |
#set($a=$column - 1) |
|
102 |
PyTuple_SetItem(pArg, ${a}, data_${column}); |
|
103 |
#{end} |
|
104 |
PyTuple_SetItem(pArg, ${dataLength}, model_path); |
|
105 |
#set($a=$dataLength + 1) |
|
106 |
PyTuple_SetItem(pArg, ${a}, settings_1); |
|
107 |
|
|
108 |
/*PyTuple_SetItem(pArg, 1, parseCppData(data_in->feature_range));*/ |
|
109 |
|
|
110 |
|
|
111 |
//********************调用python方法计算***************** |
|
112 |
PyObject* pReturn = PyEval_CallObject(pFunc, pArg); |
|
113 |
if (pReturn == NULL) |
|
114 |
{ |
|
115 |
cout << "return error" << endl; |
|
116 |
throw "函数返回值异常!"; |
|
117 |
} |
|
118 |
//*******************返回数据封装************************ |
|
119 |
// 开辟返回数据内存空间,转换数据,拆分数据 |
|
120 |
|
|
121 |
/*jobject result = jhmappydict_(env, pReturn);*/ |
|
122 |
jobject result = ConvertPydictToJhmap(env, pReturn); |
|
123 |
if (result == NULL) |
|
124 |
{ |
|
125 |
cout << "ConvertPydictToJhmap error" << endl; |
|
126 |
throw "函数返回值异常!"; |
|
127 |
} |
|
128 |
delete pModule; |
|
129 |
pModule = nullptr; |
|
130 |
delete pModule; |
|
131 |
|
|
132 |
delete pFunc; |
|
133 |
pFunc = nullptr; |
|
134 |
delete pFunc; |
|
135 |
|
|
136 |
#{foreach} ($column in [1..$dataLength]) |
|
137 |
delete[] data_${column}; |
|
138 |
data_${column} = nullptr; |
|
139 |
delete data_${column}; |
|
140 |
|
|
141 |
#{end} |
|
142 |
delete model_path; |
|
143 |
model_path = nullptr; |
|
144 |
delete model_path; |
|
145 |
|
|
146 |
delete settings_1; |
|
147 |
settings_1 = nullptr; |
|
148 |
delete settings_1; |
|
149 |
|
|
150 |
delete[] pArg; |
|
151 |
pArg = nullptr; |
|
152 |
delete pArg; |
|
153 |
|
|
154 |
return result; |
|
155 |
} |
|
156 |
catch (const char* msg) |
|
157 |
{ |
|
158 |
cout << "predict error" << endl; |
|
159 |
return NULL; |
|
160 |
} |
|
161 |
} |