zhangmeng
2019-12-17 b0ba865ccbf8bdbd047bb256d8e1db9af1e5711b
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()
}