| | |
| | | package common |
| | | |
| | | import ( |
| | | "context" |
| | | "encoding/json" |
| | | "fmt" |
| | | "io/ioutil" |
| | | "strconv" |
| | | "time" |
| | | "context" |
| | | "encoding/json" |
| | | "fmt" |
| | | "io/ioutil" |
| | | "strconv" |
| | | "time" |
| | | |
| | | "basic.com/pubsub/protomsg.git" |
| | | "github.com/gogo/protobuf/proto" |
| | | "basic.com/pubsub/protomsg.git" |
| | | "github.com/gogo/protobuf/proto" |
| | | ) |
| | | |
| | | // GetIpcAddress get ipc |
| | | func GetIpcAddress(shm bool, id string) string { |
| | | if shm { |
| | | return id |
| | | } |
| | | return `ipc:///tmp/` + id + `.ipc` |
| | | if shm { |
| | | return id |
| | | } |
| | | return `ipc:///tmp/` + id + `.ipc` |
| | | } |
| | | |
| | | // SubConfig sub |
| | | type SubConfig struct { |
| | | SoFile string `json:"so_file_path"` |
| | | Env string `json:"runtime"` |
| | | Param map[string]string `json:"param"` |
| | | SoFile string `json:"so_file_path"` |
| | | Env string `json:"runtime"` |
| | | Param map[string]string `json:"param"` |
| | | } |
| | | |
| | | // SdkConfig sdk |
| | | type SdkConfig struct { |
| | | SoFile string `json:"so_file_path"` |
| | | Env string `json:"runtime"` |
| | | Param map[string]string `json:"param"` |
| | | Sub *SubConfig `json:"sub"` |
| | | SoFile string `json:"so_file_path"` |
| | | Env string `json:"runtime"` |
| | | Param map[string]string `json:"param"` |
| | | Sub *SubConfig `json:"sub"` |
| | | } |
| | | |
| | | // ReadConfig conf |
| | | func ReadConfig(file string) (SdkConfig, error) { |
| | | data, err := ioutil.ReadFile(file) |
| | | if err != nil { |
| | | return SdkConfig{}, fmt.Errorf("READ SDK CONFIG FILE %s ERROR", file) |
| | | } |
| | | data, err := ioutil.ReadFile(file) |
| | | if err != nil { |
| | | return SdkConfig{}, fmt.Errorf("READ SDK CONFIG FILE %s ERROR", file) |
| | | } |
| | | |
| | | //读取的数据为json格式,需要进行解码 |
| | | var v SdkConfig |
| | | err = json.Unmarshal(data, &v) |
| | | //读取的数据为json格式,需要进行解码 |
| | | var v SdkConfig |
| | | err = json.Unmarshal(data, &v) |
| | | |
| | | return v, err |
| | | return v, err |
| | | } |
| | | |
| | | // Atoi atoi |
| | | func Atoi(s string) int { |
| | | i, _ := strconv.Atoi(s) |
| | | return i |
| | | i, _ := strconv.Atoi(s) |
| | | return i |
| | | } |
| | | |
| | | // UnserilizeProto un |
| | | func UnserilizeProto(ctx context.Context, data <-chan []byte, out chan<- protomsg.SdkMessage, fn func(...interface{})) { |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | case d := <-data: |
| | | if len(d) < 100 { |
| | | continue |
| | | } |
| | | msg := protomsg.SdkMessage{} |
| | | if err := proto.Unmarshal(d, &msg); err != nil { |
| | | fn("UnserilizeProto Unmarshal msg 处理异常 Error:", err) |
| | | continue |
| | | } |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | case d := <-data: |
| | | if len(d) < 100 { |
| | | continue |
| | | } |
| | | msg := protomsg.SdkMessage{} |
| | | if err := proto.Unmarshal(d, &msg); err != nil { |
| | | fn("UnserilizeProto Unmarshal msg 处理异常 Error:", err) |
| | | continue |
| | | } |
| | | |
| | | out <- msg |
| | | out <- msg |
| | | |
| | | default: |
| | | time.Sleep(10 * time.Millisecond) |
| | | } |
| | | } |
| | | default: |
| | | time.Sleep(10 * time.Millisecond) |
| | | } |
| | | } |
| | | } |
| | | |
| | | // UnpackImage unpack |
| | | func UnpackImage(msg protomsg.SdkMessage, fnName string, fn func(...interface{})) *protomsg.Image { |
| | | // 反序列化数据得到sdk入参 |
| | | i := &protomsg.Image{} |
| | | err := proto.Unmarshal(msg.Data, i) |
| | | if err != nil { |
| | | fn(fnName, " protobuf Unmarshal decode CameraImage error: ", err.Error()) |
| | | return nil |
| | | } |
| | | if i.Data == nil { |
| | | fn(fnName, " protomsg.Image data null") |
| | | return nil |
| | | } |
| | | return i |
| | | // 反序列化数据得到sdk入参 |
| | | i := &protomsg.Image{} |
| | | err := proto.Unmarshal(msg.Data, i) |
| | | if err != nil { |
| | | fn(fnName, " protobuf Unmarshal decode CameraImage error: ", err.Error()) |
| | | return nil |
| | | } |
| | | if i.Data == nil { |
| | | fn(fnName, " protomsg.Image data null") |
| | | return nil |
| | | } |
| | | return i |
| | | } |
| | |
| | | package common |
| | | |
| | | import ( |
| | | "context" |
| | | "os" |
| | | "sync" |
| | | "time" |
| | | "context" |
| | | "os" |
| | | "sync" |
| | | "time" |
| | | ) |
| | | |
| | | // Disturber stop |
| | | type Disturber struct { |
| | | mtx sync.Mutex |
| | | live bool |
| | | until int |
| | | maxTry int |
| | | mtx sync.Mutex |
| | | live bool |
| | | until int |
| | | maxTry int |
| | | } |
| | | |
| | | // NewDisturber new |
| | | func NewDisturber(maxTry int) *Disturber { |
| | | return &Disturber{ |
| | | live: true, |
| | | until: 0, |
| | | maxTry: maxTry, |
| | | } |
| | | return &Disturber{ |
| | | live: true, |
| | | until: 0, |
| | | maxTry: maxTry, |
| | | } |
| | | } |
| | | |
| | | // Prevent prevent |
| | | func (d *Disturber) Prevent() { |
| | | d.mtx.Lock() |
| | | defer d.mtx.Unlock() |
| | | d.live = true |
| | | d.mtx.Lock() |
| | | defer d.mtx.Unlock() |
| | | d.live = true |
| | | } |
| | | |
| | | // MaybeReboot reboot |
| | | func (d *Disturber) MaybeReboot(ctx context.Context, fn func(...interface{})) { |
| | | d.live = true |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | default: |
| | | d.mtx.Lock() |
| | | running := d.live |
| | | d.mtx.Unlock() |
| | | d.live = true |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | default: |
| | | d.mtx.Lock() |
| | | running := d.live |
| | | d.mtx.Unlock() |
| | | |
| | | if running { |
| | | d.until = 0 |
| | | if running { |
| | | d.until = 0 |
| | | |
| | | d.mtx.Lock() |
| | | d.live = false |
| | | d.mtx.Unlock() |
| | | d.mtx.Lock() |
| | | d.live = false |
| | | d.mtx.Unlock() |
| | | |
| | | } else { |
| | | d.until++ |
| | | fn("!!!!!!No Running: ", d.until) |
| | | if d.until > d.maxTry { |
| | | fn("!!!!!!Too Long Running, Reboot: ", d.maxTry) |
| | | os.Exit(0) |
| | | } |
| | | } |
| | | time.Sleep(time.Second) |
| | | } |
| | | } |
| | | } else { |
| | | d.until++ |
| | | fn("!!!!!!No Running: ", d.until) |
| | | if d.until > d.maxTry { |
| | | fn("!!!!!!Too Long Running, Reboot: ", d.maxTry) |
| | | os.Exit(0) |
| | | } |
| | | } |
| | | time.Sleep(time.Second) |
| | | } |
| | | } |
| | | } |
| | |
| | | ,start_(false) |
| | | { |
| | | if(init(gpu_index, module_path) < 0){ |
| | | throw std::runtime_error("init swscale error"); |
| | | } |
| | | throw std::runtime_error("init swscale error"); |
| | | } |
| | | } |
| | | |
| | | reid::~reid() |
| | |
| | | |
| | | // ReID id |
| | | type ReID struct { |
| | | handle unsafe.Pointer |
| | | handle unsafe.Pointer |
| | | } |
| | | |
| | | // NewSDK new reid |
| | | func NewSDK(gpu int, module string) *ReID { |
| | | cmodule := C.CString(module) |
| | | defer C.free(unsafe.Pointer(cmodule)) |
| | | h := C.create_reid(C.int(gpu), cmodule) |
| | | return &ReID{h} |
| | | cmodule := C.CString(module) |
| | | defer C.free(unsafe.Pointer(cmodule)) |
| | | h := C.create_reid(C.int(gpu), cmodule) |
| | | return &ReID{h} |
| | | } |
| | | |
| | | // Free free |
| | |
| | | // Extract extract |
| | | // func (r *ReID) Extract(img []byte) []float32 { |
| | | |
| | | // var fSize C.int |
| | | // cfeat := C.extract(r.handle, (*C.uchar)(unsafe.Pointer(&img[0])), &fSize) |
| | | // if cfeat == nil { |
| | | // return nil |
| | | // } |
| | | // var fSize C.int |
| | | // cfeat := C.extract(r.handle, (*C.uchar)(unsafe.Pointer(&img[0])), &fSize) |
| | | // if cfeat == nil { |
| | | // return nil |
| | | // } |
| | | |
| | | // ret := make([]float32, 0, fSize) |
| | | // for i := 0; i < int(fSize); i++ { |
| | | // r := C.get_val(cfeat, C.int(i)) |
| | | // ret = append(ret, float32(r)) |
| | | // } |
| | | // C.free(unsafe.Pointer(cfeat)) |
| | | // return ret |
| | | // ret := make([]float32, 0, fSize) |
| | | // for i := 0; i < int(fSize); i++ { |
| | | // r := C.get_val(cfeat, C.int(i)) |
| | | // ret = append(ret, float32(r)) |
| | | // } |
| | | // C.free(unsafe.Pointer(cfeat)) |
| | | // return ret |
| | | // } |
| | | |
| | | // Extract2 extract |
| | | func (r *ReID) Extract2(img unsafe.Pointer, w, h, c int) []float32 { |
| | | |
| | | var fSize C.int |
| | | cfeat := C.extract(r.handle, (*C.uchar)(img), C.int(w), C.int(h), C.int(c), &fSize) |
| | | if cfeat == nil { |
| | | return nil |
| | | } |
| | | var fSize C.int |
| | | cfeat := C.extract(r.handle, (*C.uchar)(img), C.int(w), C.int(h), C.int(c), &fSize) |
| | | if cfeat == nil { |
| | | return nil |
| | | } |
| | | |
| | | ret := make([]float32, 0, fSize) |
| | | for i := 0; i < int(fSize); i++ { |
| | | r := C.get_val(cfeat, C.int(i)) |
| | | ret = append(ret, float32(r)) |
| | | } |
| | | C.free(unsafe.Pointer(cfeat)) |
| | | return ret |
| | | ret := make([]float32, 0, fSize) |
| | | for i := 0; i < int(fSize); i++ { |
| | | r := C.get_val(cfeat, C.int(i)) |
| | | ret = append(ret, float32(r)) |
| | | } |
| | | C.free(unsafe.Pointer(cfeat)) |
| | | return ret |
| | | } |
| | | |
| | | // Compare compare |
| | | func (r *ReID) Compare(feat1, feat2 []float32) float32 { |
| | | |
| | | p := C.compare(r.handle, (*C.float)(unsafe.Pointer(&feat1)), (*C.float)(unsafe.Pointer(&feat2))) |
| | | return float32(p) |
| | | p := C.compare(r.handle, (*C.float)(unsafe.Pointer(&feat1)), (*C.float)(unsafe.Pointer(&feat2))) |
| | | return float32(p) |
| | | } |
| | |
| | | package rpc |
| | | |
| | | import ( |
| | | "context" |
| | | "context" |
| | | |
| | | "time" |
| | | "time" |
| | | |
| | | "basic.com/valib/deliver.git" |
| | | "basic.com/valib/deliver.git" |
| | | ) |
| | | |
| | | // Reciever recv from ipc |
| | | type Reciever struct { |
| | | ctx context.Context |
| | | ipcURL string |
| | | out chan<- []byte |
| | | ctx context.Context |
| | | ipcURL string |
| | | out chan<- []byte |
| | | |
| | | shm bool |
| | | fnLogger func(...interface{}) |
| | | shm bool |
| | | fnLogger func(...interface{}) |
| | | } |
| | | |
| | | // NewReciever new recv |
| | | func NewReciever(url string, out chan<- []byte, shm bool, fn func(...interface{})) *Reciever { |
| | | return &Reciever{ |
| | | ipcURL: url, |
| | | out: out, |
| | | shm: shm, |
| | | fnLogger: fn, |
| | | } |
| | | return &Reciever{ |
| | | ipcURL: url, |
| | | out: out, |
| | | shm: shm, |
| | | fnLogger: fn, |
| | | } |
| | | } |
| | | |
| | | // Run run a IPC client |
| | | func (r *Reciever) Run(ctx context.Context) { |
| | | |
| | | if r.shm { |
| | | r.runShm(ctx) |
| | | } else { |
| | | r.run(ctx, deliver.NewServer(deliver.PushPull, r.ipcURL)) |
| | | } |
| | | if r.shm { |
| | | r.runShm(ctx) |
| | | } else { |
| | | r.run(ctx, deliver.NewServer(deliver.PushPull, r.ipcURL)) |
| | | } |
| | | } |
| | | |
| | | func (r *Reciever) run(ctx context.Context, i deliver.Deliver) { |
| | | |
| | | count := 0 |
| | | count := 0 |
| | | |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | i.Close() |
| | | return |
| | | default: |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | i.Close() |
| | | return |
| | | default: |
| | | |
| | | if r.shm { |
| | | if d, err := i.Recv(); err != nil { |
| | | i.Close() |
| | | r.fnLogger("Reciever RECV From:", r.ipcURL, " ERROR: ", err) |
| | | if r.shm { |
| | | if d, err := i.Recv(); err != nil { |
| | | i.Close() |
| | | r.fnLogger("Reciever RECV From:", r.ipcURL, " ERROR: ", err) |
| | | |
| | | c, err := deliver.NewServerWithError(deliver.Shm, r.ipcURL) |
| | | loopR: |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | default: |
| | | if err == nil { |
| | | break loopR |
| | | } |
| | | time.Sleep(time.Second) |
| | | c, err = deliver.NewServerWithError(deliver.Shm, r.ipcURL) |
| | | r.fnLogger("Recver ANALYSIS CREATE:", r.ipcURL, " FAILED : ", err) |
| | | } |
| | | } |
| | | i = c |
| | | r.fnLogger("Reciever CREATE SHM:", r.ipcURL) |
| | | } else { |
| | | if d != nil { |
| | | count++ |
| | | if count > 10 { |
| | | count = 0 |
| | | r.fnLogger("~~~shm recv image:", len(d)) |
| | | } |
| | | if len(d) > 2 { |
| | | r.out <- d |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | if msg, err := i.Recv(); err != nil { |
| | | // logo.Errorln("recv error : ", err, " url: ", r.ipcURL) |
| | | } else { |
| | | count++ |
| | | if count > 10 { |
| | | count = 0 |
| | | r.fnLogger("~~~mangos recv image:", len(msg)) |
| | | } |
| | | if len(msg) > 2 { |
| | | r.out <- msg |
| | | } |
| | | } |
| | | } |
| | | time.Sleep(10 * time.Millisecond) |
| | | } |
| | | } |
| | | c, err := deliver.NewServerWithError(deliver.Shm, r.ipcURL) |
| | | loopR: |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | default: |
| | | if err == nil { |
| | | break loopR |
| | | } |
| | | time.Sleep(time.Second) |
| | | c, err = deliver.NewServerWithError(deliver.Shm, r.ipcURL) |
| | | r.fnLogger("Recver ANALYSIS CREATE:", r.ipcURL, " FAILED : ", err) |
| | | } |
| | | } |
| | | i = c |
| | | r.fnLogger("Reciever CREATE SHM:", r.ipcURL) |
| | | } else { |
| | | if d != nil { |
| | | count++ |
| | | if count > 10 { |
| | | count = 0 |
| | | r.fnLogger("~~~shm recv image:", len(d)) |
| | | } |
| | | if len(d) > 2 { |
| | | r.out <- d |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | if msg, err := i.Recv(); err != nil { |
| | | // logo.Errorln("recv error : ", err, " url: ", r.ipcURL) |
| | | } else { |
| | | count++ |
| | | if count > 10 { |
| | | count = 0 |
| | | r.fnLogger("~~~mangos recv image:", len(msg)) |
| | | } |
| | | if len(msg) > 2 { |
| | | r.out <- msg |
| | | } |
| | | } |
| | | } |
| | | time.Sleep(10 * time.Millisecond) |
| | | } |
| | | } |
| | | } |
| | | |
| | | func (r *Reciever) runShm(ctx context.Context) { |
| | | c, err := deliver.NewServerWithError(deliver.Shm, r.ipcURL) |
| | | c, err := deliver.NewServerWithError(deliver.Shm, r.ipcURL) |
| | | loopRBegin: |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | default: |
| | | if err == nil { |
| | | break loopRBegin |
| | | } |
| | | time.Sleep(1 * time.Second) |
| | | c, err = deliver.NewServerWithError(deliver.Shm, r.ipcURL) |
| | | r.fnLogger("Recver CLIENT CREATE", r.ipcURL, "FAILED : ", err) |
| | | } |
| | | } |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | default: |
| | | if err == nil { |
| | | break loopRBegin |
| | | } |
| | | time.Sleep(1 * time.Second) |
| | | c, err = deliver.NewServerWithError(deliver.Shm, r.ipcURL) |
| | | r.fnLogger("Recver CLIENT CREATE", r.ipcURL, "FAILED : ", err) |
| | | } |
| | | } |
| | | |
| | | r.run(ctx, c) |
| | | r.run(ctx, c) |
| | | } |
| | |
| | | package rpc |
| | | |
| | | import ( |
| | | "context" |
| | | "time" |
| | | "context" |
| | | "time" |
| | | |
| | | "basic.com/valib/deliver.git" |
| | | "basic.com/valib/deliver.git" |
| | | ) |
| | | |
| | | // Sender decoder ingo |
| | | type Sender struct { |
| | | ipcURL string |
| | | in <-chan []byte |
| | | shm bool |
| | | ipcURL string |
| | | in <-chan []byte |
| | | shm bool |
| | | |
| | | fnLogger func(...interface{}) |
| | | fnLogger func(...interface{}) |
| | | } |
| | | |
| | | // NewSender Sender |
| | | func NewSender(ipcURL string, in <-chan []byte, shm bool, fn func(...interface{})) *Sender { |
| | | return &Sender{ |
| | | ipcURL: ipcURL, |
| | | in: in, |
| | | shm: shm, |
| | | fnLogger: fn, |
| | | } |
| | | return &Sender{ |
| | | ipcURL: ipcURL, |
| | | in: in, |
| | | shm: shm, |
| | | fnLogger: fn, |
| | | } |
| | | } |
| | | |
| | | // Run run a IPC producer |
| | | func (s *Sender) Run(ctx context.Context) { |
| | | |
| | | if s.shm { |
| | | s.runShm(ctx) |
| | | } else { |
| | | i := deliver.NewServer(deliver.PushPull, s.ipcURL) |
| | | if i == nil { |
| | | s.fnLogger("sender 2 pubsub nng create error") |
| | | return |
| | | } |
| | | s.run(ctx, i) |
| | | } |
| | | if s.shm { |
| | | s.runShm(ctx) |
| | | } else { |
| | | i := deliver.NewServer(deliver.PushPull, s.ipcURL) |
| | | if i == nil { |
| | | s.fnLogger("sender 2 pubsub nng create error") |
| | | return |
| | | } |
| | | s.run(ctx, i) |
| | | } |
| | | } |
| | | |
| | | func (s *Sender) run(ctx context.Context, i deliver.Deliver) { |
| | | |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | i.Close() |
| | | return |
| | | case d := <-s.in: |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | i.Close() |
| | | return |
| | | case d := <-s.in: |
| | | |
| | | if s.shm { |
| | | if err := i.Send(d); err != nil { |
| | | i.Close() |
| | | s.fnLogger("SENDER To:", s.ipcURL, " ERROR: ", err) |
| | | if s.shm { |
| | | if err := i.Send(d); err != nil { |
| | | i.Close() |
| | | s.fnLogger("SENDER To:", s.ipcURL, " ERROR: ", err) |
| | | |
| | | c, err := deliver.NewServerWithError(deliver.Shm, s.ipcURL) |
| | | loopS: |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | default: |
| | | if err == nil { |
| | | break loopS |
| | | } |
| | | time.Sleep(time.Second) |
| | | c, err = deliver.NewServerWithError(deliver.Shm, s.ipcURL) |
| | | s.fnLogger("Sender CLIENT CREATE:", s.ipcURL, " FAILED : ", err) |
| | | } |
| | | c, err := deliver.NewServerWithError(deliver.Shm, s.ipcURL) |
| | | loopS: |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | default: |
| | | if err == nil { |
| | | break loopS |
| | | } |
| | | time.Sleep(time.Second) |
| | | c, err = deliver.NewServerWithError(deliver.Shm, s.ipcURL) |
| | | s.fnLogger("Sender CLIENT CREATE:", s.ipcURL, " FAILED : ", err) |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | i = c |
| | | s.fnLogger("Sender Create Shm:", s.ipcURL) |
| | | } else { |
| | | i = c |
| | | s.fnLogger("Sender Create Shm:", s.ipcURL) |
| | | } else { |
| | | |
| | | } |
| | | } else { |
| | | err := i.Send(d) |
| | | if err != nil { |
| | | // logo.Errorln("error sender 2 pubsub: ", err) |
| | | } else { |
| | | s.fnLogger("mangos send to pubsub len: ", len(d)) |
| | | } |
| | | } |
| | | default: |
| | | time.Sleep(10 * time.Millisecond) |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | err := i.Send(d) |
| | | if err != nil { |
| | | // logo.Errorln("error sender 2 pubsub: ", err) |
| | | } else { |
| | | s.fnLogger("mangos send to pubsub len: ", len(d)) |
| | | } |
| | | } |
| | | default: |
| | | time.Sleep(10 * time.Millisecond) |
| | | } |
| | | } |
| | | } |
| | | |
| | | func (s *Sender) runShm(ctx context.Context) { |
| | | c, err := deliver.NewServerWithError(deliver.Shm, s.ipcURL) |
| | | c, err := deliver.NewServerWithError(deliver.Shm, s.ipcURL) |
| | | loopSBegin: |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | default: |
| | | if err == nil { |
| | | break loopSBegin |
| | | } |
| | | time.Sleep(1 * time.Second) |
| | | c, err = deliver.NewServerWithError(deliver.Shm, s.ipcURL) |
| | | s.fnLogger("Sender CLIENT CREATE:", s.ipcURL, "FAILED : ", err) |
| | | } |
| | | } |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | default: |
| | | if err == nil { |
| | | break loopSBegin |
| | | } |
| | | time.Sleep(1 * time.Second) |
| | | c, err = deliver.NewServerWithError(deliver.Shm, s.ipcURL) |
| | | s.fnLogger("Sender CLIENT CREATE:", s.ipcURL, "FAILED : ", err) |
| | | } |
| | | } |
| | | |
| | | s.run(ctx, c) |
| | | s.run(ctx, c) |
| | | } |
| | |
| | | #include <string.h> |
| | | void* crop_image(void *vsrc, int srcW, int srcH, int x0, int y0, int x1, int y1, int channel, int *length) |
| | | { |
| | | if (x0 < 0) x0 = 0; |
| | | if (x0 > srcW) x0 = srcW-1; |
| | | if (x1 < 0) x1 = 0; |
| | | if (x1 > srcW) x1 = srcW-1; |
| | | if (x0 < 0) x0 = 0; |
| | | if (x0 > srcW) x0 = srcW-1; |
| | | if (x1 < 0) x1 = 0; |
| | | if (x1 > srcW) x1 = srcW-1; |
| | | |
| | | if (y0 < 0) y0 = 0; |
| | | if (y0 > srcH) y0 = srcH-1; |
| | | if (y1 < 0) y1 = 0; |
| | | if (y1 > srcH) y1 = srcH-1; |
| | | if (y0 < 0) y0 = 0; |
| | | if (y0 > srcH) y0 = srcH-1; |
| | | if (y1 < 0) y1 = 0; |
| | | if (y1 > srcH) y1 = srcH-1; |
| | | |
| | | if (x1 - x0 <= 0 || y1 - y0 <= 0) return NULL; |
| | | if (x1 - x0 <= 0 || y1 - y0 <= 0) return NULL; |
| | | |
| | | if (x1-x0 > srcW) x1 = srcW-x0; |
| | | if (y1-y0 > srcH) y1 = srcH-y0; |
| | | if (x1-x0 > srcW) x1 = srcW-x0; |
| | | if (y1-y0 > srcH) y1 = srcH-y0; |
| | | |
| | | |
| | | unsigned char *src = (unsigned char*)vsrc; |
| | | |
| | | int destW = x1 - x0 + 1; |
| | | int destH = y1 - y0 + 1; |
| | | int destH = y1 - y0 + 1; |
| | | |
| | | *length = channel * destW * destH; |
| | | unsigned char * desData = (unsigned char*)malloc(*length); |
| | | *length = channel * destW * destH; |
| | | unsigned char * desData = (unsigned char*)malloc(*length); |
| | | |
| | | int i = 0; |
| | | int destIdy = 0; |
| | |
| | | memcpy(&(desData[destIdy * destW * channel]), &(src[(i * srcW + x0) * channel]),sizeof(char) * channel * destW); |
| | | } |
| | | |
| | | return desData; |
| | | return desData; |
| | | }*/ |
| | | import "C" |
| | | import ( |
| | | "context" |
| | | "time" |
| | | "unsafe" |
| | | "context" |
| | | "time" |
| | | "unsafe" |
| | | |
| | | "reid/rpc" |
| | | "reid/rpc" |
| | | |
| | | "reid/common" |
| | | "reid/common" |
| | | |
| | | "basic.com/valib/gogpu.git" |
| | | "basic.com/valib/gogpu.git" |
| | | |
| | | "basic.com/pubsub/protomsg.git" |
| | | "basic.com/pubsub/protomsg.git" |
| | | |
| | | "github.com/gogo/protobuf/proto" |
| | | "github.com/gogo/protobuf/proto" |
| | | ) |
| | | |
| | | type reid struct { |
| | | handle *ReID |
| | | fnLogger func(...interface{}) |
| | | handle *ReID |
| | | fnLogger func(...interface{}) |
| | | |
| | | gpu int |
| | | ipc string |
| | | gpu int |
| | | ipc string |
| | | } |
| | | |
| | | // Create Reid |
| | | func Create(config string, typ, id string, gpu int, shm bool, fn func(...interface{}), reserved map[string]interface{}) interface{} { |
| | | |
| | | cfg, err := common.ReadConfig(config) |
| | | if err != nil { |
| | | fn("Reid SDK Create Error When Read Config: ", err) |
| | | return nil |
| | | } |
| | | cfg, err := common.ReadConfig(config) |
| | | if err != nil { |
| | | fn("Reid SDK Create Error When Read Config: ", err) |
| | | return nil |
| | | } |
| | | |
| | | sModel, sGPU, sIPC := |
| | | "reid-model", |
| | | "gpu-memory", |
| | | "ipc-url" |
| | | sModel, sGPU, sIPC := |
| | | "reid-model", |
| | | "gpu-memory", |
| | | "ipc-url" |
| | | |
| | | params := []string{sModel, sGPU, sIPC} |
| | | params := []string{sModel, sGPU, sIPC} |
| | | |
| | | for _, v := range params { |
| | | if _, ok := cfg.Param[v]; !ok { |
| | | fn("Reid SDK Create Error Because of Param Not Found: ", v) |
| | | return nil |
| | | } |
| | | } |
| | | for _, v := range params { |
| | | if _, ok := cfg.Param[v]; !ok { |
| | | fn("Reid SDK Create Error Because of Param Not Found: ", v) |
| | | return nil |
| | | } |
| | | } |
| | | |
| | | gpuM := common.Atoi(cfg.Param[sGPU]) |
| | | gpuM := common.Atoi(cfg.Param[sGPU]) |
| | | |
| | | rGPU := gpu |
| | | rGPU := gpu |
| | | |
| | | if rGPU == -1 { |
| | | rGPU = gogpu.ValidGPU(gpuM + 512) |
| | | } |
| | | if rGPU == -1 { |
| | | rGPU = gogpu.ValidGPU(gpuM + 512) |
| | | } |
| | | |
| | | handle := NewSDK(rGPU, cfg.Param[sModel]) |
| | | if handle == nil { |
| | | fn("Reid SDK Create Error When New SDK") |
| | | return nil |
| | | } |
| | | handle := NewSDK(rGPU, cfg.Param[sModel]) |
| | | if handle == nil { |
| | | fn("Reid SDK Create Error When New SDK") |
| | | return nil |
| | | } |
| | | |
| | | return &reid{ |
| | | handle: handle, |
| | | fnLogger: fn, |
| | | return &reid{ |
| | | handle: handle, |
| | | fnLogger: fn, |
| | | |
| | | gpu: rGPU, |
| | | ipc: cfg.Param[sIPC], |
| | | } |
| | | gpu: rGPU, |
| | | ipc: cfg.Param[sIPC], |
| | | } |
| | | } |
| | | |
| | | // Run run |
| | | func Run(ctx context.Context, i interface{}) { |
| | | s := i.(*reid) |
| | | s := i.(*reid) |
| | | |
| | | const ( |
| | | postPull = `_2` |
| | | postPush = `_1` |
| | | ) |
| | | const ( |
| | | postPull = `_2` |
| | | postPush = `_1` |
| | | ) |
| | | |
| | | ipcSnd := s.ipc + postPush |
| | | ipcRcv := s.ipc + postPull |
| | | ipcSnd := s.ipc + postPush |
| | | ipcRcv := s.ipc + postPull |
| | | |
| | | sndURL := common.GetIpcAddress(true, ipcSnd) |
| | | rcvURL := common.GetIpcAddress(true, ipcRcv) |
| | | sndURL := common.GetIpcAddress(true, ipcSnd) |
| | | rcvURL := common.GetIpcAddress(true, ipcRcv) |
| | | |
| | | chSnd := make(chan []byte, 3) |
| | | chRcv := make(chan []byte, 3) |
| | | chSnd := make(chan []byte, 3) |
| | | chRcv := make(chan []byte, 3) |
| | | |
| | | recv := rpc.NewReciever(rcvURL, chRcv, true, s.fnLogger) |
| | | go recv.Run(ctx) |
| | | recv := rpc.NewReciever(rcvURL, chRcv, true, s.fnLogger) |
| | | go recv.Run(ctx) |
| | | |
| | | chMsg := make(chan protomsg.SdkMessage, 3) |
| | | go common.UnserilizeProto(ctx, chRcv, chMsg, s.fnLogger) |
| | | chMsg := make(chan protomsg.SdkMessage, 3) |
| | | go common.UnserilizeProto(ctx, chRcv, chMsg, s.fnLogger) |
| | | |
| | | send := rpc.NewSender(sndURL, chSnd, true, s.fnLogger) |
| | | go send.Run(ctx) |
| | | send := rpc.NewSender(sndURL, chSnd, true, s.fnLogger) |
| | | go send.Run(ctx) |
| | | |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | case msg := <-chMsg: |
| | | if len(msg.Tasklab.Sdkinfos) == 0 || int(msg.Tasklab.Index) >= len(msg.Tasklab.Sdkinfos) { |
| | | s.fnLogger("reid !!!!!! Recv Msg From Humantrack Error") |
| | | continue |
| | | } |
| | | i := common.UnpackImage(msg, "reid", s.fnLogger) |
| | | if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 { |
| | | s.fnLogger("reid !!!!!! Unpack Image From Humantrack Msg Failed") |
| | | continue |
| | | } |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | case msg := <-chMsg: |
| | | if len(msg.Tasklab.Sdkinfos) == 0 || int(msg.Tasklab.Index) >= len(msg.Tasklab.Sdkinfos) { |
| | | s.fnLogger("reid !!!!!! Recv Msg From Humantrack Error") |
| | | continue |
| | | } |
| | | i := common.UnpackImage(msg, "reid", s.fnLogger) |
| | | if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 { |
| | | s.fnLogger("reid !!!!!! Unpack Image From Humantrack Msg Failed") |
| | | continue |
| | | } |
| | | |
| | | sdkInfo := msg.Tasklab.Sdkinfos[int(msg.Tasklab.Index)] |
| | | s.fnLogger("reid~~~~~~Recv From Humantrack SDK Result Length: ", len(sdkInfo.Sdkdata)) |
| | | sdkInfo := msg.Tasklab.Sdkinfos[int(msg.Tasklab.Index)] |
| | | s.fnLogger("reid~~~~~~Recv From Humantrack SDK Result Length: ", len(sdkInfo.Sdkdata)) |
| | | |
| | | res := &protomsg.HumanTrackResult{} |
| | | if err := proto.Unmarshal(sdkInfo.Sdkdata, res); err != nil { |
| | | s.fnLogger("reid !!!!!! proto.Unmarshal SDK Result From Humantrack msg Error:", err) |
| | | continue |
| | | } |
| | | res := &protomsg.HumanTrackResult{} |
| | | if err := proto.Unmarshal(sdkInfo.Sdkdata, res); err != nil { |
| | | s.fnLogger("reid !!!!!! proto.Unmarshal SDK Result From Humantrack msg Error:", err) |
| | | continue |
| | | } |
| | | |
| | | for _, v := range res.Result { |
| | | for _, v := range res.Result { |
| | | |
| | | var clen C.int |
| | | l, t, r, b := C.int(v.RcHuman.Left), C.int(v.RcHuman.Top), C.int(v.RcHuman.Right), C.int(v.RcHuman.Bottom) |
| | | cutImg := C.crop_image(unsafe.Pointer(&i.Data[0]), C.int(i.Width), C.int(i.Height), l, t, r, b, 3, &clen) |
| | | if cutImg != nil { |
| | | dl := int(clen) |
| | | data := (*[1 << 26]byte)((*[1 << 26]byte)(cutImg))[:dl:dl] |
| | | var clen C.int |
| | | l, t, r, b := C.int(v.RcHuman.Left), C.int(v.RcHuman.Top), C.int(v.RcHuman.Right), C.int(v.RcHuman.Bottom) |
| | | cutImg := C.crop_image(unsafe.Pointer(&i.Data[0]), C.int(i.Width), C.int(i.Height), l, t, r, b, 3, &clen) |
| | | if cutImg != nil { |
| | | dl := int(clen) |
| | | data := (*[1 << 26]byte)((*[1 << 26]byte)(cutImg))[:dl:dl] |
| | | |
| | | w, h := int(r-l+1), int(b-t+1) |
| | | v.Feature = s.handle.Extract2(unsafe.Pointer(&data[0]), w, h, 3) |
| | | C.free(cutImg) |
| | | } |
| | | w, h := int(r-l+1), int(b-t+1) |
| | | v.Feature = s.handle.Extract2(unsafe.Pointer(&data[0]), w, h, 3) |
| | | C.free(cutImg) |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | if len(res.Result) > 0 { |
| | | if out, err := proto.Marshal(res); err == nil { |
| | | msg.Tasklab.Sdkinfos[int(msg.Tasklab.Index)].Sdkdata = out |
| | | s.fnLogger("reid~~~~~~Send To Humantrack Result Length:", len(out)) |
| | | } |
| | | } |
| | | if len(res.Result) > 0 { |
| | | if out, err := proto.Marshal(res); err == nil { |
| | | msg.Tasklab.Sdkinfos[int(msg.Tasklab.Index)].Sdkdata = out |
| | | s.fnLogger("reid~~~~~~Send To Humantrack Result Length:", len(out)) |
| | | } |
| | | } |
| | | |
| | | if data, err := proto.Marshal(&msg); err == nil { |
| | | if data == nil { |
| | | s.fnLogger("reid !!!!!! proto.Marshal Failed To Marshal proto.SdkMessage") |
| | | continue |
| | | } |
| | | s.fnLogger("reid~~~~~~MSG Send Back To Humantrack Length:", len(data)) |
| | | if data, err := proto.Marshal(&msg); err == nil { |
| | | if data == nil { |
| | | s.fnLogger("reid !!!!!! proto.Marshal Failed To Marshal proto.SdkMessage") |
| | | continue |
| | | } |
| | | s.fnLogger("reid~~~~~~MSG Send Back To Humantrack Length:", len(data)) |
| | | |
| | | chSnd <- data |
| | | } else { |
| | | s.fnLogger("reid !!!!!! proto.Marshal Out Msg Error:", err) |
| | | } |
| | | default: |
| | | chSnd <- data |
| | | } else { |
| | | s.fnLogger("reid !!!!!! proto.Marshal Out Msg Error:", err) |
| | | } |
| | | default: |
| | | |
| | | time.Sleep(10 * time.Millisecond) |
| | | time.Sleep(10 * time.Millisecond) |
| | | |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |