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"
|
|
"reid/common"
|
|
"basic.com/valib/gogpu.git"
|
|
"basic.com/pubsub/protomsg.git"
|
|
"github.com/gogo/protobuf/proto"
|
)
|
|
type reid struct {
|
handle *ReID
|
fnLogger func(...interface{})
|
|
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
|
}
|
|
sModel, sGPU, sIPC :=
|
"reid-model",
|
"gpu-memory",
|
"ipc-url"
|
|
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
|
}
|
}
|
|
gpuM := common.Atoi(cfg.Param[sGPU])
|
|
rGPU := gpu
|
|
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
|
}
|
|
return &reid{
|
handle: handle,
|
fnLogger: fn,
|
|
gpu: rGPU,
|
ipc: cfg.Param[sIPC],
|
}
|
}
|
|
// Run run
|
func Run(ctx context.Context, i interface{}) {
|
s := i.(*reid)
|
|
const (
|
postPull = `_2`
|
postPush = `_1`
|
)
|
|
ipcSnd := s.ipc + postPush
|
ipcRcv := s.ipc + postPull
|
|
sndURL := common.GetIpcAddress(true, ipcSnd)
|
rcvURL := common.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 common.UnserilizeProto(ctx, chRcv, chMsg, s.fnLogger)
|
|
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
|
}
|
|
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
|
}
|
|
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 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))
|
|
chSnd <- data
|
} else {
|
s.fnLogger("reid !!!!!! proto.Marshal Out Msg Error:", err)
|
}
|
default:
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
}
|
}
|