zhangmeng
2019-12-17 00d07f58cd88a1e9dd13deed75ad1af3db6ebcf6
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,9 +20,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 +47,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{}),
@@ -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()
}