1
2 #include "base/modules/ml/include/HaasMLCloud.h"
3 #include "ulog/ulog.h"
4 #include "base/modules/core/include/HaasErrno.h"
5 #include <string>
6 using namespace std;
7
8 #define LOG_TAG "HAAS_ML_CLOUD"
9
HaasMLCloud()10 HaasMLCloud::HaasMLCloud()
11 {
12 LOGD(LOG_TAG, "entern\n");
13 int32_t ret;
14 }
15
~HaasMLCloud()16 HaasMLCloud::~HaasMLCloud()
17 {
18 LOGD(LOG_TAG, "entern\n");
19 int32_t ret;
20 }
21
Config(char * key,char * secret,char * endpoint,char * bucket,char * url)22 int HaasMLCloud::Config(char *key, char *secret, char *endpoint,
23 char *bucket, char *url)
24 {
25 LOGD(LOG_TAG, "entern\n");
26 LOGD(LOG_TAG, "key = %s;\n", key);
27 LOGD(LOG_TAG, "secret = %s;\n", secret);
28 LOGD(LOG_TAG, "endpoint = %s;\n", endpoint);
29 LOGD(LOG_TAG, "bucket = %s;\n", bucket);
30 LOGD(LOG_TAG, "url = %s;\n", url);
31 mFacePath = url;
32 LOGD(LOG_TAG, "mFacePath = %s;\n", mFacePath.c_str());
33 ucloud_ai_set_key_secret(key, secret);
34 ucloud_ai_set_oss_endpoint(endpoint);
35 ucloud_ai_set_oss_bucket(bucket);
36
37 return STATUS_OK;
38 }
39
SetInputData(const char * dataPath)40 int HaasMLCloud::SetInputData(const char* dataPath)
41 {
42 LOGD(LOG_TAG, "entern dataPath = %s;\n", dataPath);
43 mDataPath = dataPath;
44 return STATUS_OK;
45 }
46
LoadNet(const char * modePath)47 int HaasMLCloud::LoadNet(const char* modePath)
48 {
49 LOGD(LOG_TAG, "entern modePath = %s;\n", modePath);
50 mAiMode = modePath;
51 return STATUS_OK;
52 }
53
Predict()54 int HaasMLCloud::Predict()
55 {
56 LOGD(LOG_TAG, "entern mAiMode = %s;\n", mAiMode);
57 int ret = -1;
58 std::string mode(mAiMode);
59 LOGD(LOG_TAG, "Train mode = %s;\n", mode.c_str());
60
61 ret = mode.compare("ObjectDet");
62 if (ret == 0)
63 {
64 LOGD(LOG_TAG, "Get ObjectDet Mode");
65 return PredictObjectDet();
66 }
67
68 ret = mode.compare("FacebodyComparing");
69 if (ret == 0)
70 {
71 LOGD(LOG_TAG, "Get FacebodyComparing Mode");
72 return PredictFacebodyComparing();
73 }
74
75 ret = mode.compare("AnimeStyle");
76 if (ret == 0)
77 {
78 LOGD(LOG_TAG, "Get AnimeStyle Mode");
79 return PredictAnimeStyle();
80 }
81
82 ret = mode.compare("RecognizeExpression");
83 if (ret == 0)
84 {
85 LOGD(LOG_TAG, "Get RecognizeExpression Mode");
86 return PredictRecognizeExpression();
87 }
88
89 ret = mode.compare("RecognizeCharacter");
90 if (ret == 0)
91 {
92 LOGD(LOG_TAG, "Get RecognizeCharacter Mode");
93 return PredictRecognizeCharacter();
94 }
95
96 LOGE(LOG_TAG, "AI Mode Par Is illegal\n");
97 return STATUS_ERROR;
98 }
99
GetPredictResponses(char * outResult,int len)100 int HaasMLCloud::GetPredictResponses(char* outResult, int len)
101 {
102 LOGD(LOG_TAG, "entern\n");
103 return STATUS_OK;
104 }
105
UnLoadNet()106 int HaasMLCloud::UnLoadNet()
107 {
108 LOGD(LOG_TAG, "entern\n");
109 return STATUS_OK;
110 }
111
PredictObjectDet()112 int HaasMLCloud::PredictObjectDet()
113 {
114 LOGD(LOG_TAG, "entern\n");
115 ucloud_ai_objectdet_detect_object(mDataPath, ObjectDetectCallback);
116 return STATUS_OK;
117 }
118
ObjectDetectCallback(ucloud_ai_result_t * result)119 int HaasMLCloud::ObjectDetectCallback(ucloud_ai_result_t *result)
120 {
121 int len = 0;
122 char *p_type = NULL;
123 int x, y, w, h;
124 char *type = NULL;
125 float score;
126
127 LOGD(LOG_TAG, "entern ObjectDetectCallback:\n");
128 if (!result)
129 return -1;
130
131 type = result->objectdet.object.type;
132 score = result->objectdet.object.score;
133 x = result->objectdet.object.box.x;
134 y = result->objectdet.object.box.y;
135 w = result->objectdet.object.box.w;
136 h = result->objectdet.object.box.h;
137
138 if (!type) {
139 LOGE(LOG_TAG, "type is null\n");
140 return -1;
141 }
142 p_type = strdup(type);
143 if (!p_type) {
144 LOGE(LOG_TAG, "p_type strdup fail\n");
145 return -1;
146 }
147
148 LOGD(LOG_TAG, "Detect object result:\n");
149 LOGE(LOG_TAG, "type: %s, Score: %.1f, x: %d, y: %d, w: %d, h: %d\n", p_type, score, x, y, w, h);
150 free(p_type);
151 p_type = NULL;
152
153 return 0;
154 }
155
PredictFacebodyComparing()156 int HaasMLCloud::PredictFacebodyComparing()
157 {
158 LOGD(LOG_TAG, "entern\n");
159 ucloud_ai_facebody_comparing_face(mDataPath, mFacePath.c_str(), FacebodyComparingCallback);
160 return STATUS_OK;
161 }
162
FacebodyComparingCallback(ucloud_ai_result_t * result)163 int HaasMLCloud::FacebodyComparingCallback(ucloud_ai_result_t *result)
164 {
165 LOGD(LOG_TAG, "entern\n");
166 float confidence;
167
168 if (!result)
169 return -1;
170
171 LOGD(LOG_TAG, "Facebody comparing result:\n");
172 confidence = result->facebody.face.confidence;
173 float x = result->facebody.face.location.x;
174 float y = result->facebody.face.location.y;
175 float w = result->facebody.face.location.w;
176 float h = result->facebody.face.location.h;
177 LOGE(LOG_TAG, "confidence: %.1f\n", confidence);
178 LOGE(LOG_TAG, "x: %.1f\n", x);
179 LOGE(LOG_TAG, "y: %.1f\n", y);
180 LOGE(LOG_TAG, "w: %.1f\n", w);
181 LOGE(LOG_TAG, "h: %.1f\n", h);
182 return STATUS_OK;
183 }
184
AnimeStyleCallback(ucloud_ai_result_t * result)185 int HaasMLCloud::AnimeStyleCallback(ucloud_ai_result_t *result)
186 {
187 LOGD(LOG_TAG, "entern\n");
188 int ret;
189 char *url = NULL;
190
191 if (!result)
192 return -1;
193
194 url = result->facebody.anime.url;
195 LOGD(LOG_TAG, "Generate human anime style result:\n");
196 LOGE(LOG_TAG, "url: %s\n", url);
197 return STATUS_OK;
198 }
199
PredictAnimeStyle()200 int HaasMLCloud::PredictAnimeStyle()
201 {
202 LOGD(LOG_TAG, "entern\n");
203 ucloud_ai_facebody_generate_human_anime_style(mDataPath, AnimeStyleCallback);
204 return STATUS_OK;
205 }
206
PredictRecognizeExpression()207 int HaasMLCloud::PredictRecognizeExpression()
208 {
209 LOGD(LOG_TAG, "entern\n");
210 ucloud_ai_facebody_recognize_expression(mDataPath, RecognizeExpressionCallback);
211 return STATUS_OK;
212 }
213
RecognizeExpressionCallback(ucloud_ai_result_t * result)214 int HaasMLCloud::RecognizeExpressionCallback(ucloud_ai_result_t *result)
215 {
216
217 LOGD(LOG_TAG, "entern\n");
218 int len;
219 char *expression, *p_expression;
220 float face_probability;
221
222 if (!result)
223 return -1;
224
225 expression = result->facebody.expression.expression;
226 face_probability = result->facebody.expression.probability;
227
228 if (!expression)
229 return -1;
230
231 p_expression = strdup(expression);
232 if (!p_expression) {
233 LOGE(LOG_TAG, "p_expression strdup fail\n");
234 return -1;
235 }
236
237 LOGE(LOG_TAG, "Recognize expression result:\n");
238 LOGE(LOG_TAG, "type: %s, probability: %.1f\n", p_expression, face_probability);
239 free(p_expression);
240 p_expression = NULL;
241 return STATUS_OK;
242 }
243
RecognizeCharacterCallback(ucloud_ai_result_t * result)244 int HaasMLCloud::RecognizeCharacterCallback(ucloud_ai_result_t *result)
245 {
246
247 LOGD(LOG_TAG, "entern\n");
248 float probability;
249 char *text = NULL;
250 int left, top;
251 int width, height;
252
253 if (!result)
254 return -1;
255
256 LOGD(LOG_TAG, "Recognize character result:\n");
257 text = result->ocr.character.text;
258 left = result->ocr.character.left;
259 top = result->ocr.character.top;
260 width = result->ocr.character.width;
261 height = result->ocr.character.height;
262 probability = result->ocr.character.probability;
263 if (text) {
264 LOGE(LOG_TAG, "text: %s\n", text);
265 LOGE(LOG_TAG, "probability: %.1f\n", probability);
266 LOGE(LOG_TAG, "text area: left: %d, top: %d, weight: %d, height: %d\n", top, left, width, height);
267 }
268 return STATUS_OK;
269 }
270
PredictRecognizeCharacter()271 int HaasMLCloud::PredictRecognizeCharacter()
272 {
273 LOGD(LOG_TAG, "entern\n");
274 ucloud_ai_ocr_recognize_character(mDataPath, RecognizeCharacterCallback);
275 return STATUS_OK;
276 }
277