派生自 libgowrapper/face

zhangmeng
2020-01-15 0ffd95f2278e860736e46f8b73f357470f5a3d91
run.go
@@ -2,13 +2,19 @@
import (
   "context"
   "os"
   "sync"
   "time"
   "unsafe"
   "face/common"
   "basic.com/pubsub/protomsg.git"
   "basic.com/valib/gogpu.git"
   "github.com/gogo/protobuf/proto"
)
const maxTryBeforeReboot = 10
type face struct {
   handle *SDKFace
@@ -27,6 +33,40 @@
   ipc2Rule            string
   ruleMsgMaxCacheSize int
   reserved            map[string]interface{}
   running     bool
   rebootUntil int
   mtxRunning  sync.Mutex
}
func (f *face) maybeReboot(ctx context.Context) {
   for {
      select {
      case <-ctx.Done():
         return
      default:
         f.mtxRunning.Lock()
         running := f.running
         f.mtxRunning.Unlock()
         if running {
            f.rebootUntil = 0
            f.mtxRunning.Lock()
            f.running = false
            f.mtxRunning.Unlock()
         } else {
            f.rebootUntil++
            f.fnLogger("Face No Running: ", f.rebootUntil)
            if f.rebootUntil > maxTryBeforeReboot {
               f.fnLogger("Face Too Long Running, Reboot")
               os.Exit(0)
            }
         }
         time.Sleep(time.Second)
      }
   }
}
// Create create sdk
@@ -112,6 +152,9 @@
      ipc2Rule:            ipc2Rule,
      ruleMsgMaxCacheSize: ruleMaxSize,
      reserved:            reserved,
      running:     true,
      rebootUntil: maxTryBeforeReboot,
   }
}
@@ -145,6 +188,8 @@
   go torule.Run(ctx)
   go s.run(ctx, chRcv, chSnd)
   go s.maybeReboot(ctx)
}
//////////////////////////////////////////////////////////////////
@@ -254,17 +299,69 @@
         // conv to bgr24 and resize
         imgW, imgH := int(i.Width), int(i.Height)
         f.fnLogger("Face Start Run:", dtchn, "CAMERAID: ", rMsg.Msg.Cid)
         // f.fnLogger("Face~~~Track", dtchn)
         detect := f.handle.Track(i.Data, imgW, imgH, 3, dtchn)
         // f.fnLogger("Face~~~Track Over", dtchn)
         count, data, _ := f.handle.Run(i.Data, imgW, imgH, 3, dtchn)
         var faces []*protomsg.ResultFaceDetect
         //将sdk返回值转换成protomsg类型
         for _, d := range detect {
            //运行sd
            // f.fnLogger("Face~~~FaceInfo2FacePos", dtchn)
            dec := FaceInfo2FacePos(d)
            // f.fnLogger("Face~~~FaceInfo2FacePos over", dtchn)
            // f.fnLogger("Face~~~Propertize", dtchn)
            prop := f.handle.Propertize(dec, i.Data, imgW, imgH, 3, dtchn)
            // f.fnLogger("Face~~~Propertize Over", dtchn)
            f.fnLogger("Face~~~Extract", dtchn)
            feat := f.handle.Extract(dec, i.Data, imgW, imgH, 3, dtchn)
            f.fnLogger("Face~~~Extract Over", dtchn)
            // f.fnLogger("Face~~~protomsg.ThftResult", dtchn)
            resP := (*protomsg.ThftResult)(unsafe.Pointer(&prop))
            // f.fnLogger("Face~~~protomsg.ThftResult Over", dtchn)
            // f.fnLogger("Face~~~tconvert2ProtoFacePos", dtchn)
            result := tconvert2ProtoFacePos(d)
            // f.fnLogger("Face~~~tconvert2ProtoFacePos Over", dtchn)
            //组成结果并序列化
            res := &protomsg.ResultFaceDetect{Pos: result, Result: resP, Feats: feat}
            faces = append(faces, res)
         }
         var err error
         var data []byte
         if len(faces) > 0 {
            // logo.Infoln("CID: ", rMsg.Msg.Cid, " TASK: ", rMsg.Msg.Tasklab.Taskid, " FACE TRACK OBJS: ", len(faces))
            facePos := protomsg.ParamFacePos{Faces: faces}
            data, err = proto.Marshal(&facePos)
            if err != nil {
               f.fnLogger("fdetect marshal proto face pos error", err)
               data = nil
            }
         }
         // f.fnLogger("Face~~~EjectResult", dtchn)
         common.EjectResult(data, rMsg, out)
         // f.fnLogger("Face~~~EjectResult Over", dtchn)
         f.mtxRunning.Lock()
         f.running = true
         f.mtxRunning.Unlock()
         var id, name string
         if rMsg.Msg.Tasklab != nil {
            id, name = rMsg.Msg.Tasklab.Taskid, rMsg.Msg.Tasklab.Taskname
         }
         f.fnLogger("Chan:", dtchn, "CAMERAID: ", rMsg.Msg.Cid, " TASKID: ", id, " TASKNAME: ", name, " DETECT ", f.typ, " COUNT: ", count)
         f.fnLogger("Chan:", dtchn, "CAMERAID: ", rMsg.Msg.Cid, " TASKID: ", id, " TASKNAME: ", name, " DETECT ", f.typ, " COUNT: ", len(faces))
         sc++
         if sc == 25 {