| | |
| | | */ |
| | | import "C" |
| | | import ( |
| | | "errors" |
| | | "unsafe" |
| | | |
| | | "basic.com/libgowrapper/sdkstruct.git" |
| | | "basic.com/pubsub/protomsg.git" |
| | | "github.com/gogo/protobuf/proto" |
| | | ) |
| | | |
| | | // SDKFace sdk |
| | |
| | | } |
| | | |
| | | // NewSDK sdk |
| | | func NewSDK(fn func(...interface{})) interface{} { |
| | | func NewSDK(fn func(...interface{})) *SDKFace { |
| | | h := C.create_sdkface() |
| | | if h == nil { |
| | | return nil |
| | |
| | | } |
| | | |
| | | // Free free |
| | | func Free(i interface{}) { |
| | | s := i.(*SDKFace) |
| | | func (s *SDKFace) Free() { |
| | | if s != nil && s.handle != nil { |
| | | C.release(s.handle) |
| | | } |
| | |
| | | } |
| | | |
| | | // 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 |
| | | } |
| | |
| | | } |
| | | |
| | | // 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 |
| | | } |
| | |
| | | } |
| | | |
| | | // Propertizer prop |
| | | func Propertizer(i interface{}, threadMax int) bool { |
| | | s := i.(*SDKFace) |
| | | func (s *SDKFace) Propertizer(threadMax int) bool { |
| | | |
| | | if s.propertizer { |
| | | return true |
| | | } |
| | |
| | | } |
| | | |
| | | // 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 |
| | | } |
| | |
| | | } |
| | | |
| | | // 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 |
| | | } |
| | |
| | | } |
| | | |
| | | // 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)) |
| | | |
| | |
| | | } |
| | | |
| | | // 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 |
| | | } |
| | |
| | | } |
| | | |
| | | // 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 |
| | | } |
| | |
| | | } |
| | | |
| | | // 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 |
| | | } |
| | |
| | | } |
| | | |
| | | // 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") |
| | |
| | | } |
| | | |
| | | // 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 |
| | |
| | | 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, |
| | | } |
| | | } |