| | |
| | | package main |
| | | |
| | | /* |
| | | #include <stdlib.h> |
| | | #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 (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 > 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; |
| | | |
| | | *length = channel * destW * destH; |
| | | unsigned char * desData = (unsigned char*)malloc(*length); |
| | | |
| | | int i = 0; |
| | | int destIdy = 0; |
| | | |
| | | for (i = y0; i <= y1; i++) |
| | | { |
| | | destIdy = i - y0; |
| | | memcpy(&(desData[destIdy * destW * channel]), &(src[(i * srcW + x0) * channel]),sizeof(char) * channel * destW); |
| | | } |
| | | |
| | | return desData; |
| | | }*/ |
| | | import "C" |
| | | import ( |
| | | "context" |
| | | "time" |
| | | "unsafe" |
| | | |
| | | "reid/rpc" |
| | | |
| | | "basic.com/libgowrapper/sdkhelper.git" |
| | | |
| | | "basic.com/valib/gogpu.git" |
| | | |
| | | "basic.com/pubsub/protomsg.git" |
| | | |
| | | "basic.com/valib/deliver.git" |
| | | "github.com/gogo/protobuf/proto" |
| | | ) |
| | | |
| | |
| | | func Run(ctx context.Context, i interface{}) { |
| | | s := i.(*reid) |
| | | |
| | | c := deliver.NewClient(deliver.ReqRep, s.ipc) |
| | | const ( |
| | | postPull = `_2` |
| | | postPush = `_1` |
| | | ) |
| | | |
| | | var msg []byte |
| | | var err error |
| | | ipcSnd := s.ipc + postPush |
| | | ipcRcv := s.ipc + postPull |
| | | |
| | | sndURL := sdkhelper.GetIpcAddress(true, ipcSnd) |
| | | rcvURL := sdkhelper.GetIpcAddress(true, ipcRcv) |
| | | |
| | | chSnd := make(chan []byte, 3) |
| | | chRcv := make(chan []byte, 3) |
| | | |
| | | recv := rpc.NewReciever(rcvURL, chRcv, true, s.fnLogger) |
| | | go recv.Run(ctx) |
| | | |
| | | chMsg := make(chan protomsg.SdkMessage, 3) |
| | | go sdkhelper.UnserilizeProto(ctx, chRcv, chMsg, s.fnLogger) |
| | | |
| | | send := rpc.NewSender(sndURL, chSnd, true, s.fnLogger) |
| | | go send.Run(ctx) |
| | | |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | return |
| | | default: |
| | | |
| | | msg, err = c.Recv() |
| | | if err != nil { |
| | | case msg := <-chMsg: |
| | | if len(msg.Tasklab.Sdkinfos) == 0 || int(msg.Tasklab.Index) >= len(msg.Tasklab.Sdkinfos) { |
| | | s.fnLogger("reid !!!!!! recv msg error") |
| | | continue |
| | | } |
| | | i := sdkhelper.UnpackImage(msg, "reid", s.fnLogger) |
| | | if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 { |
| | | s.fnLogger("reid !!!!!! unpack image error") |
| | | continue |
| | | } |
| | | |
| | | i := &protomsg.Image{} |
| | | err := proto.Unmarshal(msg, i) |
| | | if err != nil { |
| | | s.fnLogger("REID~~~~~~protobuf decode CameraImage error: ", err) |
| | | continue |
| | | } |
| | | if i.Data == nil { |
| | | s.fnLogger("REID~~~~~~protomsg.Image data null") |
| | | continue |
| | | } |
| | | s.fnLogger("REID~~~~~~Recv Image:", len(i.Data)) |
| | | sdkInfo := msg.Tasklab.Sdkinfos[int(msg.Tasklab.Index)] |
| | | s.fnLogger("reid !!!!!! recv from humantrack len: ", len(sdkInfo.Sdkdata)) |
| | | |
| | | feat := s.handle.Extract2(unsafe.Pointer(&i.Data[0]), int(i.Width), int(i.Height), 3) |
| | | if feat == nil { |
| | | // feat = make([]float32, 1) |
| | | } else { |
| | | s.fnLogger("REID~~~~~~Run Reid Use GPU: ", s.gpu) |
| | | for k := 0; k < 3; k++ { |
| | | s.fnLogger("REID~~~~~~extractor---human_feats------%f", feat[k+2000]) |
| | | res := &protomsg.HumanTrackResult{} |
| | | if err := proto.Unmarshal(sdkInfo.Sdkdata, res); err != nil { |
| | | s.fnLogger(err, " sdkinfo msg Unmarshal 处理异常") |
| | | continue |
| | | } |
| | | 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] |
| | | |
| | | 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 out, err := proto.Marshal(res); err == nil { |
| | | msg.Tasklab.Sdkinfos[int(msg.Tasklab.Index)].Sdkdata = out |
| | | nMsg := protomsg.SdkMessage{} |
| | | |
| | | if data, err := proto.Marshal(&nMsg); err == nil { |
| | | if data == nil { |
| | | s.fnLogger(err, " msg Marshal 处理异常") |
| | | continue |
| | | } |
| | | s.fnLogger("reid !!!!!! send to humantrack len: ", len(data)) |
| | | |
| | | chSnd <- data |
| | | } |
| | | } |
| | | buf := float32SliceAsByteSlice(feat) |
| | | c.Send(buf) |
| | | default: |
| | | |
| | | time.Sleep(10 * time.Millisecond) |
| | | |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | func float32SliceAsByteSlice(src []float32) []byte { |
| | | if len(src) == 0 { |
| | | return nil |
| | | } |
| | | |
| | | l := len(src) * 4 |
| | | ptr := unsafe.Pointer(&src[0]) |
| | | // It is important to keep in mind that the Go garbage collector |
| | | // will not interact with this data, and that if src if freed, |
| | | // the behavior of any Go code using the slice is nondeterministic. |
| | | // Reference: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices |
| | | return (*[1 << 26]byte)((*[1 << 26]byte)(ptr))[:l:l] |
| | | } |