From f7c4a3cfd07adede3308f8d9d3d7315427d90a7c Mon Sep 17 00:00:00 2001
From: zhangmeng <zhangmeng@aiotlink.com>
Date: 星期五, 17 一月 2020 09:40:08 +0800
Subject: [PATCH] bug fixed logs print copy
---
run.go | 166 +++++++++++++++++++++++++++++++++++++++---------------
1 files changed, 119 insertions(+), 47 deletions(-)
diff --git a/run.go b/run.go
index 9cd9ec1..67a420d 100644
--- a/run.go
+++ b/run.go
@@ -1,23 +1,67 @@
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"
)
type reid struct {
handle *ReID
- list *sdkhelper.LockList
fnLogger func(...interface{})
+ gpu int
ipc string
}
@@ -46,17 +90,11 @@
gpuM := sdkhelper.Atoi(cfg.Param[sGPU])
- rGPU := 0
+ rGPU := gpu
if rGPU == -1 {
rGPU = gogpu.ValidGPU(gpuM + 512)
}
- if rGPU == -1 {
- fn("Reid SDK Create Error When Find GPU")
- return nil
- }
-
- rGPU = 0
handle := NewSDK(rGPU, cfg.Param[sModel])
if handle == nil {
@@ -66,9 +104,9 @@
return &reid{
handle: handle,
- list: sdkhelper.NewLockList(6),
fnLogger: fn,
+ gpu: rGPU,
ipc: cfg.Param[sIPC],
}
}
@@ -77,53 +115,87 @@
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
+ 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
+ }
+
+ sdkInfo := msg.Tasklab.Sdkinfos[int(msg.Tasklab.Index)]
+ s.fnLogger("reid !!!!!! recv from humantrack len: ", len(sdkInfo.Sdkdata))
+
+ 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
+ }
+ }
default:
- msg, err = c.Recv()
- if err != nil {
- s.fnLogger("REID~~~~~~Recv Image Error : ", err)
- 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
- }
- feat := s.handle.Extract2(unsafe.Pointer(&i.Data[0]), int(i.Width), int(i.Height), 3)
- if feat == nil {
+ time.Sleep(10 * time.Millisecond)
- }
- buf := float32SliceAsByteSlice(feat)
- c.Send(buf)
}
}
-}
-
-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]
}
--
Gitblit v1.8.0