派生自 Algorithm/baseDetector

Scheaven
2021-01-05 6ae75cc17b2952c63a79ff2c86da841f0dbbf3c6
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "sdk.h"
 
#include <stdio.h>
#include <dlfcn.h>
#include <sys/shm.h>
#include <time.h>
#include <string.h>
 
#ifdef __cplusplus //
extern "C"
{
#endif
#include "std_target.h"
 
// sdk interface func name
// sdk-interface-name
const static char sin_create[]          = "create";
 
const static char sin_get_result_face[]          = "get_result_face";
const static char sin_set_data[]          = "set_data";
const static char sin_op_channel[]          = "op_channel";
const static char sin_set_cb[]          = "set_cb";
 
const static char sin_release[]         = "release";
const static char sin_get_result[]      = "get_result";
const static char sin_release_result[]  = "release_result";
 
 
#define check(lib, fn, log, lib_name, fn_name, ret) do{ \
    if (!fn){ \
        dlclose(lib); \
        lib=NULL; \
        printf("dlsym func \"%s\" from file \"%s\" failed", fn_name, lib_name); \
        return ret; \
    } \
}while(0)
 
namespace container
{
    sdk::sdk(const char *id)
    :libsdk_(nullptr)
    ,sdk_handle_(nullptr)
    ,fn_create(nullptr)
 
    ,fn_get_result_face(nullptr)
    ,fn_set_data(nullptr)
    ,fn_op_channel(nullptr)
    ,fn_set_cb(nullptr)
 
    ,fn_release(nullptr)
    ,fn_get_result(nullptr)
    ,fn_release_result(nullptr)
    ,id_or_name_(id)
    {}
    
    sdk::~sdk()
    {
        release();
    }
    
    int sdk::release(){
 
        if (sdk_handle_ && fn_release){
            fn_release(sdk_handle_);
        }
        if (libsdk_){
            dlclose(libsdk_);
            libsdk_ = NULL;
        }
 
        return 0;
    }
 
    int sdk::fndd(void *args, const int sender_chan, const char *recver, const char *data, const int len)
    {
        printf("call back func \n");
        fn_set_data(sdk_handle_, data, strlen(data), 1);
        return 0;
    }
 
    int sdk::init(const char *so, const char *conf){
        if (libsdk_) return 0;
 
        libsdk_ = dlopen(so, RTLD_LAZY);
        if (!libsdk_) {
            printf("sdk dlopen %s conf %s error %s\n", so, conf, dlerror());
            return -1;
        }
 
        // MUST EXIST FUNCTIONS
        fn_create = (fn_sdk_create)dlsym(libsdk_, sin_create);
        check(libsdk_, fn_create, fn_log_, so, sin_create, -1);
 
 
 
        fn_get_result_face = (fn_sdk_get_result_face)dlsym(libsdk_, sin_get_result_face);
        check(libsdk_, fn_get_result_face, fn_log_, so, sin_get_result_face, -1);
 
        fn_set_data = (fn_sdk_set_data)dlsym(libsdk_, sin_set_data);
        check(libsdk_, fn_set_data, fn_log_, so, sin_set_data, -1);
 
        fn_op_channel = (fn_sdk_op_channel)dlsym(libsdk_, sin_op_channel);
        check(libsdk_, fn_op_channel, fn_log_, so, sin_op_channel, -1);
 
        fn_set_cb = (fn_sdk_set_cb)dlsym(libsdk_, sin_set_cb);
        check(libsdk_, fn_set_cb, fn_log_, so, sin_set_cb, -1);
 
 
 
 
        fn_release = (fn_sdk_release)dlsym(libsdk_, sin_release);
        check(libsdk_, fn_release, fn_log_, so, sin_release, -1);
        
        fn_get_result = (fn_sdk_get_result)dlsym(libsdk_, sin_get_result);
        check(libsdk_, fn_get_result, fn_log_, so, sin_get_result, -1);
 
        fn_release_result = (fn_sdk_release_result)dlsym(libsdk_, sin_release_result);
        check(libsdk_, fn_release_result, fn_log_, so, sin_release_result, -1);
 
        int max_chan = 7;
        if ((sdk_handle_ = fn_create(conf, &max_chan)) == NULL){
            printf("sdk create sdk handle failed");
            return -1;
        }
 
        const char* data = "[[{\"cameraId\":\"b0b766c2-e29f-432f-aabb-0bedd36c604b\",\"polygonId\":\"7196c66f-5b82-449c-a08c-9f53a5d48ed4\",\"polygon\":[{\"x\":397,\"y\":348},{\"x\":72,\"y\":537},{\"x\":953,\"y\":539}],\"coordTrans\":[{\"x0\":261,\"y0\":0,\"x1\":0,\"y1\":0},{\"x0\":427,\"y0\":474,\"x1\":33,\"y1\":234}]},{\"cameraId\":\"18e2c6f1-fd3e-4bc6-b878-5a38228cebe8\",\"polygonId\":\"20b88549-6c41-4b30-844b-088215095995\",\"polygon\":[{\"x\":197,\"y\":179},{\"x\":252,\"y\":532},{\"x\":675,\"y\":535},{\"x\":470,\"y\":154},{\"x\":470,\"y\":154},{\"x\":378,\"y\":185}],\"coordTrans\":[{\"x0\":304,\"y0\":0,\"x1\":0,\"y1\":0},{\"x0\":415,\"y0\":372,\"x1\":1235,\"y1\":432}]}]]";
        fn_set_data(sdk_handle_, data, strlen(data), 0);
 
        const char* map_chan = "{\"name\":\"b0b766c2-e29f-432f-aabb-0bedd36c604b\",\"id\":0}";
        fn_op_channel(sdk_handle_, map_chan, strlen(map_chan), 1);
        map_chan = "{\"name\":\"18e2c6f1-fd3e-4bc6-b878-5a38228cebe8\",\"id\":1}";
        fn_op_channel(sdk_handle_, map_chan, strlen(map_chan), 1);
        char* aa= "ddf";
//        fn_set_cb(sdk_handle_, sdk::fndd, aa);
 
        if (max_chan <= 0) max_chan = 16;
        return 0;
    }
 
    int sdk::run(void *pic_data, int w, int h, int chn) {
 
        TImage img;
        img.width = w;
        img.height = h;
        img.channel = chn;
        img.data = (unsigned char *)pic_data;
        const int face_info_size = 0;
        char* face_info;
 
//        void * sr = fn_get_result(sdk_handle_, &img, 0);
        void * sr = fn_get_result_face(sdk_handle_, &img, 0, face_info, face_info_size);
        TResult * t_result = (TResult*) sr;
 
       printf("t_result->count==%d\n", t_result->count);
       for (int i = 0; i <t_result->count ; ++i) {
            printf("=======id:%d confidence:%d left:%d top:%d \n",t_result->targets[i].id,t_result->targets[i].confidence , t_result->targets[i].rect.left, t_result->targets[i].rect.top );
        }
 
        fn_release_result(sr);
        //free(img);
        return 1;
    }
 
 
} // namespace container
#ifdef __cplusplus //
}
#endif