1 /*
2  * Copyright (C) 2021-2023 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <string.h>
7 #include "ulog/ulog.h"
8 #include "aos/kernel.h"
9 #include "aiagent_engine.h"
10 #include "engine/ucloud_ai_engine.h"
11 #include "aiagent_service.h"
12 #include "ucloud_ai_common.h"
13 #include "aiconfig.h"
14 
15 #define TAG "UCLOUD_AI_ENGINE"
16 
ucloud_ai_engine_delete(aiagent_engine_t * eng)17 static void ucloud_ai_engine_delete(aiagent_engine_t *eng)
18 {
19     if (!eng)
20         return -1;
21 
22     free(eng);
23 }
24 
ucloud_ai_engine_available(void)25 static bool ucloud_ai_engine_available(void)
26 {
27     return true;
28 }
29 
ucloud_ai_engine_init(aiagent_engine_t * eng)30 int32_t ucloud_ai_engine_init(aiagent_engine_t *eng)
31 {
32     int32_t ret;
33 
34     if (!eng)
35         return -1;
36 
37     ret = ucloud_ai_init();
38     if (ret < 0) {
39         LOGE(TAG, "ucloud_ai_init failed, ret: %d\n", ret);
40         return -1;
41     }
42 
43     ucloud_ai_set_key_secret(OSS_ACCESS_KEY, OSS_ACCESS_SECRET);
44     ucloud_ai_set_oss_endpoint(OSS_ENDPOINT);
45     ucloud_ai_set_oss_bucket(OSS_BUCKET);
46 }
47 
ucloud_ai_engine_uninit(aiagent_engine_t * eng)48 int32_t ucloud_ai_engine_uninit(aiagent_engine_t *eng)
49 {
50     if (!eng)
51         return -1;
52 
53     return ucloud_ai_uninit();
54 }
55 
ucloud_ai_engine_model_infer(aiagent_engine_t * eng)56 int ucloud_ai_engine_model_infer(aiagent_engine_t *eng)
57 {
58     if (!eng)
59         return -1;
60 
61     ucloud_ai_cb_t cb = (ucloud_ai_cb_t)eng->callback;
62     switch (eng->model) {
63 #ifdef CONFIG_ALICLOUD_FACEBODY_ENABLE
64         case UCLOUD_AI_MODEL_COMPARING_FACEBODY:
65             ucloud_ai_facebody_comparing_face(eng->src1, eng->src2, cb);
66             break;
67         case UCLOUD_AI_MODEL_GENERATE_HUMAN_ANIME_STYLE:
68             ucloud_ai_facebody_generate_human_anime_style(eng->src1, cb);
69             break;
70         case UCLOUD_AI_MODEL_RECOGNIZE_EXPRESSION:
71             ucloud_ai_facebody_recognize_expression(eng->src1, cb);
72             break;
73 #endif
74 #ifdef CONFIG_ALICLOUD_OBJECTDET_ENABLE
75         case UCLOUD_AI_MODEL_DETECT_OBJECT:
76             ucloud_ai_objectdet_detect_object(eng->src1, cb);
77             break;
78         case UCLOUD_AI_MODEL_DETECT_MAIN_BODY:
79             ucloud_ai_objectdet_detect_main_body(eng->src1, cb);
80             break;
81 #endif
82 #ifdef CONFIG_ALICLOUD_IMAGESEG_ENABLE
83         case UCLOUD_AI_MODEL_SEGMENT_COMMON_IMAGE:
84             ucloud_ai_imageseg_segment_common_image(eng->src1, cb);
85             break;
86         case UCLOUD_AI_MODEL_SEGMENT_FACE:
87             ucloud_ai_imageseg_segment_face(eng->src1, cb);
88             break;
89 #endif
90 #ifdef CONFIG_ALICLOUD_OCR_ENABLE
91         case UCLOUD_AI_MODEL_RECOGNIZE_IDENTITY_CARD_FACE_SIDE:
92             ucloud_ai_ocr_recognize_identity_card_face_side(eng->src1, cb);
93             break;
94         case UCLOUD_AI_MODEL_RECOGNIZE_IDENTITY_CARD_BACK_SIDE:
95             ucloud_ai_ocr_recognize_identity_card_back_side(eng->src1, cb);
96             break;
97         case UCLOUD_AI_MODEL_RECOGNIZE_BANK_CARD:
98             ucloud_ai_ocr_recognize_bank_card(eng->src1, cb);
99             break;
100         case UCLOUD_AI_MODEL_RECOGNIZE_CHARACTER:
101             ucloud_ai_ocr_recognize_character(eng->src1, cb);
102             break;
103 #endif
104 #ifdef CONFIG_ALICLOUD_IMAGERECOG_ENABLE
105         case UCLOUD_AI_MODEL_CLASSIFYING_RUBBISH:
106             ucloud_ai_imagerecog_classifying_rubbish(eng->src1, cb);
107             break;
108         case UCLOUD_AI_MODEL_DETECT_FRUITS:
109             ucloud_ai_imagerecog_detect_fruits(eng->src1, cb);
110             break;
111 #endif
112 #ifdef CONFIG_ALICLOUD_IMAGEENHAN_ENABLE
113         case UCLOUD_AI_MODEL_ERASE_PERSON:
114             ucloud_ai_imageenhan_erase_person(eng->src1, IMAGEENHAN_ERASE_PERSON_USERMASK_URL, cb);
115             break;
116         case UCLOUD_AI_MODEL_EXTEND_IMAGE_STYLE:
117             ucloud_ai_imageenhan_extend_image_style(eng->src1, IMAGEENHAN_EXTEND_IMAGE_STYLE_URL, cb);
118             break;
119 #endif
120         default:
121             break;
122     }
123     return 0;
124 }
125 
ucloud_ai_engine_create(int engineid)126 static aiagent_engine_t *ucloud_ai_engine_create(int engineid)
127 {
128     aiagent_engine_t *eng;
129 
130     /* Malloc ai agent eng*/
131     eng = (aiagent_engine_t *) malloc(sizeof(aiagent_engine_t));
132     if (!eng) {
133         LOGE(TAG, "malloc camera device fail\n");
134         return NULL;
135     }
136     eng->is_dummy = false;
137 
138     /* Set the function pointers */
139     eng->ai_engine_init = ucloud_ai_engine_init;
140     eng->ai_engine_uninit = ucloud_ai_engine_uninit;
141     eng->ai_engine_model_infer = ucloud_ai_engine_model_infer;
142     // eng->callback = ucloud_callback;
143     eng->ai_engine_free = ucloud_ai_engine_delete;
144 
145     return eng;
146 }
147 
148 aiagent_context_t ucloud_ai_engine = {
149     UCLOUD_AI_NAME, "ucloud ai inference engine",
150     ucloud_ai_engine_available, ucloud_ai_engine_create
151 };
152