1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 
5 #include "py/mperrno.h"
6 #include "py/obj.h"
7 #include "py/runtime.h"
8 #include "py/builtin.h"
9 
10 #include "base/modules/c/include/WrapperIHaasML.h"
11 #include "ulog/ulog.h"
12 
13 #define LOG_TAG "ML"
14 
15 extern const mp_obj_type_t minicv_ml_type;
16 // this is the actual C-structure for our new object
17 typedef struct
18 {
19     // base represents some basic information, like type
20     mp_obj_base_t Base;
21     // a member created by us
22     char *ModuleName;
23     MLEngineType_t  mType;
24     void            *mInstance;
25 } mp_ml_obj_t;
26 
ml_obj_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)27 void ml_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
28 {
29     LOGD(LOG_TAG, "entern %s;\n", __func__);
30     mp_ml_obj_t *self = MP_OBJ_TO_PTR(self_in);
31     mp_printf(print, "ModuleName(%s)", self->ModuleName);
32 }
33 
ml_obj_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)34 STATIC mp_obj_t ml_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
35 {
36     LOGD(LOG_TAG, "entern  %s;\n", __func__);
37     mp_ml_obj_t* driver_obj = m_new_obj(mp_ml_obj_t);
38     if (!driver_obj) {
39         mp_raise_OSError(MP_EINVAL);
40     }
41 
42     driver_obj->Base.type = &minicv_ml_type;
43     driver_obj->ModuleName = "minicv-ML";
44     driver_obj->mType = ML_ENGINE_NONE;
45     driver_obj->mInstance = NULL;
46 
47     return MP_OBJ_FROM_PTR(driver_obj);
48 }
49 
obj_open(size_t n_args,const mp_obj_t * args)50 STATIC mp_obj_t obj_open(size_t n_args, const mp_obj_t *args)
51 {
52     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
53     int ret = -1;
54     void* instance = NULL;
55     if (n_args < 2)
56     {
57         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
58         return mp_const_none;
59     }
60     mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
61     mp_ml_obj_t* driver_obj = (mp_ml_obj_t *)self;
62     if (driver_obj == NULL)
63     {
64         LOGE(LOG_TAG, "driver_obj is NULL\n");
65         return mp_const_none;
66     }
67 
68     if (driver_obj->mInstance != NULL)
69     {
70         LOGE(LOG_TAG, "Module has been opened, please clode first\n");
71         return mp_const_none;
72     }
73 
74     driver_obj->mType = (MLEngineType_t)mp_obj_get_int(args[1]);
75     LOGD(LOG_TAG, "%s:mType = %d;\n", __func__, driver_obj->mType);
76     instance = MLCreateInstance(driver_obj->mType);
77     driver_obj->mInstance = instance;
78     if (instance == NULL)
79     {
80         LOGE(LOG_TAG, "MLCreateInstance failed\n");
81         return mp_const_none;
82     }
83     LOGD(LOG_TAG, "%s:out\n", __func__);
84 
85     return mp_const_none;
86 }
87 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(ml_obj_open, 2, obj_open);
88 
obj_close(size_t n_args,const mp_obj_t * args)89 STATIC mp_obj_t obj_close(size_t n_args, const mp_obj_t *args)
90 {
91     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
92     int ret = -1;
93     void* instance = NULL;
94     if (n_args < 1)
95     {
96         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
97         return mp_const_none;
98     }
99     mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
100     mp_ml_obj_t* driver_obj = (mp_ml_obj_t *)self;
101     if (driver_obj == NULL)
102     {
103         LOGE(LOG_TAG, "driver_obj is NULL\n");
104         return mp_const_none;
105     }
106 
107     if (driver_obj->mInstance == NULL)
108     {
109         LOGE(LOG_TAG, "Module has not been opened, not need close\n");
110         return mp_const_none;
111     }
112 
113     MLDestoryInstance(driver_obj->mInstance);
114     driver_obj->mType = ML_ENGINE_NONE;
115     driver_obj->mInstance = NULL;
116     LOGD(LOG_TAG, "%s:out\n", __func__);
117 
118     return mp_const_none;
119 }
120 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(ml_obj_close, 1, obj_close);
121 
obj_config(size_t n_args,const mp_obj_t * args)122 STATIC mp_obj_t obj_config(size_t n_args, const mp_obj_t *args)
123 {
124     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
125     int ret = -1;
126     void* instance = NULL;
127     if (n_args < 6)
128     {
129         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
130         return mp_const_none;
131     }
132     mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
133     mp_ml_obj_t* driver_obj = (mp_ml_obj_t *)self;
134     if (driver_obj == NULL)
135     {
136         LOGE(LOG_TAG, "driver_obj is NULL\n");
137         return mp_const_none;
138     }
139 
140     if (driver_obj->mInstance == NULL)
141     {
142         LOGE(LOG_TAG, "Module has been closed, please open first\n");
143         return mp_const_none;
144     }
145 
146     char *key = (char *)mp_obj_str_get_str(args[1]);
147     char *secret = (char *)mp_obj_str_get_str(args[2]);
148     char *region_id = (char *)mp_obj_str_get_str(args[3]);
149     char *endpoint = (char *)mp_obj_str_get_str(args[4]);
150     char *url = (char *)mp_obj_str_get_str(args[5]);
151     LOGD(LOG_TAG, "key = %s;\n", key);
152     LOGD(LOG_TAG, "secret = %s;\n", secret);
153     LOGD(LOG_TAG, "region_id = %s;\n", region_id);
154     LOGD(LOG_TAG, "endpoint = %s;\n", endpoint);
155     LOGD(LOG_TAG, "url = %s;\n", url);
156     ret = MLConfig(driver_obj->mInstance, key, secret, region_id,
157             endpoint, url);
158     if (ret != 0)
159     {
160         LOGE(LOG_TAG, "MLConfig failed\n");
161         return mp_const_none;
162     }
163 
164     return mp_const_none;
165 }
166 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(ml_obj_config, 6, obj_config);
167 
obj_setInputData(size_t n_args,const mp_obj_t * args)168 STATIC mp_obj_t obj_setInputData(size_t n_args, const mp_obj_t *args)
169 {
170     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
171     int ret = -1;
172     void* instance = NULL;
173     if (n_args < 2)
174     {
175         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
176         return mp_const_none;
177     }
178     mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
179     mp_ml_obj_t* driver_obj = (mp_ml_obj_t *)self;
180     if (driver_obj == NULL)
181     {
182         LOGE(LOG_TAG, "driver_obj is NULL\n");
183         return mp_const_none;
184     }
185 
186     if (driver_obj->mInstance == NULL)
187     {
188         LOGE(LOG_TAG, "Module has been closed, please open first\n");
189         return mp_const_none;
190     }
191 
192     char *mFileName = (char *)mp_obj_str_get_str(args[1]);
193     LOGD(LOG_TAG, "mFileName = %s;\n", mFileName);
194     ret = MLSetInputData(driver_obj->mInstance, mFileName);
195     if (ret != 0)
196     {
197         LOGE(LOG_TAG, "MLSetInputData failed mFileName = %s;\n", mFileName);
198         return mp_const_none;
199     }
200     LOGD(LOG_TAG, "%s:out\n", __func__);
201 
202     return mp_const_none;
203 }
204 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(ml_obj_setInputData, 2, obj_setInputData);
205 
obj_loadNet(size_t n_args,const mp_obj_t * args)206 STATIC mp_obj_t obj_loadNet(size_t n_args, const mp_obj_t *args)
207 {
208     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
209     int ret = -1;
210     void* instance = NULL;
211     if (n_args < 2)
212     {
213         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
214         return mp_const_none;
215     }
216     mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
217     mp_ml_obj_t* driver_obj = (mp_ml_obj_t *)self;
218     if (driver_obj == NULL)
219     {
220         LOGE(LOG_TAG, "driver_obj is NULL\n");
221         return mp_const_none;
222     }
223 
224     if (driver_obj->mInstance == NULL)
225     {
226         LOGE(LOG_TAG, "Module has been closed, please open first\n");
227         return mp_const_none;
228     }
229 
230     char *mFileName = (char *)mp_obj_str_get_str(args[1]);
231     LOGD(LOG_TAG, "mFileName = %s;\n", mFileName);
232     ret = MLLoadNet(driver_obj->mInstance, mFileName);
233     if (ret != 0)
234     {
235         LOGE(LOG_TAG, "MLLoadNet failed mFileName = %s;\n", mFileName);
236         return mp_const_none;
237     }
238     LOGD(LOG_TAG, "%s:out\n", __func__);
239 
240     return mp_const_none;
241 }
242 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(ml_obj_loadNet, 2, obj_loadNet);
243 
obj_predict(size_t n_args,const mp_obj_t * args)244 STATIC mp_obj_t obj_predict(size_t n_args, const mp_obj_t *args)
245 {
246     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
247     int ret = -1;
248     void* instance = NULL;
249     if (n_args < 1)
250     {
251         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
252         return mp_const_none;
253     }
254     mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
255     mp_ml_obj_t* driver_obj = (mp_ml_obj_t *)self;
256     if (driver_obj == NULL)
257     {
258         LOGE(LOG_TAG, "driver_obj is NULL\n");
259         return mp_const_none;
260     }
261 
262     if (driver_obj->mInstance == NULL)
263     {
264         LOGE(LOG_TAG, "Module has been closed, please open first\n");
265         return mp_const_none;
266     }
267 
268     ret = MLPredict(driver_obj->mInstance);
269     if (ret != 0)
270     {
271         LOGE(LOG_TAG, "MLPredict failed\n");
272         return mp_const_none;
273     }
274     LOGD(LOG_TAG, "%s:out\n", __func__);
275 
276     return mp_const_none;
277 }
278 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(ml_obj_predict, 1, obj_predict);
279 
obj_getPredictResponses(size_t n_args,const mp_obj_t * args)280 STATIC mp_obj_t obj_getPredictResponses(size_t n_args, const mp_obj_t *args)
281 {
282     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
283     int ret = -1;
284     void* instance = NULL;
285     if (n_args < 2)
286     {
287         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
288         return mp_const_none;
289     }
290     mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
291     mp_ml_obj_t* driver_obj = (mp_ml_obj_t *)self;
292     if (driver_obj == NULL)
293     {
294         LOGE(LOG_TAG, "driver_obj is NULL\n");
295         return mp_const_none;
296     }
297 
298     if (driver_obj->mInstance == NULL)
299     {
300         LOGE(LOG_TAG, "Module has been closed, please open first\n");
301         return mp_const_none;
302     }
303 
304     mp_buffer_info_t bufinfo;
305     mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
306     memset(bufinfo.buf, 0, bufinfo.len);
307     LOGD(LOG_TAG, "bufinfo.buf = %p;bufinfo.len = %d;\n", bufinfo.buf, bufinfo.len);
308     ret = MLGetPredictResponses(driver_obj->mInstance, bufinfo.buf, bufinfo.len);
309     if (ret != 0)
310     {
311         LOGE(LOG_TAG, "MLGetPredictResponses failed\n");
312         //return mp_const_none;
313     }
314     LOGD(LOG_TAG, "bufinfo.buf = %s;bufinfo.len = %d;\n", bufinfo.buf, bufinfo.len);
315     LOGD(LOG_TAG, "%s:out ret = %d;\n", __func__, ret);
316 
317     return MP_ROM_INT(ret);
318 }
319 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(ml_obj_getPredictResponses, 2, obj_getPredictResponses);
320 
obj_unLoadNet(size_t n_args,const mp_obj_t * args)321 STATIC mp_obj_t obj_unLoadNet(size_t n_args, const mp_obj_t *args)
322 {
323     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
324     int ret = -1;
325     void* instance = NULL;
326     if (n_args < 1)
327     {
328         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
329         return mp_const_none;
330     }
331     mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
332     mp_ml_obj_t* driver_obj = (mp_ml_obj_t *)self;
333     if (driver_obj == NULL)
334     {
335         LOGE(LOG_TAG, "driver_obj is NULL\n");
336         return mp_const_none;
337     }
338 
339     if (driver_obj->mInstance == NULL)
340     {
341         LOGE(LOG_TAG, "Module has been closed, please open first\n");
342         return mp_const_none;
343     }
344 
345     ret = MLUnLoadNet(driver_obj->mInstance);
346     if (ret != 0)
347     {
348         LOGE(LOG_TAG, "MLUnLoadNet failed\n");
349         return mp_const_none;
350     }
351     LOGD(LOG_TAG, "%s:out\n", __func__);
352 
353     return mp_const_none;
354 }
355 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(ml_obj_unLoadNet, 1, obj_unLoadNet);
356 
357 STATIC const mp_rom_map_elem_t ml_locals_dict_table[] = {
358     {MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ML)},
359     {MP_OBJ_NEW_QSTR(MP_QSTR_ML_ENGINE_NONE), MP_ROM_INT(ML_ENGINE_NONE)},
360     {MP_OBJ_NEW_QSTR(MP_QSTR_ML_ENGINE_MNN), MP_ROM_INT(ML_ENGINE_MNN)},
361     {MP_OBJ_NEW_QSTR(MP_QSTR_ML_ENGINE_CLOUD), MP_ROM_INT(ML_ENGINE_CLOUD)},
362     {MP_OBJ_NEW_QSTR(MP_QSTR_ML_ENGINE_ODLA), MP_ROM_INT(ML_ENGINE_ODLA)},
363     {MP_OBJ_NEW_QSTR(MP_QSTR_ML_ENGINE_MAX), MP_ROM_INT(ML_ENGINE_MAX)},
364     {MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&ml_obj_open)},
365     {MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&ml_obj_close)},
366     {MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&ml_obj_config)},
367     {MP_ROM_QSTR(MP_QSTR_setInputData), MP_ROM_PTR(&ml_obj_setInputData)},
368     {MP_ROM_QSTR(MP_QSTR_loadNet), MP_ROM_PTR(&ml_obj_loadNet)},
369     {MP_ROM_QSTR(MP_QSTR_predict), MP_ROM_PTR(&ml_obj_predict)},
370     {MP_ROM_QSTR(MP_QSTR_getPredictResponses), MP_ROM_PTR(&ml_obj_getPredictResponses)},
371     {MP_ROM_QSTR(MP_QSTR_unLoadNet), MP_ROM_PTR(&ml_obj_unLoadNet)},
372 };
373 
374 STATIC MP_DEFINE_CONST_DICT(ml_locals_dict, ml_locals_dict_table);
375 
376 const mp_obj_type_t minicv_ml_type = {
377     .base = {&mp_type_type},
378     .name = MP_QSTR_ML,
379     .print = ml_obj_print,
380     .make_new = ml_obj_make_new,
381     .locals_dict = (mp_obj_dict_t *)&ml_locals_dict,
382 };
383