cheliequan
2023-02-07 2a6e62b3a521d39f390e615cf5d11cf024cb0df6
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package shmparser
 
/*
#cgo LDFLAGS: -ldl
#include <stdlib.h>
#include "shmparser.h"
*/
import "C"
import (
    "unsafe"
)
 
func str2byte(s string) []byte {
    x := (*[2]uintptr)(unsafe.Pointer(&s))
    h := [3]uintptr{x[0], x[1], x[1]}
    return *(*[]byte)(unsafe.Pointer(&h))
}
 
func byte2str(d []byte) string {
    return *(*string)(unsafe.Pointer(&d))
}
 
// Image2Shm fill image to shm
func Image2Shm(shm []byte, cid, cname, timestamp string, data []byte, w, h int, fid int64) {
 
    dataptr := (*C.uchar)(unsafe.Pointer(&data[0]))
    cidbyte := str2byte(cid)
    cidptr := (*C.char)(unsafe.Pointer(&cidbyte[0]))
    cnamebyte := str2byte(cname)
    cnameptr := (*C.char)(unsafe.Pointer(&cnamebyte[0]))
 
    tsbyte := str2byte(timestamp)
    tsptr := (*C.char)(unsafe.Pointer(&tsbyte[0]))
 
    cimg := C.make_image_ref(C.ulong(fid),
        dataptr, C.uint(len(data)), C.uint(w), C.uint(h),
        tsptr, C.uint(len(timestamp)),
        cidptr, C.uint(len(cid)),
        cnameptr, C.uint(len(cname)),
    )
 
    shmptr := unsafe.Pointer(&shm[0])
    C.goImage2Shm(shmptr, unsafe.Pointer(cimg))
    C.free_stimg(cimg)
}
 
// ImageShm Image struct in Shm
type ImageShm struct {
    Data      []byte
    Width     int
    Height    int
    Timestamp string
    Id        uint64
    Cid       string
    Cname     string
}
 
func uintptr2byte(p unsafe.Pointer, size uint64) []byte {
    if (^uint(0) >> 63) == 0 {
        const maxLen = 0x7fffffff
        return (*(*[maxLen]byte)(p))[:size:size]
    } else {
        const maxLen = 0xffffffffffff
        return (*(*[maxLen]byte)(p))[:size:size]
    }
}
 
func Shm2Image(shm []byte) ImageShm {
    cimg := C.shm2image(unsafe.Pointer(&shm[0]))
 
    var goimg ImageShm
 
    var data, ts, cid, cname unsafe.Pointer
    var sd, id C.ulong
    var sts, scid, scname, w, h C.uint
 
    p := unsafe.Pointer(cimg)
 
    C.goImageGet(p, &data, &sd, &ts, &sts, &cid, &scid, &cname, &scname, &id, &w, &h)
    goimg.Data = uintptr2byte(data, uint64(sd))
 
    tmp := uintptr2byte(ts, uint64(sts))
    goimg.Timestamp = byte2str(tmp)
 
    tmp = uintptr2byte(cid, uint64(scid))
    goimg.Cid = byte2str(tmp)
 
    tmp = uintptr2byte(cname, uint64(scname))
    goimg.Cname = byte2str(tmp)
 
    goimg.Width = int(w)
    goimg.Height = int(h)
    goimg.Id = uint64(id)
 
    C.free_stimg(cimg)
 
    return goimg
}
 
// Rect struct
type Rect struct {
    Left   int
    Top    int
    Right  int
    Bottom int
}
 
// Target analysis Target
type Target struct {
    Id         uint64
    Type       string
    Confidence int
    Rc         Rect
    Feat       []byte
    Attr       []byte
}
 
// Resdk result of sdk
type Resdk struct {
    Type      string
    Id        string
    Name      string
    Timestamp string
    Targets   []Target
}
 
// Strule struct of rule message
type Strule struct {
    Handletrack string
    Datatype    string
    Sdks        []Resdk
}
 
// Rule2Shm rule message init to shm
func Rule2Shm(shm []byte, handletrack string) {
    shmptr := unsafe.Pointer(&shm[0])
    ht := str2byte(handletrack)
    htptr := (*C.char)(unsafe.Pointer(&ht[0]))
 
    C.rule2shm(shmptr, htptr, C.uint(len(handletrack)))
}
 
// AddResSdk2RuleInShm add sdk message to rule in shm
func AddResSdk2RuleInShm(shm []byte, sdk Resdk) {
 
    ctgts := C.goTargetsNew(C.uint(len(sdk.Targets)))
    for k, v := range sdk.Targets {
        tpbyte := str2byte(v.Type)
        tptr := (*C.char)(unsafe.Pointer(&tpbyte[0]))
        featptr := (*C.uchar)(unsafe.Pointer(&v.Feat[0]))
        attrptr := (*C.uchar)(unsafe.Pointer(&v.Attr[0]))
 
        C.goTargetAdd(ctgts, C.int(k), C.ulong(v.Id), tptr, C.uint(len(v.Type)),
            C.int(v.Confidence),
            C.int(v.Rc.Left), C.int(v.Rc.Top), C.int(v.Rc.Right), C.int(v.Rc.Bottom),
            featptr, C.uint(len(v.Feat)), attrptr, C.uint(len(v.Attr)))
    }
 
    shmptr := unsafe.Pointer(&shm[0])
    ts := str2byte(sdk.Timestamp)
    tsptr := (*C.char)(unsafe.Pointer(&ts[0]))
 
    tp := str2byte(sdk.Type)
    tpstr := (*C.char)(unsafe.Pointer(&tp[0]))
 
    C.goSdkPut(shmptr, ctgts, C.uint(len(sdk.Targets)),
        tpstr, C.uint(len(sdk.Type)),
        tsptr, C.uint(len(sdk.Timestamp)))
 
    C.goTargetsDel(ctgts)
}
 
// Shm2Rule get Rule from shm
func Shm2Rule(shm []byte) Strule {
 
    var rule Strule
 
    crule := C.shm2rule(unsafe.Pointer(&shm[0]))
 
    var ht unsafe.Pointer
    var sht C.uint
 
    p := unsafe.Pointer(crule)
    C.goHandletrackGet(p, &ht, &sht)
    tmp := uintptr2byte(ht, uint64(sht))
    rule.Handletrack = byte2str(tmp)
 
    var dt unsafe.Pointer
    var sdt C.uint
    C.goDatatypeGet(p, &dt, &sdt)
    tmp = uintptr2byte(dt, uint64(sdt))
    rule.Datatype = byte2str(tmp)
 
    ////////////////////////////////////////////////
 
    var csdk unsafe.Pointer
    var s32 C.uint
    C.goSdkArrayGet(p, &csdk, &s32)
 
    for i := 0; i < int(s32); i++ {
        var ctype, cid, cname, cts, ctgt unsafe.Pointer
        var st, si, sn, sts, stgt C.uint
        var sdk Resdk
 
        C.goSdkGet(csdk, C.int(i), &ctype, &st, &cid, &si,
            &cname, &sn, &cts, &sts, &ctgt, &stgt)
        tmp = uintptr2byte(ctype, uint64(st))
        sdk.Type = byte2str(tmp)
 
        tmp = uintptr2byte(cid, uint64(si))
        sdk.Id = byte2str(tmp)
 
        tmp = uintptr2byte(cname, uint64(sn))
        sdk.Name = byte2str(tmp)
        tmp = uintptr2byte(cts, uint64(sts))
        sdk.Timestamp = byte2str(tmp)
 
        for j := 0; j < int(stgt); j++ {
            var tid C.ulong
            var tc, tl, tt, tr, tb C.int
            var tp, tfeat, tattr unsafe.Pointer
            var stp, stf, sta C.uint
 
            var tgt Target
 
            C.goTargetGet(ctgt, C.int(j), &tid, &tc, &tl, &tt, &tr, &tb,
                &tp, &stp, &tfeat, &stf, &tattr, &sta)
 
            tgt.Id = uint64(tid)
            tgt.Confidence = int(tc)
            tgt.Rc.Left = int(tl)
            tgt.Rc.Top = int(tt)
            tgt.Rc.Right = int(tr)
            tgt.Rc.Bottom = int(tb)
 
            tmp = uintptr2byte(tp, uint64(stp))
            tgt.Type = byte2str(tmp)
 
            tmp = uintptr2byte(tfeat, uint64(stf))
            tgt.Feat = tmp
 
            tmp = uintptr2byte(tattr, uint64(sta))
            tgt.Attr = tmp
 
            sdk.Targets = append(sdk.Targets, tgt)
        }
 
        rule.Sdks = append(rule.Sdks, sdk)
    }
 
    C.free_strule(crule)
 
    return rule
}