From c100c51a38101792945bae4078705570a5f7fe8b Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期六, 11 一月 2020 16:32:26 +0800 Subject: [PATCH] debug --- goface.go | 140 ++++++++++++++++++++++++++++++++-------------- 1 files changed, 98 insertions(+), 42 deletions(-) diff --git a/goface.go b/goface.go index 8370f71..7ac9122 100644 --- a/goface.go +++ b/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 @@ -24,10 +27,11 @@ extractor bool propertizer bool tracker bool + fnLogger func(...interface{}) } // NewSDK sdk -func NewSDK() interface{} { +func NewSDK(fn func(...interface{})) *SDKFace { h := C.create_sdkface() if h == nil { return nil @@ -39,25 +43,32 @@ extractor: false, propertizer: false, tracker: false, + fnLogger: fn, } } // Free free -func Free(i interface{}) { - s := i.(*SDKFace) +func (s *SDKFace) Free() { if s != nil && s.handle != nil { C.release(s.handle) } } +func (s *SDKFace) printLog(l ...interface{}) { + if s.fnLogger != nil { + s.fnLogger(l) + } +} + // 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 } ret := C.init_detector(s.handle, C.int(minFaces), C.int(rollAngles), C.int(threadMax), C.int(gpu)) if ret <= 0 { + s.printLog("->face--> CREATE Detector ERROR: ", ret) return false } s.detector = true @@ -65,13 +76,14 @@ } // 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 } ret := C.init_extractor(s.handle, C.int(threadMax), C.int(gpu)) if ret <= 0 { + s.printLog("->face--> CREATE Extractor ERROR: ", ret) return false } s.extractor = true @@ -79,13 +91,14 @@ } // Propertizer prop -func Propertizer(i interface{}, threadMax int) bool { - s := i.(*SDKFace) +func (s *SDKFace) Propertizer(threadMax int) bool { + if s.propertizer { return true } ret := C.init_propertizer(s.handle, C.int(threadMax)) if ret <= 0 { + s.printLog("->face--> CREATE Propertizer ERROR: ", ret) return false } s.propertizer = true @@ -93,14 +106,15 @@ } // 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 } ret := C.init_tracker(s.handle, C.int(w), C.int(h), C.int(maxFaces), C.int(interval), C.int(sampleSize), C.int(threadMax), C.int(gpu)) if ret <= 0 { + s.printLog("->face--> CREATE Tracker ERROR: ", ret) return false } s.tracker = true @@ -122,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 } @@ -133,27 +147,29 @@ if ret > 0 { return CFacePosArrayToGoArray(cfpos, int(count)) } + s.printLog("->face--> Detect No One, Ret: ", ret) 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)) //(void *handle, const cFacePos *pos, const void*data, const int w, const int h, const int c, const int chan, void **feat, int *featLen); var feat unsafe.Pointer var featLen C.int - p := C.extract(s.handle, pos, unsafe.Pointer(&data[0]), C.int(w), C.int(h), C.int(c), C.int(ch), &feat, &featLen) - if p > 0 { + ret := C.extract(s.handle, pos, unsafe.Pointer(&data[0]), C.int(w), C.int(h), C.int(c), C.int(ch), &feat, &featLen) + if ret > 0 { return C.GoBytes(feat, featLen) } + s.printLog("->face--> Extract Nothing, Ret: ", ret) return nil } // 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 } @@ -163,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 } @@ -177,6 +193,7 @@ C.free(thft) return &gothft } + s.printLog("->face--> Propertize Nothing, Ret: ", ret) return nil } @@ -193,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 } @@ -209,7 +226,6 @@ //if len(faces) > 0{ // fmt.Println("faces detected:", len(faces)) //} - return faces } return nil @@ -233,29 +249,28 @@ } // 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") return false } - ret := C.resize(s.handle, C.int(w), C.int(h), C.int(ch)) - if ret >= 0 { + ret := C.track_resize(s.handle, C.int(w), C.int(h), C.int(ch)) + if ret == 1 { return true } + s.printLog("->face--> TrackerResize Failed, Ret: ", ret, " SDK Channel: ", ch, " Size: ", w, "x", h) return false } // 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 @@ -265,27 +280,68 @@ var fInfo []sdkstruct.CFaceInfo - if s.tracker { - 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 - //灏唖dk杩斿洖鍊艰浆鎹㈡垚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, + } } -- Gitblit v1.8.0