From 88e51b749b1306dc9573603af116984d7249ced9 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期四, 19 十二月 2019 13:43:41 +0800
Subject: [PATCH] udpate

---
 work/sdk/humantrack.go       |  134 ++++++++++++++++++++++++++++++++++++--------
 gohumantrack/gohumantrack.go |   39 ++++++++++++
 2 files changed, 146 insertions(+), 27 deletions(-)

diff --git a/gohumantrack/gohumantrack.go b/gohumantrack/gohumantrack.go
index ffed316..b2a7965 100644
--- a/gohumantrack/gohumantrack.go
+++ b/gohumantrack/gohumantrack.go
@@ -98,7 +98,7 @@
 // HumanTracker struct
 type HumanTracker struct {
 	handle    unsafe.Pointer
-	result 	  unsafe.Pointer
+	result    unsafe.Pointer
 	batchSize int
 }
 
@@ -169,6 +169,43 @@
 	return result, nil
 }
 
+// ProcessImagePointer process
+func (h *HumanTracker) ProcessImagePointer(imgs []*ImageHumanTracker) ([]FgResult, error) {
+	if len(imgs) != h.batchSize {
+		return nil, errors.New("input images count doesn't equalize to batchsize")
+	}
+	cImgs := C.create_batch_image(C.int(h.batchSize))
+	if cImgs == nil {
+		return nil, errors.New("create C images error")
+	}
+	defer C.free(cImgs)
+	for k, v := range imgs {
+		ret := 0
+		if v == nil {
+			ret = C.fill_images(cImgs, C.int(h.batchSize), C.int(k), nil, 0, 0, 0)
+		} else {
+			ret = C.fill_images(cImgs, C.int(h.batchSize), C.int(k), unsafe.Pointer(&v.Data[0]), C.int(v.Width), C.int(v.Height), C.int(v.Channel))
+		}
+		if int(ret) != k {
+			return nil, errors.New("fill C images error")
+		}
+	}
+
+	cRet := C.process(h.handle, cImgs, C.int(h.batchSize), h.result)
+	if cRet == nil {
+		return nil, errors.New("create C results error")
+	}
+
+	var result []FgResult
+	p := uintptr(cRet)
+	for i := 0; i < h.batchSize; i++ {
+		j := *(*FgResult)(unsafe.Pointer(p))
+		result = append(result, j)
+		p += unsafe.Sizeof(j)
+	}
+	return result, nil
+}
+
 // FFSimilarity similarity
 func FFSimilarity(feaA, feaB [128]float32) float64 {
 	var norm1, norm2 float64
diff --git a/work/sdk/humantrack.go b/work/sdk/humantrack.go
index 99e9de0..ba9fb12 100644
--- a/work/sdk/humantrack.go
+++ b/work/sdk/humantrack.go
@@ -20,6 +20,12 @@
 	batchSize int
 	flag      int
 	list      *LockList
+
+	mapCameraImageIndex map[string]int
+	recvImageCount      int
+	index               int
+	images              [batchSize]*gohumantrack.ImageHumanTracker
+	msgs                [batchSize]*work.MsgRS
 }
 
 // NewHumanTracker new
@@ -29,6 +35,9 @@
 		batchSize: batchSize,
 		flag:      flag,
 		list:      NewLockList(6),
+
+		recvImageCount: 0,
+		index:          0,
 	}
 }
 
@@ -84,7 +93,7 @@
 	}
 
 	hr := convert2ProtoHumanTrackResult(res)
-	result := protomsg.HumanTrackResult{Result: hr[0]}
+	result := protomsg.HumanTrackResult{Result: hr}
 	data, err := proto.Marshal(&result)
 	if err != nil {
 		logo.Errorln("HUMAN TRACKER MARSHAL PROTO PLATE IDS ERROR", err)
@@ -99,9 +108,87 @@
 
 }
 
+func (t *HumanTracker) trackBatch(rMsg work.MsgRS, out chan<- work.MsgRS, typ string) {
+	i := unpackImage(rMsg, typ)
+	if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 {
+		ejectResult(nil, rMsg, out)
+		return
+	}
+
+	imgW, imgH := int(i.Width), int(i.Height)
+
+	img := gohumantrack.ImageHumanTracker{
+		Data:    i.Data,
+		Width:   imgW,
+		Height:  imgH,
+		Channel: 3,
+	}
+
+	// mapCameraImageIndex map[string]int
+	// images              []gohumantrack.ImageHumanTracker
+
+	t.recvImageCount++
+	if t.mapCameraImageIndex == nil {
+		t.mapCameraImageIndex = make(map[string]int)
+		for i := 0; i < t.batchSize; i++ {
+			t.images[i] = nil
+		}
+		for i := 0; i < t.batchSize; i++ {
+			t.msgs[i] = nil
+		}
+	}
+	if i, ok := t.mapCameraImageIndex[rMsg.Msg.Cid]; ok {
+		if i < batchSize {
+			t.images[i] = &img
+			t.msgs[i] = &rMsg
+		}
+	} else {
+		if t.index < batchSize {
+			t.images[t.index] = &img
+			t.msgs[t.index] = &rMsg
+			t.mapCameraImageIndex[rMsg.Msg.Cid] = t.index
+		}
+		t.index++
+	}
+	if t.recvImageCount < t.batchSize+t.batchSize/2 {
+		return
+	}
+
+	res, err := t.tracker.ProcessImagePointer(t.images[:])
+
+	if err != nil {
+		t.mapCameraImageIndex = nil
+		ejectResult(nil, rMsg, out)
+		return
+	}
+
+	for i := 0; i < t.batchSize; i++ {
+		if t.images[i] == nil {
+			continue
+		}
+		hr := convert2ProtoHumanTrackResult(res[i])
+		result := protomsg.HumanTrackResult{Result: hr}
+		data, err := proto.Marshal(&result)
+		if err != nil {
+			logo.Errorln("HUMAN TRACKER MARSHAL PROTO PLATE IDS ERROR", err)
+			data = nil
+		}
+		msg := *t.msgs[i]
+		ejectResult(data, msg, out)
+		var id, name string
+		if msg.Msg.Tasklab != nil {
+			id, name = msg.Msg.Tasklab.Taskid, msg.Msg.Tasklab.Taskname
+		}
+		logo.Infoln("CAMERAID: ", msg.Msg.Cid, " TASKID: ", id, " TASKNAME: ", name, " Human Track COUNT: ", len(hr[0]))
+
+	}
+
+}
+
 // Run impl
 func (t *HumanTracker) Run(ctx context.Context, in <-chan work.MsgRS, out chan<- work.MsgRS, typ string) {
-	FlowSimple(ctx, in, out, typ, t.list.Push, t.list.Pop, t.track, t.Free)
+	// FlowSimple(ctx, in, out, typ, t.list.Push, t.list.Pop, t.track, t.Free)
+	FlowSimple(ctx, in, out, typ, t.list.Push, t.list.Pop, t.trackBatch, t.Free)
 }
 
 // message HumanTrack {
@@ -117,30 +204,25 @@
 //     repeated HumanTrack result = 1;
 // }
 
-func convert2ProtoHumanTrackResult(obj []gohumantrack.FgResult) [][]*protomsg.HumanTrack {
-	ret := [][]*protomsg.HumanTrack{}
-	for _, v := range obj {
-		res := []*protomsg.HumanTrack{}
-		for i := 0; i < int(v.FgNum); i++ {
-			r := v.Fginfo[i]
-
-			rect := protomsg.Rect{
-				Left:   r.Left,
-				Right:  r.Right,
-				Top:    r.Top,
-				Bottom: r.Bottom,
-			}
-			pr := &protomsg.HumanTrack{
-				RcHuman:    &rect,
-				Confidence: r.Confidence,
-				X:          r.X,
-				Y:          r.Y,
-				Id:         r.ID,
-				Feature:    r.Feature[:],
-			}
-			res = append(res, pr)
+func convert2ProtoHumanTrackResult(obj gohumantrack.FgResult) []*protomsg.HumanTrack {
+	res := []*protomsg.HumanTrack{}
+	for i := 0; i < int(v.FgNum); i++ {
+		r := v.Fginfo[i]
+		rect := protomsg.Rect{
+			Left:   r.Left,
+			Right:  r.Right,
+			Top:    r.Top,
+			Bottom: r.Bottom,
 		}
-		ret = append(ret, res)
+		pr := &protomsg.HumanTrack{
+			RcHuman:    &rect,
+			Confidence: r.Confidence,
+			X:          r.X,
+			Y:          r.Y,
+			Id:         r.ID,
+			Feature:    r.Feature[:],
+		}
+		res = append(res, pr)
 	}
-	return ret
+	return res
 }

--
Gitblit v1.8.0