From c8215ae1ce2825dca6a003c27fc721e86ae85e38 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期二, 17 十二月 2019 10:01:15 +0800
Subject: [PATCH] update

---
 work/sdk/humantrack.go |  104 +++++-------
 /dev/null              |   55 ------
 work/sdk/vdetect.go    |  127 ++++-----------
 work/sdk/ydetect.go    |   50 ------
 work/sdk/flow.go       |  117 ++++++++++++++
 5 files changed, 195 insertions(+), 258 deletions(-)

diff --git a/work/sdk/flow.go b/work/sdk/flow.go
new file mode 100644
index 0000000..c4e8019
--- /dev/null
+++ b/work/sdk/flow.go
@@ -0,0 +1,117 @@
+package sdk
+
+import (
+	"analysis/logo"
+	"analysis/work"
+	"container/list"
+	"context"
+	"sync"
+	"time"
+)
+
+// LockList list
+type LockList struct {
+	cache *list.List
+	cv    *sync.Cond
+	cond  bool
+	size  int
+}
+
+// NewLockList new
+func NewLockList(size int) *LockList {
+	return &LockList{
+		cache: list.New(),
+		cv:    sync.NewCond(&sync.Mutex{}),
+		cond:  false,
+		size:  size,
+	}
+}
+
+// Push push
+func (l *LockList) Push(v interface{}) {
+	l.cv.L.Lock()
+	l.cache.PushBack(v)
+
+	for l.cache.Len() > l.size {
+		l.cache.Remove(l.cache.Front())
+	}
+
+	l.cond = true
+	l.cv.Signal()
+	l.cv.L.Unlock()
+}
+
+// Pop pop
+func (l *LockList) Pop() interface{} {
+	l.cv.L.Lock()
+
+	for !l.cond {
+		l.cv.Wait()
+	}
+
+	elem := l.cache.Front().Value
+
+	l.cache.Remove(l.cache.Front())
+	l.cond = false
+	l.cv.L.Unlock()
+
+	return elem
+}
+
+/////////////////////////////////////////////////////////////////
+
+func flowSimpleWork(ctx context.Context, out chan<- work.MsgRS, typ string,
+	fnConsume func() interface{}, fnRun func(work.MsgRS, chan<- work.MsgRS, string)) {
+
+	tm := time.Now()
+	sc := 0
+
+	for {
+		select {
+		case <-ctx.Done():
+			return
+		default:
+
+			rMsg := fnConsume().(work.MsgRS)
+
+			fnRun(rMsg, out, typ)
+
+			sc++
+			if sc == 25 {
+				logo.Infoln(typ, " RUN 25 FRAME USE TIME: ", time.Since(tm))
+				sc = 0
+				tm = time.Now()
+			}
+			if time.Since(tm) > time.Second {
+				logo.Infof(typ, " RUN %d FRAME USE TIME: %v", sc, time.Since(tm))
+				sc = 0
+				tm = time.Now()
+			}
+		}
+	}
+
+}
+
+// FlowSimple wrap
+func FlowSimple(ctx context.Context, in <-chan work.MsgRS, out chan<- work.MsgRS, typ string,
+	fnProduce func(interface{}), fnConsume func() interface{},
+	fnRun func(work.MsgRS, chan<- work.MsgRS, string), fnClose func()) {
+
+	go flowSimpleWork(ctx, out, typ, fnConsume, fnRun)
+
+	for {
+		select {
+		case <-ctx.Done():
+			fnClose()
+			return
+		default:
+			rMsg := <-in
+			if !validRemoteMessage(rMsg, typ) {
+				logo.Errorln(typ, " validremotemessage invalid")
+				ejectResult(nil, rMsg, out)
+				continue
+			}
+			fnProduce(rMsg)
+		}
+	}
+}
diff --git a/work/sdk/humantrack.go b/work/sdk/humantrack.go
index 8c5c3fd..4ec45dd 100644
--- a/work/sdk/humantrack.go
+++ b/work/sdk/humantrack.go
@@ -5,7 +5,6 @@
 	"analysis/work"
 	"context"
 	"plugin"
-	"time"
 
 	"github.com/gogo/protobuf/proto"
 
@@ -19,6 +18,8 @@
 	gpu       int
 	batchSize int
 	flag      int
+
+	list *LockList
 
 	handle       interface{}
 	fnInit       func(int, int, int, func(...interface{})) interface{}
@@ -83,69 +84,50 @@
 	return true
 }
 
+func (t *HumanTracker) track(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)
+
+	// var images []sdkstruct.SDKImage
+	// img := sdkstruct.SDKImage{
+	// 	Data:    i.Data,
+	// 	Width:   imgW,
+	// 	Height:  imgH,
+	// 	Channel: 3,
+	// }
+	// images = append(images, img)
+	res := t.fnRun(t.handle, i.Data, imgW, imgH, 3)
+	if res != nil {
+		ejectResult(nil, rMsg, out)
+		return
+	}
+
+	hr := convert2ProtoHumanTrackResult(res)
+	result := protomsg.HumanTrackResult{Result: hr[0]}
+	data, err := proto.Marshal(&result)
+	if err != nil {
+		logo.Errorln("HUMAN TRACKER MARSHAL PROTO PLATE IDS ERROR", err)
+		data = nil
+	}
+	ejectResult(data, rMsg, out)
+	var id, name string
+	if rMsg.Msg.Tasklab != nil {
+		id, name = rMsg.Msg.Tasklab.Taskid, rMsg.Msg.Tasklab.Taskname
+	}
+
+	logo.Infoln("CAMERAID: ", rMsg.Msg.Cid, " TASKID: ", id, " TASKNAME: ", name, " DETECT YOLO COUNT: ", len(hr[0]))
+
+}
+
 // Run impl
 func (t *HumanTracker) Run(ctx context.Context, in <-chan work.MsgRS, out chan<- work.MsgRS, typ string) {
-	tm := time.Now()
-	sc := 0
 
-	for {
-		select {
-		case <-ctx.Done():
-			return
-		default:
-			rMsg := <-in
-			if !validRemoteMessage(rMsg, typ) {
-				ejectResult(nil, rMsg, out)
-				continue
-			}
-
-			i := unpackImage(rMsg, typ)
-			if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 {
-				ejectResult(nil, rMsg, out)
-				continue
-			}
-
-			imgW, imgH := int(i.Width), int(i.Height)
-
-			// var images []sdkstruct.SDKImage
-			// img := sdkstruct.SDKImage{
-			// 	Data:    i.Data,
-			// 	Width:   imgW,
-			// 	Height:  imgH,
-			// 	Channel: 3,
-			// }
-			// images = append(images, img)
-			res := t.fnRun(t.handle, i.Data, imgW, imgH, 3)
-			if res != nil {
-				ejectResult(nil, rMsg, out)
-				continue
-			}
-
-			hr := convert2ProtoHumanTrackResult(res)
-			result := protomsg.HumanTrackResult{Result: hr[0]}
-			data, err := proto.Marshal(&result)
-			if err != nil {
-				logo.Errorln("HUMAN TRACKER MARSHAL PROTO PLATE IDS ERROR", err)
-				data = nil
-			}
-			ejectResult(data, rMsg, out)
-
-			/////////////////////////////////////
-			sc++
-			if sc == 25 {
-				logo.Infoln("HUMAN TRACKER RUN 25 FRAME USE TIME: ", time.Since(tm))
-				sc = 0
-				tm = time.Now()
-			}
-
-			if time.Since(tm) > time.Second {
-				logo.Infof("HUMAN TRACKER RUN %d FRAME USE TIME: %v", sc, time.Since(tm))
-				sc = 0
-				tm = time.Now()
-			}
-
-		}
-	}
+	FlowSimple(ctx, in, out, typ, t.list.Push, t.list.Pop, t.track, func() { t.fnFree(t.handle) })
 }
 
 // message HumanTrack {
diff --git a/work/sdk/lockList.go b/work/sdk/lockList.go
deleted file mode 100644
index 16b0b20..0000000
--- a/work/sdk/lockList.go
+++ /dev/null
@@ -1,55 +0,0 @@
-package sdk
-
-import (
-	"container/list"
-	"sync"
-)
-
-// LockList list
-type LockList struct {
-	cache *list.List
-	cv    *sync.Cond
-	cond  bool
-	size  int
-}
-
-// NewLockList new
-func NewLockList(size int) *LockList {
-	return &LockList{
-		cache: list.New(),
-		cv:    sync.NewCond(&sync.Mutex{}),
-		cond:  false,
-		size:  size,
-	}
-}
-
-// Push push
-func (l *LockList) Push(v interface{}) {
-	l.cv.L.Lock()
-	l.cache.PushBack(v)
-
-	for l.cache.Len() > l.size {
-		l.cache.Remove(l.cache.Front())
-	}
-
-	l.cond = true
-	l.cv.Signal()
-	l.cv.L.Unlock()
-}
-
-// Pop pop
-func (l *LockList) Pop() interface{} {
-	l.cv.L.Lock()
-
-	for !l.cond {
-		l.cv.Wait()
-	}
-
-	elem := l.cache.Front().Value
-
-	l.cache.Remove(l.cache.Front())
-	l.cond = false
-	l.cv.L.Unlock()
-
-	return elem
-}
diff --git a/work/sdk/vdetect.go b/work/sdk/vdetect.go
index 2c63000..2ee2903 100644
--- a/work/sdk/vdetect.go
+++ b/work/sdk/vdetect.go
@@ -6,7 +6,6 @@
 	"context"
 	"fmt"
 	"plugin"
-	"time"
 
 	"basic.com/libgowrapper/sdkstruct.git"
 	"basic.com/pubsub/protomsg.git"
@@ -17,6 +16,8 @@
 type VehicleDetector struct {
 	licSrvPath string
 	modelPath  string
+
+	list *LockList
 
 	handle interface{}
 	fnInit func(int, int, string, string, func(...interface{})) interface{}
@@ -42,6 +43,8 @@
 		licSrvPath: licSrv,
 		modelPath:  model,
 
+		list: NewLockList(6),
+
 		handle: nil,
 		fnInit: fnInit.(func(int, int, string, string, func(...interface{})) interface{}),
 		fnFree: fnFree.(func(interface{})),
@@ -63,64 +66,38 @@
 	return true
 }
 
+func (d *VehicleDetector) detect(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)
+
+	vehicle := d.fnRun(d.handle, i.Data, imgW, imgH, 3, 0, 0, imgW, imgH)
+	plateids := convert2ProtoPlateIDResultVehicle(vehicle)
+
+	plateresult := protomsg.PlateIDResult{Result: plateids}
+	data, err := proto.Marshal(&plateresult)
+	if err != nil {
+		fmt.Println("PLATE ID DETECTOR MARSHAL PROTO PLATE IDS ERROR", err)
+		data = nil
+	}
+
+	ejectResult(data, rMsg, out)
+
+	var id, name string
+	if rMsg.Msg.Tasklab != nil {
+		id, name = rMsg.Msg.Tasklab.Taskid, rMsg.Msg.Tasklab.Taskname
+	}
+	logo.Infoln("CAMERAID: ", rMsg.Msg.Cid, " TASKID: ", id, " TASKNAME: ", name, " DETECT VEHICLE PLATE ID COUNT: ", len(vehicle))
+
+}
+
 // Run impl
 func (d *VehicleDetector) Run(ctx context.Context, in <-chan work.MsgRS, out chan<- work.MsgRS, typ string) {
-	tm := time.Now()
-	sc := 0
-
-	for {
-		select {
-		case <-ctx.Done():
-			d.fnFree(d.handle)
-			return
-		default:
-			rMsg := <-in
-			if !validRemoteMessage(rMsg, typ) {
-				ejectResult(nil, rMsg, out)
-				continue
-			}
-
-			i := unpackImage(rMsg, typ)
-			if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 {
-				ejectResult(nil, rMsg, out)
-				continue
-			}
-
-			imgW, imgH := int(i.Width), int(i.Height)
-
-			vehicle := d.fnRun(d.handle, i.Data, imgW, imgH, 3, 0, 0, imgW, imgH)
-			plateids := convert2ProtoPlateIDResultVehicle(vehicle)
-
-			plateresult := protomsg.PlateIDResult{Result: plateids}
-			data, err := proto.Marshal(&plateresult)
-			if err != nil {
-				fmt.Println("PLATE ID DETECTOR MARSHAL PROTO PLATE IDS ERROR", err)
-				data = nil
-			}
-
-			ejectResult(data, rMsg, out)
-
-			var id, name string
-			if rMsg.Msg.Tasklab != nil {
-				id, name = rMsg.Msg.Tasklab.Taskid, rMsg.Msg.Tasklab.Taskname
-			}
-			logo.Infoln("CAMERAID: ", rMsg.Msg.Cid, " TASKID: ", id, " TASKNAME: ", name, " DETECT VEHICLE PLATE ID COUNT: ", len(vehicle))
-
-			sc++
-			if sc == 25 {
-				logo.Infoln("PLATE ID DETECTOR RUN 25 FRAME USE TIME: ", time.Since(tm))
-				sc = 0
-				tm = time.Now()
-			}
-
-			if time.Since(tm) > time.Second {
-				logo.Infof("PLATE ID DETECTOR RUN %d FRAME USE TIME: %v", sc, time.Since(tm))
-				sc = 0
-				tm = time.Now()
-			}
-
-		}
-	}
+	FlowSimple(ctx, in, out, typ, d.list.Push, d.list.Pop, d.detect, func() { d.fnFree(d.handle) })
 }
 
 func convert2ProtoPlateIDResultVehicle(obj []sdkstruct.CVehicleITSResult) []*protomsg.PlateIDVehicle {
@@ -179,39 +156,3 @@
 	}
 	return ret
 }
-
-// hr := convert2ProtoHumanTrackResult(res)
-// result := protomsg.HumanTrackResult{Result: hr[0]}
-// data, err := proto.Marshal(&result)
-// if err != nil {
-// 	fmt.Println("HUMAN TRACKER MARSHAL PROTO PLATE IDS ERROR", err)
-// 	data = nil
-// }
-
-// func convert2ProtoHumanTrackResult(obj []sdkstruct.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)
-// 		}
-// 		ret = append(ret, res)
-// 	}
-// 	return ret
-// }
diff --git a/work/sdk/ydetect.go b/work/sdk/ydetect.go
index 7951ca5..84364f9 100644
--- a/work/sdk/ydetect.go
+++ b/work/sdk/ydetect.go
@@ -6,7 +6,6 @@
 	"context"
 	"fmt"
 	"plugin"
-	"time"
 
 	"basic.com/libgowrapper/sdkstruct.git"
 	"basic.com/pubsub/protomsg.git"
@@ -79,24 +78,7 @@
 
 // Run impl interface
 func (y *YoloDetect) Run(ctx context.Context, in <-chan work.MsgRS, out chan<- work.MsgRS, typ string) {
-	go y.work(ctx, out, typ)
-
-	for {
-		select {
-		case <-ctx.Done():
-			y.fnFree(y.handle)
-			return
-		default:
-			rMsg := <-in
-			if !validRemoteMessage(rMsg, typ) {
-				logo.Errorln("yolo validremotemessage invalid")
-				ejectResult(nil, rMsg, out)
-				continue
-			}
-			y.list.Push(rMsg)
-		}
-	}
-
+	FlowSimple(ctx, in, out, typ, y.list.Push, y.list.Pop, y.track, func() { y.fnFree(y.handle) })
 }
 
 func (y *YoloDetect) track(rMsg work.MsgRS, out chan<- work.MsgRS, typ string) {
@@ -133,36 +115,6 @@
 	}
 
 	logo.Infoln("CAMERAID: ", rMsg.Msg.Cid, " TASKID: ", id, " TASKNAME: ", name, " DETECT YOLO COUNT: ", len(whole))
-}
-
-func (y *YoloDetect) work(ctx context.Context, out chan<- work.MsgRS, typ string) {
-	tm := time.Now()
-	sc := 0
-
-	for {
-		select {
-		case <-ctx.Done():
-			return
-		default:
-
-			rMsg := y.list.Pop().(work.MsgRS)
-
-			y.track(rMsg, out, typ)
-
-			sc++
-			if sc == 25 {
-				logo.Infoln("YOLO RUN 25 FRAME USE TIME: ", time.Since(tm))
-				sc = 0
-				tm = time.Now()
-			}
-			if time.Since(tm) > time.Second {
-				logo.Infof("YOLO RUN %d FRAME USE TIME: %v", sc, time.Since(tm))
-				sc = 0
-				tm = time.Now()
-			}
-		}
-	}
-
 }
 
 func convert2ProtoYoloTrack(obj []sdkstruct.CObjTrackInfo, fx, fy float64) []*protomsg.ObjInfo {

--
Gitblit v1.8.0