派生自 libgowrapper/face

zhangmeng
2019-12-20 a79283f652e78fb8591a8af69f973055f9730c9d
goface.go
@@ -12,9 +12,12 @@
*/
import "C"
import (
   "errors"
   "unsafe"
   "basic.com/libgowrapper/sdkstruct.git"
   "basic.com/pubsub/protomsg.git"
   "github.com/gogo/protobuf/proto"
)
// SDKFace sdk
@@ -28,7 +31,7 @@
}
// NewSDK sdk
func NewSDK(fn func(...interface{})) interface{} {
func NewSDK(fn func(...interface{})) *SDKFace {
   h := C.create_sdkface()
   if h == nil {
      return nil
@@ -45,8 +48,7 @@
}
// Free free
func Free(i interface{}) {
   s := i.(*SDKFace)
func (s *SDKFace) Free() {
   if s != nil && s.handle != nil {
      C.release(s.handle)
   }
@@ -59,8 +61,8 @@
}
// Detector detector
func Detector(i interface{}, minFaces, rollAngles, threadMax, gpu int) bool {
   s := i.(*SDKFace)
func (s *SDKFace) Detector(minFaces, rollAngles, threadMax, gpu int) bool {
   if s.detector {
      return true
   }
@@ -74,8 +76,8 @@
}
// Extractor ext
func Extractor(i interface{}, threadMax, gpu int) bool {
   s := i.(*SDKFace)
func (s *SDKFace) Extractor(threadMax, gpu int) bool {
   if s.extractor {
      return true
   }
@@ -89,8 +91,8 @@
}
// Propertizer prop
func Propertizer(i interface{}, threadMax int) bool {
   s := i.(*SDKFace)
func (s *SDKFace) Propertizer(threadMax int) bool {
   if s.propertizer {
      return true
   }
@@ -104,8 +106,8 @@
}
// Tracker track
func Tracker(i interface{}, w, h, maxFaces, interval, sampleSize, threadMax, gpu int) bool {
   s := i.(*SDKFace)
func (s *SDKFace) Tracker(w, h, maxFaces, interval, sampleSize, threadMax, gpu int) bool {
   if s.tracker {
      return s.tracker
   }
@@ -134,7 +136,7 @@
}
// Detect det
func Detect(s *SDKFace, data []byte, w, h, c int, ch int) []sdkstruct.CFacePos {
func (s *SDKFace) Detect(data []byte, w, h, c int, ch int) []sdkstruct.CFacePos {
   if !s.detector {
      return nil
   }
@@ -150,7 +152,7 @@
}
// Extract extract
func Extract(s *SDKFace, fpos sdkstruct.CFacePos, data []byte, w, h, c int, ch int) []byte {
func (s *SDKFace) Extract(fpos sdkstruct.CFacePos, data []byte, w, h, c int, ch int) []byte {
   pos := (*C.cFacePos)(unsafe.Pointer(&fpos))
@@ -166,8 +168,8 @@
}
// Compare face compare
func Compare(i interface{}, feat1 []byte, feat2 []byte) float32 {
   s := i.(*SDKFace)
func (s *SDKFace) Compare(feat1 []byte, feat2 []byte) float32 {
   if s.extractor {
      return 0
   }
@@ -177,7 +179,7 @@
}
// Propertize prop
func Propertize(s *SDKFace, fpos sdkstruct.CFacePos, data []byte, w, h, c int, ch int) *sdkstruct.CThftResult {
func (s *SDKFace) Propertize(fpos sdkstruct.CFacePos, data []byte, w, h, c int, ch int) *sdkstruct.CThftResult {
   if !s.propertizer {
      return nil
   }
@@ -208,7 +210,7 @@
}
// Track track
func Track(s *SDKFace, data []byte, w, h, c int, ch int) []sdkstruct.CFaceInfo {
func (s *SDKFace) Track(data []byte, w, h, c int, ch int) []sdkstruct.CFaceInfo {
   if !s.tracker {
      return nil
   }
@@ -247,8 +249,7 @@
}
// TrackerResize init face tracker
func TrackerResize(i interface{}, w, h, ch int) bool {
   s := i.(*SDKFace)
func (s *SDKFace) TrackerResize(w, h, ch int) bool {
   if !s.tracker {
      s.printLog("->face--> TrackerResize Failed, No Tracker Init")
@@ -263,15 +264,13 @@
}
// Run run
func Run(i interface{}, data []byte, w, h, c, dchan int) []sdkstruct.CFaceResult {
func (s *SDKFace) Run(data []byte, w, h, c, dchan int) (int, []byte, error) {
   if data == nil || w <= 0 || h <= 0 {
      return nil
      return 0, nil, errors.New("->face--> Face Input Image Error")
   }
   s := i.(*SDKFace)
   if !s.tracker || !s.extractor || !s.propertizer {
      return nil
      return 0, nil, errors.New("->face--> Face SDK No Init Correctly")
   }
   channel := c
@@ -279,31 +278,70 @@
      channel = 3
   }
   // if !TrackerResize(i, w, h, dchan) {
   //    return nil
   // }
   var fInfo []sdkstruct.CFaceInfo
   fInfo = Track(s, data, w, h, c, dchan)
   fInfo = s.Track(data, w, h, c, dchan)
   if len(fInfo) == 0 {
      return 0, nil, errors.New("->face--> Face Track No One")
   }
   var faces []sdkstruct.CFaceResult
   //将sdk返回值转换成protomsg类型
   var faces []*protomsg.ResultFaceDetect
   for _, d := range fInfo {
      //运行sd
      dec := FaceInfo2FacePos(d)
      prop := Propertize(s, dec, data, w, h, c, dchan)
      feat := Extract(s, dec, data, w, h, c, dchan)
      p := s.Propertize(dec, data, w, h, c, dchan)
      feat := s.Extract(dec, data, w, h, c, dchan)
      result := sdkstruct.CFaceResult{
         Info: d,
         Prop: *prop,
         Feat: feat,
      }
      faces = append(faces, result)
      /// filter rules
      // sdkid := rMsg.Msg.Tasklab.Sdkinfos[rMsg.Msg.Tasklab.Index].Ipcid
      // size := (d.RcFace.Right - d.RcFace.Left) * (d.RcFace.Bottom - d.RcFace.Top)
      // angle := d.FAngle
      // if !filter(rMsg.Msg.Tasklab.Taskid, sdkid, angle.Confidence, float32(angle.Yaw), int(size)) {
      //    continue
      // }
      /// filter rules
      prop := (*protomsg.ThftResult)(unsafe.Pointer(&p))
      fpos := tconvert2ProtoFacePos(d)
      //组成结果并序列化
      res := &protomsg.ResultFaceDetect{Pos: fpos, Result: prop, Feats: feat}
      faces = append(faces, res)
   }
   facePos := protomsg.ParamFacePos{Faces: faces}
   d, e := proto.Marshal(&facePos)
   return faces
   return len(faces), d, e
}
func tconvert2ProtoFacePos(dec sdkstruct.CFaceInfo) *protomsg.FacePos {
   crect := dec.RcFace
   rect := protomsg.Rect{Left: crect.Left, Top: crect.Top, Right: crect.Right, Bottom: crect.Bottom}
   leftEye := (*protomsg.Point)(unsafe.Pointer(&dec.PtLeftEye))
   rightEye := (*protomsg.Point)(unsafe.Pointer(&dec.PtRightEye))
   mouth := (*protomsg.Point)(unsafe.Pointer(&dec.PtMouth))
   nose := (*protomsg.Point)(unsafe.Pointer(&dec.PtNose))
   angle := (*protomsg.FaceAngle)(unsafe.Pointer(&dec.FAngle))
   faceID := uint64(dec.NFaceID)
   facialData := dec.PFacialData[:512]
   // facialData := make([]byte, 512)
   // copy(facialData[:], dec.PFacialData[:512])
   return &protomsg.FacePos{
      RcFace:     &rect,
      PtLeftEye:  leftEye,
      PtRightEye: rightEye,
      PtMouth:    mouth,
      PtNose:     nose,
      FAngle:     angle,
      Quality:    dec.NQuality,
      FacialData: facialData,
      FaceID:     faceID,
   }
}