From 00d07f58cd88a1e9dd13deed75ad1af3db6ebcf6 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期二, 17 十二月 2019 16:07:01 +0800
Subject: [PATCH] udpate

---
 work/sdk/ydetect.go |   91 +++------------------------------------------
 1 files changed, 7 insertions(+), 84 deletions(-)

diff --git a/work/sdk/ydetect.go b/work/sdk/ydetect.go
index 737b41a..84364f9 100644
--- a/work/sdk/ydetect.go
+++ b/work/sdk/ydetect.go
@@ -3,12 +3,9 @@
 import (
 	"analysis/logo"
 	"analysis/work"
-	"container/list"
 	"context"
 	"fmt"
 	"plugin"
-	"sync"
-	"time"
 
 	"basic.com/libgowrapper/sdkstruct.git"
 	"basic.com/pubsub/protomsg.git"
@@ -23,12 +20,10 @@
 	weights string
 	name    string
 
-	cache *list.List
-	cv    *sync.Cond
-	cond  bool
+	list *LockList
 
 	handle interface{}
-	fnInit func(string, string, string, int) interface{}
+	fnInit func(string, string, string, int, func(...interface{})) interface{}
 	fnFree func(interface{})
 	fnRun  func(interface{}, string, []byte, int, int, int, float32, int) ([]sdkstruct.CObjTrackInfo, []sdkstruct.CObjTrackInfo)
 }
@@ -52,12 +47,11 @@
 		cfg:     cfg,
 		weights: weights,
 		name:    name,
-		cache:   list.New(),
-		cv:      sync.NewCond(&sync.Mutex{}),
-		cond:    false,
+
+		list: NewLockList(6),
 
 		handle: nil,
-		fnInit: fnInit.(func(string, string, string, int) interface{}),
+		fnInit: fnInit.(func(string, string, string, int, func(...interface{})) interface{}),
 		fnFree: fnFree.(func(interface{})),
 		fnRun:  fnRun.(func(interface{}, string, []byte, int, int, int, float32, int) ([]sdkstruct.CObjTrackInfo, []sdkstruct.CObjTrackInfo)),
 	}
@@ -70,7 +64,7 @@
 	if gpu == -1 {
 		gpu = gogpu.ValidGPU(2048)
 	}
-	h := y.fnInit(y.cfg, y.weights, y.name, gpu)
+	h := y.fnInit(y.cfg, y.weights, y.name, gpu, logo.Infoln)
 	logo.Infoln("YOLO USE GPU: ", gpu)
 
 	if h == nil {
@@ -84,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.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) {
@@ -167,58 +144,4 @@
 		ret = append(ret, &obj)
 	}
 	return ret
-}
-
-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()
-			}
-		}
-	}
-
-}
-
-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()
 }

--
Gitblit v1.8.0