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 |  133 ++++++++++++++++++++++++++++----------------
 1 files changed, 84 insertions(+), 49 deletions(-)

diff --git a/goface.go b/goface.go
index d2495c6..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
@@ -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,14 +61,14 @@
 }
 
 // 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")
+		s.printLog("->face--> CREATE Detector ERROR: ", ret)
 		return false
 	}
 	s.detector = true
@@ -74,14 +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")
+		s.printLog("->face--> CREATE Extractor ERROR: ", ret)
 		return false
 	}
 	s.extractor = true
@@ -89,14 +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")
+		s.printLog("->face--> CREATE Propertizer ERROR: ", ret)
 		return false
 	}
 	s.propertizer = true
@@ -104,15 +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")
+		s.printLog("->face--> CREATE Tracker ERROR: ", ret)
 		return false
 	}
 	s.tracker = true
@@ -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
 	}
@@ -224,10 +226,8 @@
 		//if len(faces) > 0{
 		// fmt.Println("faces detected:", len(faces))
 		//}
-
 		return faces
 	}
-	s.printLog("->face--> Track No One, Ret: ", ret)
 	return nil
 }
 
@@ -249,15 +249,14 @@
 }
 
 // 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)
@@ -265,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
@@ -281,32 +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
-	//灏唖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)
 
 	}
-	s.printLog("->face--> Run Detect Face Count: ", len(fInfo), " Result: ", len(faces))
+	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