From b0ba865ccbf8bdbd047bb256d8e1db9af1e5711b Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期二, 17 十二月 2019 09:37:19 +0800 Subject: [PATCH] update --- work/sdk/facetrack.go | 5 + work/sdk/ydetect.go | 97 +++++++++++--------------------- work/sdk/lockList.go | 55 ++++++++++++++++++ 3 files changed, 92 insertions(+), 65 deletions(-) diff --git a/work/sdk/facetrack.go b/work/sdk/facetrack.go index 20e76ea..462db59 100644 --- a/work/sdk/facetrack.go +++ b/work/sdk/facetrack.go @@ -81,8 +81,7 @@ case <-ctx.Done(): e.fnFree(e.handle) return - default: - rMsg := <-in + case rMsg := <-in: if !validRemoteMessage(rMsg, typ) { logo.Errorln("face track validremotemessage invalid") ejectResult(nil, rMsg, out) @@ -115,6 +114,8 @@ go e.detectTrackOneChn(ctx, e.ftrackChans[rMsg.Msg.Cid], out, typ, chn) e.ftrackChans[rMsg.Msg.Cid] <- rMsg } + default: + time.Sleep(time.Millisecond * 100) } } } diff --git a/work/sdk/lockList.go b/work/sdk/lockList.go new file mode 100644 index 0000000..16b0b20 --- /dev/null +++ b/work/sdk/lockList.go @@ -0,0 +1,55 @@ +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/ydetect.go b/work/sdk/ydetect.go index 922186c..7951ca5 100644 --- a/work/sdk/ydetect.go +++ b/work/sdk/ydetect.go @@ -3,11 +3,9 @@ import ( "analysis/logo" "analysis/work" - "container/list" "context" "fmt" "plugin" - "sync" "time" "basic.com/libgowrapper/sdkstruct.git" @@ -23,9 +21,7 @@ weights string name string - cache *list.List - cv *sync.Cond - cond bool + list *LockList handle interface{} fnInit func(string, string, string, int, func(...interface{})) interface{} @@ -52,9 +48,8 @@ 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, func(...interface{})) interface{}), @@ -98,7 +93,7 @@ ejectResult(nil, rMsg, out) continue } - y.push(rMsg) + y.list.Push(rMsg) } } @@ -140,6 +135,36 @@ 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 { ret := []*protomsg.ObjInfo{} @@ -167,58 +192,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