motor_rainshed_cls.cpp
4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "motor_rainshed_cls.h"
#include "sy_errorinfo.h"
#include "cnn_cls.h"
#include "dvpp_processx.h"
#include <time.h>
#include <sys/time.h>
#include "stream_data.h"
using namespace atlas_utils;
using namespace std;
struct Resource {
aclrtContext ctx;
aclrtStream stream;
};
typedef struct Tools {
Resource src;
CnnCls* mRainCls;
DvppProcessx* dvpp;
}Tools;
int mrc_init(void** handle, mrc_param param) {
int ret = SY_SUCCESS;
Tools* tools = new Tools;
// init resource
// ACL_CALL(aclInit(nullptr), ACL_SUCCESS, SY_FAILED);
// ACL_CALL(aclrtSetDevice(param.devId), ACL_SUCCESS, SY_FAILED);
// ACL_CALL(aclrtCreateContext(&tools->src.ctx, param.devId), ACL_SUCCESS, SY_FAILED);
ACL_CALL(aclrtCreateStream(&tools->src.stream), ACL_SUCCESS, SY_FAILED);
// motor rainshed classfication init
tools->mRainCls = new CnnCls();
tools->mRainCls->config.confThr = param.thresld;
ret = tools->mRainCls->Init(param.modelNames);
if (ret != SY_SUCCESS) {
delete tools->mRainCls;
tools->mRainCls = nullptr;
return SY_FAILED;
}
tools->dvpp = new DvppProcessx();
tools->dvpp->InitResource(tools->src.stream);
*handle = tools;
return SY_SUCCESS;
}
// double msecond() {
// struct timeval tv;
// gettimeofday(&tv, 0);
// return (tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0);
// }
int mrc_batch(void * handle, sy_img *img_data_array, int batch_size, mrc_result *result) {
Tools* tools = (Tools*) handle;
int inputW = tools->mRainCls->GetInputWidth();
int inputH = tools->mRainCls->GetInputHeight();
// printf("debug inputw:%d,inputh:%d\n",inputW,inputH);
for (int i = 0; i < batch_size; i++) {
if (img_data_array[i].data_ == NULL || img_data_array[i].w_ == 0 || img_data_array[i].h_ == 0) {
ERROR_LOG("mRainCls get null input ptr!");
return SY_FAILED;
}
ImageData resizeImg, src;
//debug========================================================================
// src.width = img_data_array[i].w_;
// src.height = img_data_array[i].h_;
// src.alignWidth = ((src.width + 127) & ~(127));
// src.alignHeight = ((src.height + 15) & ~(15));
// src.size = src.alignWidth * src.alignHeight * 1.5;
// src.data.reset((uint8_t*)img_data_array[i].data_, [](uint8_t* p) { acldvppFree((void *)p); });
//debug end====================================================================
// Utils::CopysyImageDataToDvpp(src, img_data_array[i]);
ACL_CALL(Utils::CopysyImageDataToDvppV2(src, img_data_array[i]), SY_SUCCESS, SY_FAILED);
ACL_CALL(tools->dvpp->CropAndPaste(resizeImg, src, inputW, inputH), SY_SUCCESS, SY_FAILED);
// forward
//double t1, t2;
//t1 = msecond();
int ret = tools->mRainCls->Inference(resizeImg);
if (ret != SY_SUCCESS) {
return SY_MODEL_FORWARD_ERROR;
}
//t2 = msecond();
//printf("debug infer time: %.2f\n", t2 - t1);
vector<float> mRainClsRes;
ret = tools->mRainCls->PostProcess(mRainClsRes);
if (ret != SY_SUCCESS) {
return SY_MODEL_GETRESULT_ERROR;
}
result[i].index = mRainClsRes[0];
result[i].score = mRainClsRes[1];
// printf("debug index:%d,confidence:%f\n",result[i].index,result[i].score);
// for (auto res : mRainClsRes) {
// printf("debug info: %f\n",res);
// }
}
return SY_SUCCESS;
}
void mrc_release(void **handle) {
Tools* tools = (Tools*) handle;
// printf("debug line:%d\n",__LINE__);
if (tools) {
if (tools->mRainCls) {
// delete tools->mRainCls;
tools->mRainCls = nullptr;
}
// printf("debug line:%d\n",__LINE__);
if (tools->dvpp) {
// delete tools->dvpp;
tools->dvpp = nullptr;
}
//printf("debug line:%d\n",__LINE__);
// aclFinalize();
aclrtDestroyStream(&tools->src.stream);
// delete tools;
tools = NULL;
//printf("debug line:%d\n",__LINE__);
}
}
const char * mrc_get_version() {
return "motorrc_arm_vdec_310p_v0.0.1.20230921_without_timelimit";
}