1 
2 #if 0
3 
4 #include "base/modules/ml/include/HaasMLOlda.h"
5 #include "ulog/ulog.h"
6 #include "base/modules/core/include/HaasErrno.h"
7 #include "uai_quant.h"
8 #include "uai_scale_data.h"
9 #include "uai_img_data.h"
10 #include "uai_odla_inference.h"
11 #include "uai_odla_imgproc.h"
12 #include "HaasImageCodecPng.h"
13 
14 #define LOG_TAG "HAAS_ML_OLDA"
15 
16 HaasMLOlda::HaasMLOlda()
17     : input_scale(0),
18       input_shift(0),
19       norm_img_data(NULL)
20 {
21     LOGD(LOG_TAG, "entern\n");
22     norm_img_data = (uint8_t *)malloc(DEC_IMG_WIDTH * DEC_IMG_HEIGHT);
23     memset(norm_img_data, 0, DEC_IMG_WIDTH * DEC_IMG_HEIGHT);
24     memset(out_output_Softmax, 0, 21);
25 }
26 
27 HaasMLOlda::~HaasMLOlda()
28 {
29     LOGD(LOG_TAG, "entern\n");
30     free(norm_img_data);
31     norm_img_data = NULL;
32 }
33 
34 int HaasMLOlda::SetInputData(const char* dataPath)
35 {
36     LOGD(LOG_TAG, "entern path = %s;\n", dataPath);
37 
38     HaasImageCodecPng * mHaasImageCodecPng = new HaasImageCodecPng();
39     ImageBuffer_t *image = mHaasImageCodecPng->ImgDecode(dataPath);
40     if (image == NULL)
41     {
42         LOGD(LOG_TAG, "mHaasImageCodecBmp->ImgDecode failed\n");
43     }
44     uint8_t *gray_img = NULL;
45     LOGD(LOG_TAG, "bitmap pixels = %p;width = %d; height = %d;\n", image->address[0], image->width, image->height);
46     gray_img = (uint8_t*)malloc(image->width * image->height);
47     memset(gray_img, 0, image->width * image->height);
48 
49     uai_get_gray_img((uint8_t*)image->address[0], image->width, image->height, gray_img);
50     uai_get_resize_img(gray_img, image->width, image->height, norm_img_data, DEC_IMG_WIDTH, DEC_IMG_HEIGHT);
51     free(gray_img);
52 
53     free(image->address[0]);
54     free(image);
55     LOGD(LOG_TAG, "call delete mHaasImageCodecBmp\n");
56     delete mHaasImageCodecPng;
57     return STATUS_OK;
58 }
59 
60 int HaasMLOlda::LoadNet(const char* modePath)
61 {
62     LOGD(LOG_TAG, "entern\n");
63     char data_src[128];
64     uai_model_quant_scale_data_t *model_scale = NULL;
65 
66     memset(data_src, 0, 128);
67     snprintf(data_src, 128, "mem:%u,size:%u", uai_scale_data, uai_scale_data_len);
68     uai_load_model_scale_config(data_src);
69     model_scale = uai_load_model_scale();
70 
71     input_scale = model_scale->head.input_scale;
72     input_shift = model_scale->head.input_shift;
73 
74     return STATUS_OK;
75 }
76 
77 int HaasMLOlda::Predict()
78 {
79     LOGD(LOG_TAG, "entern\n");
80     uai_quant_normalize_uint8(norm_img_data, DEC_IMG_WIDTH * DEC_IMG_HEIGHT, input_scale,     input_shift);
81 
82     alios_b0((const int8_t*)norm_img_data, out_output_Softmax);
83     return STATUS_OK;
84 }
85 
86 int HaasMLOlda::GetPredictResponses(char* outResult, int len)
87 {
88     LOGD(LOG_TAG, "entern\n");
89     if (outResult == NULL || len < 3)
90     {
91         LOGE(LOG_TAG, "par is illegal outResult = %p; len = %d;", outResult, len);
92         return STATUS_ERROR;
93     }
94     memset(outResult, 0, len);
95     uai_get_img_dec_result(out_output_Softmax, 21, outResult);
96     LOGE(LOG_TAG, "AI RECOGNIZE number is: %s\n\n", outResult);
97     LOGD(LOG_TAG, "score: \n");
98     for(int i = 0; i < 21; i++) {
99         LOGD(LOG_TAG, "[%d]: %d\n", i, out_output_Softmax[i]);
100     }
101     return STATUS_OK;
102 }
103 
104 int HaasMLOlda::UnLoadNet()
105 {
106     LOGD(LOG_TAG, "entern\n");
107     return STATUS_OK;
108 }
109 
110 int HaasMLOlda::uai_get_img_dec_result(int8_t output_softmax[], int result_num, char *result)
111 {
112 	int max_id = 0;
113 	int i = 0;
114 
115 	for (int i = 0; i < result_num; i++) {
116 		if (output_softmax[i] > output_softmax[max_id]) {
117 			max_id = i;
118 		}
119 	}
120 
121 	if (output_softmax[max_id] != 0) {
122 		if (max_id <= 9) {
123 			result[0] = 0x30 + max_id;
124 			result[1] = 0;
125 		}
126 		else {
127 			result[0] = 0x30 + max_id - 10;
128 			result[1] = '.';
129 			result[2] = 0;
130 		}
131 	}
132 
133 	return 0;
134 }
135 
136 #endif