From 8c36971afa38f8a2fb058704d6c584e0ce0c2bc4 Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期四, 02 二月 2023 17:59:22 +0800 Subject: [PATCH] add go api --- shmparser/shmparser.go | 192 +++++++++++++++++++++++++++ shmparser/std_target.h | 0 shmparser/shmparser.h | 40 +++++ ctest/main.c | 2 go.mod | 3 main.go | 9 + shmparser/shmparser.c | 163 +++++++++++++++++++++++ c_shm | 0 CMakeLists.txt | 4 9 files changed, 409 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a2dfe1..4b85d8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ ${CMAKE_SOURCE_DIR} ) -add_library(${BIN} SHARED shmparser.c) +add_library(${BIN} SHARED shmparser/shmparser.c) target_link_libraries(${BIN}) -add_executable(test shmparser.c main.c) \ No newline at end of file +add_executable(test shmparser/shmparser.c ctest/main.c) \ No newline at end of file diff --git a/c_shm b/c_shm new file mode 100755 index 0000000..5f45ccb --- /dev/null +++ b/c_shm Binary files differ diff --git a/main.c b/ctest/main.c similarity index 99% rename from main.c rename to ctest/main.c index 0297891..940becf 100644 --- a/main.c +++ b/ctest/main.c @@ -2,7 +2,7 @@ #include <string.h> #include <stdlib.h> #include <errno.h> -#include "shmparser.h" +#include "shmparser/shmparser.h" static char* timestamp = "2023-1-31 10:05:56.998"; static char* cid = "cameraid"; diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f54bfe9 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module goshm + +go 1.14 diff --git a/main.go b/main.go new file mode 100644 index 0000000..b7323ea --- /dev/null +++ b/main.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +import "goshm/shmparser" + +func main() { + fmt.Println("hello") +} diff --git a/shmparser.c b/shmparser/shmparser.c similarity index 78% rename from shmparser.c rename to shmparser/shmparser.c index 06f0857..1ad70dc 100644 --- a/shmparser.c +++ b/shmparser/shmparser.c @@ -3,6 +3,7 @@ #include <stdlib.h> #include <string.h> #include <stdint.h> +#include <stdio.h> enum{ IMG_DATA = 1, @@ -430,7 +431,7 @@ Put64(pheader, packHeader(RULE_HANDLETRACK, dummy_val + 3 + st_size)); if (dummy_val + 3 + st_size > SIZEHT) - printf("handletrack_size Must Less Than %d But Now %d\n", SIZEHT, dummy_val + 3 + st_size); + printf("handletrack_size Must Less Than %lu But Now %lu\n", SIZEHT, dummy_val + 3 + st_size); // 涓嬩竴涓� field tmp += SIZEHT - dummy_val; @@ -546,3 +547,163 @@ free(rule->sdk); free(rule); } + +/////////////////////////////////////////////////////////////// +// go need function + +void goimage2shm(void* shm, void* img){ + struct stimg* cimg = (struct stimg*)img; + image2shm(shm, cimg); +} + +void goimageinfo(void* img, void** data, uint64_t* size, + void** ts, uint32_t* tss, + void** cid, uint32_t* cids, + void** cname, uint32_t* cnames, + uint64_t* id, uint32_t* w, uint32_t* h) +{ + struct stimg* cimg = (struct stimg*)img; + *data = cimg->data; + *size = cimg->data_size; + + *ts = cimg->timestamp; + *tss = cimg->timestamp_size; + + *cid = cimg->camera_id; + *cids = cimg->camera_id_size; + + *cname = cimg->camera_name; + *cnames = cimg->camera_name_size; + + *id = cimg->id; + *w = cimg->width; + *h = cimg->height; +} + +void gotargetsdel(void* tgts){ + if (tgts) { + struct sttgt* t = (struct sttgt*)tgts; + free(t); + } +} + +void* gotargetsnew(const uint32_t size){ + struct sttgt* t = (struct sttgt*)calloc(size, sizeof(struct sttgt)); + return t; +} + +void gotargetsadd(void* tgts, int idx, uint64_t id, char* type, uint32_t tsize, int32_t confidence, + int left, int top, int right, int bottom, + unsigned char* feat, uint32_t featsize, + unsigned char* attr, uint32_t attrsize) +{ + struct sttgt* ts = (struct sttgt*)tgts; + struct sttgt t; + memset(&t, 0, sizeof(t)); + + t.id = id; + t.type = type; + t.type_size = tsize; + t.confidence = confidence; + t.rect.left = left; + t.rect.top = top; + t.rect.right = right; + t.rect.bottom = bottom; + t.feature = feat; + t.feature_size = featsize; + t.attribute = attr; + t.attribute_size = attrsize; + + ts[idx] = t; +} + +void goruleaddsdk(void* shm, void* tgts, uint32_t tgtcnt, + char* type, uint32_t tsize, + char* timestamp, uint32_t tmsize) +{ + TResult* res = (TResult*)calloc(1, sizeof(TResult)); + res->count = tgtcnt; + res->targets = (Target*)calloc(tgtcnt, sizeof(Target)); + + struct sttgt* ts = (struct sttgt*)tgts; + + for(uint32_t i = 0; i < tgtcnt; i++){ + Target t; + memset(&t, 0, sizeof(t)); + t.id = ts[i].id; + t.confidence = ts[i].confidence; + memcpy(&t.rect, &ts[i].rect, sizeof(t.rect)); + memcpy(t.type, ts[i].type, ts[i].type_size); + t.feature = (char*)ts[i].feature; + t.feature_size = ts[i].feature_size; + t.attribute = (char*)ts[i].attribute; + t.attribute_size = ts[i].attribute_size; + + res->targets[i] = t; + } + + add_result2rule_in_shm(shm, res, type, tsize, timestamp, tmsize); + free(res->targets); + free(res); +} + +//////////////////// unserialize + +void gorulehandletrack(void* rule, void** data, uint32_t* size){ + struct strule* r = (struct strule*)rule; + *data = r->handletrack; + *size = r->handletrack_size; +} + +void goruledatatype(void* rule, void** data, uint32_t* size){ + struct strule* r = (struct strule*)rule; + *data = r->datatype; + *size = r->datatype_size; +} + +void gorulesdk(void* rule, void** data, uint32_t* size){ + struct strule* r = (struct strule*)rule; + *data = r->sdk; + *size = r->sdk_count; +} + +void gorulesdkinfo(void* sdk, int idx, void** t, uint32_t* ts, + void** i, uint32_t* is, void** n, uint32_t* ns, + void** tm, uint32_t* tms, void** data, uint32_t* size) +{ + struct stsdk* s = (struct stsdk*)sdk; + *t = s->sdktype; + *ts = s->sdktype_size; + + *i = s->sdkid; + *is = s->sdkid_size; + + *n = s->sdkname; + *ns = s->sdkname_size; + + *tm = s->timestamp; + *tms = s->timestamp_size; + *data = s->tgt; + *size = s->tgt_count; +} + +void gorulesdktargetinfo(void* tgt, int idx, uint64_t* id, int32_t* c, + int32_t* l,int32_t* t,int32_t* r,int32_t* b, + void** tp, uint32_t* tps, + void** feat, uint32_t* fs, void** attr, uint32_t* attrs) +{ + struct sttgt* tt = (struct sttgt*)tgt; + *id = tt->id; + *c = tt->confidence; + *l = tt->rect.left; + *t = tt->rect.top; + *r = tt->rect.right; + *b = tt->rect.bottom; + *tp = tt->type; + *tps = tt->type_size; + *feat = tt->feature; + *fs = tt->feature_size; + *attr = tt->attribute; + *attrs = tt->attribute_size; +} + diff --git a/shmparser/shmparser.go b/shmparser/shmparser.go new file mode 100644 index 0000000..56e6de0 --- /dev/null +++ b/shmparser/shmparser.go @@ -0,0 +1,192 @@ +package shmparser + +/* +#cgo LDFLAGS: -ldl +#include <stdlib.h> +#include "shmparser.h" +*/ +import "C" +import ( + "strings" + "time" + "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 string, data []byte, w, h int, fid int64) { + timestamp := strings.Replace(time.Now().Format("2006-01-02 15:04:05.000"), ".", ":", -1) + + 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)) +} + +// ImageShm Image struct in Shm +type ImageShm struct { + Data []byte + Width int + Height int + Timestamp string + Id uint64 + Cid string + Cname string +} + +func Shm2Image(shm []byte) ImageShm { + cimg := C.shm2image(unsafe.Pointer(&shm[0])) + + var goimg ImageShm + const maxLen = 0xffffffffffff + + 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.goimageinfo(p, &data, &sd, &ts, &sts, &cid, &scid, &cname, &scname, &id, &w, &h) + goimg.Data = (*[maxLen]byte)(data)[:uint64(sd):uint64(sd)] + + tmp := (*[maxLen]byte)(ts)[:int(sts):int(sts)] + goimg.Timestamp = byte2str(tmp) + + tmp = (*[maxLen]byte)(cid)[:int(scid):int(scid)] + goimg.Cid = byte2str(tmp) + + tmp = (*[maxLen]byte)(cname)[:int(scname):int(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.gotargetsadd(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.goruleaddsdk(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])) + + const maxLen = 0xffffffffffff + + var data unsafe.Pointer + var s32 C.uint + + p := unsafe.Pointer(crule) + C.gorulehandletrack(p, &data, &s32) + tmp := (*[maxLen]byte)(data)[:int(s32):int(s32)] + rule.Handletrack = byte2str(tmp) + + C.goruledatatype(p, &data, &s32) + tmp = (*[maxLen]byte)(data)[:int(s32):int(s32)] + rule.Datatype = byte2str(tmp) + + //////////////////////////////////////////////// + + C.free_strule(crule) + + return rule +} diff --git a/shmparser.h b/shmparser/shmparser.h similarity index 63% rename from shmparser.h rename to shmparser/shmparser.h index bce3460..9fe8732 100644 --- a/shmparser.h +++ b/shmparser/shmparser.h @@ -107,6 +107,46 @@ struct strule* shm2rule(void* shm); void free_strule(struct strule* rule); +/////////////////////////////////////////////////////////////// +// go need function +// go image +void goimage2shm(void* shm, void* img); +void goimageinfo(void* img, void** data, uint64_t* size, + void** ts, uint32_t* tss, + void** cid, uint32_t* cids, + void** cname, uint32_t* cnames, + uint64_t* id, uint32_t* w, uint32_t* h); + +// go rule/sdk/result +void gotargetsdel(void* tgts); +void* gotargetsnew(const uint32_t size); +void gotargetsadd(void* tgts, int idx, uint64_t id, char* type, uint32_t tsize, int32_t confidence, + int left, int top, int right, int bottom, + unsigned char* feat, uint32_t featsize, + unsigned char* attr, uint32_t attrsize); +void goruleaddsdk(void* shm, void* tgts, uint32_t tgtcnt, + char* type, uint32_t tsize, + char* timestamp, uint32_t tmsize); + +// unserialize +void gorulehandletrack(void* rule, void** data, uint32_t* size); +void goruledatatype(void* rule, void** data, uint32_t* size); + +void gorulesdk(void* rule, void** data, uint32_t* size); +void gorulesdkinfo(void* sdk, int idx, void** t, uint32_t* ts, + void** i, uint32_t* is, void** n, uint32_t* ns, + void** tm, uint32_t* tms, void** data, uint32_t* size); +void gorulesdktargetinfo(void* tgt, int idx, uint64_t* id, int32_t* c, + int32_t* l,int32_t* t,int32_t* r,int32_t* b, + void** tp, uint32_t* tps, + void** feat, uint32_t* fs, void** attr, uint32_t* attrs); +////////////////////////////////////////////////////////////// +// no use +void* gosdknew(); +void gosdkadd(void* sdk, char* type, uint32_t tsize, char* id, uint32_t idsize, + char* name, uint32_t nsize, char* timestamp, uint32_t tmsize, + uint32_t tgtcnt, void* tgts); + #ifdef __cplusplus } #endif diff --git a/std_target.h b/shmparser/std_target.h similarity index 100% rename from std_target.h rename to shmparser/std_target.h -- Gitblit v1.8.0