| | |
| | | #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 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)) |
| | | |
| | | sdkInfo := msg.Tasklab.Sdkinfos[int(msg.Tasklab.Index)] |
| | | s.fnLogger("reid~~~~~~Recv From Humantrack SDK Result Length: ", len(sdkInfo.Sdkdata)) |
| | | // chSnd <- data |
| | | // } else { |
| | | // s.fnLogger("reid !!!!!! proto.Marshal Out Msg Error:", err) |
| | | // } |
| | | // continue |
| | | //////////////////////////////////////////////////////use reid |
| | | if len(msg.Tasklab.Sdkinfos) == 0 || int(msg.Tasklab.Index) >= len(msg.Tasklab.Sdkinfos) { |
| | | s.fnLogger("reid !!!!!! Recv Msg From Humantrack Error") |
| | | sendback(&msg, chSnd, s.fnLogger) |
| | | 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 |
| | | } |
| | | 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") |
| | | sendback(&msg, chSnd, s.fnLogger) |
| | | continue |
| | | } |
| | | |
| | | for _, v := range res.Result { |
| | | sdkInfo := msg.Tasklab.Sdkinfos[int(msg.Tasklab.Index)] |
| | | if sdkInfo.Sdkdata == nil || len(sdkInfo.Sdkdata) < 2 { |
| | | sendback(&msg, chSnd, s.fnLogger) |
| | | continue |
| | | } |
| | | s.fnLogger("reid~~~~~~Recv From Humantrack Image Len:", len(i.Data), "SDK Result Length: ", len(sdkInfo.Sdkdata)) |
| | | |
| | | 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] |
| | | res := &protomsg.HumanTrackResult{} |
| | | if err := proto.Unmarshal(sdkInfo.Sdkdata, res); err != nil { |
| | | s.fnLogger("reid !!!!!! proto.Unmarshal SDK Result From Humantrack msg Error:", err) |
| | | sendback(&msg, chSnd, s.fnLogger) |
| | | continue |
| | | } |
| | | |
| | | 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) |
| | | } |
| | | 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) |
| | | w, h := int(r-l+1), int(b-t+1) |
| | | 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 && int(clen) == w*h*3 { |
| | | data := C.GoBytes(unsafe.Pointer(cutImg), clen) |
| | | // dl := int(clen) |
| | | // data := (*[1 << 26]byte)((*[1 << 26]byte)(cutImg))[:dl:dl] |
| | | |
| | | 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)) |
| | | } |
| | | } |
| | | // 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) |
| | | } else{ |
| | | s.fnLogger("reid!!!!!!Cut Image Error Len:", clen, "size:", w, "x", h) |
| | | } |
| | | |
| | | 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: |
| | | if res.Result != nil && 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)) |
| | | } |
| | | } |
| | | |
| | | time.Sleep(10 * time.Millisecond) |
| | | sendback(&msg, chSnd, s.fnLogger) |
| | | default: |
| | | |
| | | } |
| | | time.Sleep(10 * time.Millisecond) |
| | | |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | func sendback(msg *protomsg.SdkMessage, ch chan<- []byte, fn func(...interface{})) { |
| | | if data, err := proto.Marshal(msg); err == nil { |
| | | if data == nil { |
| | | fn("reid !!!!!! proto.Marshal Failed To Marshal proto.SdkMessage") |
| | | return |
| | | } |
| | | fn("reid~~~~~~MSG Send Back To Humantrack Length:", len(data)) |
| | | |
| | | ch <- data |
| | | } else { |
| | | fn("reid !!!!!! proto.Marshal Out Msg Error:", err) |
| | | } |
| | | } |