From 4a745a94f3eee82624e1b211b48920c0b7fcd6d2 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期二, 17 十二月 2019 10:30:03 +0800
Subject: [PATCH] udpate

---
 work/sdk/plateIDdetect.go |   91 +++++++----------
 work/sdk/ydetect.go       |   11 -
 work/sdk/yolotrack.go     |   72 --------------
 work/sdk/flow.go          |  117 +++++++++++++++++++++++
 4 files changed, 159 insertions(+), 132 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/plateIDdetect.go b/work/sdk/plateIDdetect.go
index 0fce158..520d66a 100644
--- a/work/sdk/plateIDdetect.go
+++ b/work/sdk/plateIDdetect.go
@@ -4,7 +4,6 @@
 	"analysis/logo"
 	"analysis/work"
 	"context"
-	"time"
 
 	"analysis/gosdk"
 )
@@ -15,6 +14,7 @@
 	cfgCloud   *gosdk.CPlateIDCloudCfg
 	licSrvPath string
 	modelPath  string
+	list       *LockList
 	fn         func(*gosdk.SDKImage, *gosdk.CRECT, int, int, *work.MsgRS, chan<- work.MsgRS)
 }
 
@@ -31,6 +31,7 @@
 		return &PlateIDDetector{
 			licSrvPath: licSrv,
 			modelPath:  model,
+			list:       NewLockList(6),
 			fn:         vehicle,
 		}
 	}
@@ -46,6 +47,7 @@
 			cfgCloud:   nil,
 			licSrvPath: licSrv,
 			modelPath:  "",
+			list:       NewLockList(6),
 			fn:         eparking,
 		}
 
@@ -60,10 +62,21 @@
 			cfgCloud:   cfg,
 			licSrvPath: licSrv,
 			modelPath:  model,
+			list:       NewLockList(6),
 			fn:         cloud,
 		}
 	}
 	return nil
+}
+
+func (d *PlateIDDetector) Free() {
+	if d.config != nil {
+		gosdk.FreePlateIdDetector()
+	} else if d.cfgCloud != nil {
+		gosdk.FreePlateIDCloudSDKDetector()
+	} else {
+		gosdk.FreeVehicleITSDetector()
+	}
 }
 
 // Init impl
@@ -89,59 +102,33 @@
 	return true
 }
 
+func (d *PlateIDDetector) 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)
+	img := gosdk.SDKImage{Data: i.Data, Width: imgW, Height: imgH}
+
+	rcDetect := gosdk.CRECT{
+		Left:   0,
+		Top:    0,
+		Right:  (int32)(imgW),
+		Bottom: (int32)(imgH),
+	}
+
+	mW, mH := 4096, 2160
+	if d.config != nil {
+		mW, mH = int(d.config.MaxImageWidth), int(d.config.MaxImageHeight)
+	}
+	d.fn(&img, &rcDetect, mW, mH, &rMsg, out)
+}
+
 // Run impl
 func (d *PlateIDDetector) 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)
-			img := gosdk.SDKImage{Data: i.Data, Width: imgW, Height: imgH}
-
-			rcDetect := gosdk.CRECT{
-				Left:   0,
-				Top:    0,
-				Right:  (int32)(imgW),
-				Bottom: (int32)(imgH),
-			}
-
-			mW, mH := 4096, 2160
-			if d.config != nil {
-				mW, mH = int(d.config.MaxImageWidth), int(d.config.MaxImageHeight)
-			}
-			d.fn(&img, &rcDetect, mW, mH, &rMsg, out)
-
-			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.Free() })
 }
 
 func eparking(img *gosdk.SDKImage, rc *gosdk.CRECT, mW, mH int, rMsg *work.MsgRS, out chan<- work.MsgRS) {
diff --git a/work/sdk/ydetect.go b/work/sdk/ydetect.go
index 144ae0f..bc56bcc 100644
--- a/work/sdk/ydetect.go
+++ b/work/sdk/ydetect.go
@@ -3,9 +3,7 @@
 import (
 	"analysis/logo"
 	"analysis/work"
-	"container/list"
 	"context"
-	"sync"
 
 	"basic.com/valib/gogpu.git"
 
@@ -27,9 +25,7 @@
 
 	tracker map[string]*trackInfo
 
-	cache *list.List
-	cv    *sync.Cond
-	cond  bool
+	list *LockList
 }
 
 // NewYDetectWithTrack with track
@@ -39,9 +35,7 @@
 		cfg:     cfg,
 		weights: weights,
 		name:    name,
-		cache:   list.New(),
-		cv:      sync.NewCond(&sync.Mutex{}),
-		cond:    false,
+		list:    NewLockList(6),
 	}
 }
 
@@ -67,5 +61,4 @@
 // Run impl interface
 func (y *YoloDetect) Run(ctx context.Context, in <-chan work.MsgRS, out chan<- work.MsgRS, typ string) {
 	y.detectTrack(ctx, in, out, typ)
-
 }
diff --git a/work/sdk/yolotrack.go b/work/sdk/yolotrack.go
index 29058d2..34a468d 100644
--- a/work/sdk/yolotrack.go
+++ b/work/sdk/yolotrack.go
@@ -4,7 +4,6 @@
 	"analysis/logo"
 	"analysis/work"
 	"context"
-	"time"
 
 	"analysis/gosdk"
 
@@ -14,76 +13,7 @@
 
 func (y *YoloDetect) detectTrack(ctx context.Context, in <-chan work.MsgRS, out chan<- work.MsgRS, typ string) {
 	y.tracker = make(map[string]*trackInfo)
-	go y.work(ctx, out, typ)
-
-	for {
-		select {
-		case <-ctx.Done():
-			return
-		default:
-			rMsg := <-in
-			if !validRemoteMessage(rMsg, typ) {
-				logo.Errorln("yolo validremotemessage invalid")
-				ejectResult(nil, rMsg, out)
-				continue
-			}
-			y.push(rMsg)
-		}
-	}
-}
-
-func (y *YoloDetect) push(data work.MsgRS) {
-	y.cv.L.Lock()
-	y.cache.PushBack(data)
-	if y.cache.Len() > 12 {
-		for i := 0; i < y.cache.Len(); {
-			y.cache.Remove(y.cache.Front())
-			i = i + 2
-		}
-	}
-	// logo.Infof("push to cache count : %d\n", t.cache.Len())
-	y.cond = true
-	y.cv.Signal()
-	y.cv.L.Unlock()
-}
-
-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:
-
-			y.cv.L.Lock()
-
-			for !y.cond {
-				y.cv.Wait()
-			}
-
-			rMsg := y.cache.Front().Value.(work.MsgRS)
-
-			y.track(rMsg, out, typ)
-			y.cache.Remove(y.cache.Front())
-			y.cond = false
-			y.cv.L.Unlock()
-
-			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()
-			}
-		}
-	}
-
+	FlowSimple(ctx, in, out, typ, y.list.Push, y.list.Pop, y.track, func() {})
 }
 
 func (y *YoloDetect) track(rMsg work.MsgRS, out chan<- work.MsgRS, typ string) {

--
Gitblit v1.8.0