| | |
| | | |
| | | // SDKFace sdk |
| | | type SDKFace struct { |
| | | handle unsafe.Pointer |
| | | detector bool |
| | | extractor bool |
| | | propertizer bool |
| | |
| | | |
| | | // NewSDK sdk |
| | | func NewSDK(fn func(...interface{})) *SDKFace { |
| | | h := C.create_sdkface() |
| | | if h == nil { |
| | | return nil |
| | | } |
| | | |
| | | return &SDKFace{ |
| | | handle: h, |
| | | detector: false, |
| | | extractor: false, |
| | | propertizer: false, |
| | |
| | | |
| | | // Free free |
| | | func (s *SDKFace) Free() { |
| | | C.c_api_release() |
| | | if s != nil && s.handle != nil { |
| | | C.release(s.handle) |
| | | } |
| | | } |
| | | |
| | | func (s *SDKFace) printLog(l ...interface{}) { |
| | |
| | | } |
| | | |
| | | // Detector detector |
| | | func (s *SDKFace) Detector(minFaces, rollAngle, threadMax, gpu int) bool { |
| | | func (s *SDKFace) Detector(minFaces, rollAngles, threadMax, gpu int) bool { |
| | | |
| | | if s.detector { |
| | | return true |
| | | } |
| | | |
| | | ret := C.c_api_face_detector_init(C.int(threadMax), C.int(gpu), C.int(minFaces), C.int(rollAngle)) |
| | | |
| | | 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 |
| | |
| | | if s.extractor { |
| | | return true |
| | | } |
| | | ret := C.c_api_face_extractor_init(C.int(threadMax), C.int(gpu)) |
| | | ret := C.init_extractor(s.handle, C.int(threadMax), C.int(gpu)) |
| | | if ret <= 0 { |
| | | s.printLog("->face--> CREATE Extractor ERROR: ", ret) |
| | | return false |
| | |
| | | if s.propertizer { |
| | | return true |
| | | } |
| | | ret := C.c_api_face_property_init(C.int(threadMax)) |
| | | ret := C.init_propertizer(s.handle, C.int(threadMax)) |
| | | if ret <= 0 { |
| | | s.printLog("->face--> CREATE Propertizer ERROR: ", ret) |
| | | return false |
| | |
| | | func (s *SDKFace) Tracker(w, h, maxFaces, interval, sampleSize, threadMax, gpu int) bool { |
| | | |
| | | if s.tracker { |
| | | return true |
| | | return s.tracker |
| | | } |
| | | ret := C.c_api_face_tracker_init(C.int(threadMax), C.int(gpu), C.int(w), C.int(h), C.int(maxFaces), C.int(interval), C.int(sampleSize)) |
| | | 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 |
| | |
| | | return nil |
| | | } |
| | | |
| | | var cfpos unsafe.Pointer |
| | | var count C.int |
| | | cfpos := C.c_api_face_detect(&count, (*C.uchar)(unsafe.Pointer(&data[0])), C.int(w), C.int(h), C.int(ch)) |
| | | if cfpos != nil { |
| | | defer C.free(unsafe.Pointer(cfpos)) |
| | | return CFacePosArrayToGoArray(unsafe.Pointer(cfpos), int(count)) |
| | | ret := C.detect(s.handle, unsafe.Pointer(&data[0]), C.int(w), C.int(h), C.int(c), C.int(ch), &cfpos, &count) |
| | | if ret > 0 { |
| | | return CFacePosArrayToGoArray(cfpos, int(count)) |
| | | } |
| | | |
| | | s.printLog("->face--> Detect No One") |
| | | s.printLog("->face--> Detect No One, Ret: ", ret) |
| | | return nil |
| | | } |
| | | |
| | | // Extract extract |
| | | func (s *SDKFace) Extract(fpos sdkstruct.CFacePos, data []byte, w, h, c int, ch int) []byte { |
| | | if !s.extractor { |
| | | return nil |
| | | } |
| | | |
| | | var featLen C.int |
| | | pos := (*C.cFacePos)(unsafe.Pointer(&fpos)) |
| | | |
| | | p := C.c_api_face_extract(&featLen, pos, (*C.uchar)(unsafe.Pointer(&data[0])), C.int(w), C.int(h), C.int(ch)) |
| | | if p != nil { |
| | | defer C.free(unsafe.Pointer(p)) |
| | | return C.GoBytes(unsafe.Pointer(p), featLen) |
| | | //(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 |
| | | 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") |
| | | s.printLog("->face--> Extract Nothing, Ret: ", ret) |
| | | return nil |
| | | } |
| | | |
| | |
| | | return 0 |
| | | } |
| | | |
| | | res := C.c_api_face_compare((*C.uchar)(unsafe.Pointer(&feat1[0])), (*C.uchar)(unsafe.Pointer(&feat2[0]))) |
| | | res := C.compare(s.handle, (*C.uchar)(unsafe.Pointer(&feat1[0])), (*C.uchar)(unsafe.Pointer(&feat2[0]))) |
| | | return float32(res) |
| | | } |
| | | |
| | | // Propertize prop |
| | | func (s *SDKFace) Propertize(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 sdkstruct.CThftResult{Age: 0} |
| | | return nil |
| | | } |
| | | |
| | | pos := (*C.cFacePos)(unsafe.Pointer(&fpos)) |
| | | result := C.c_api_face_property(pos, (*C.uchar)(unsafe.Pointer(&data[0])), C.int(w), C.int(h), C.int(ch)) |
| | | s.printLog("->face--> Propertize") |
| | | return *(*sdkstruct.CThftResult)(unsafe.Pointer(&result)) |
| | | |
| | | var thft unsafe.Pointer |
| | | ret := C.propertize(s.handle, pos, unsafe.Pointer(&data[0]), C.int(w), C.int(h), C.int(c), C.int(ch), &thft) |
| | | if ret == 0 { |
| | | gothft := *(*sdkstruct.CThftResult)(thft) |
| | | C.free(thft) |
| | | return &gothft |
| | | } |
| | | s.printLog("->face--> Propertize Nothing, Ret: ", ret) |
| | | return nil |
| | | } |
| | | |
| | | // CFaceInfoArrayToGoArray convert cFaceInfo array to go |
| | |
| | | //img, const int chan, void **fInfo, int *fcnt); |
| | | |
| | | var fCount C.int |
| | | cFinfo := C.c_api_face_track(&fCount, (*C.uchar)(unsafe.Pointer(&data[0])), C.int(w), C.int(h), C.int(ch)) |
| | | // fmt.Println("cFinfo detected:", cFinfo) |
| | | if cFinfo == nil { |
| | | return nil |
| | | } |
| | | defer C.free(unsafe.Pointer(cFinfo)) |
| | | faces := CFaceInfoArrayToGoArray(unsafe.Pointer(cFinfo), int(fCount)) |
| | | var finfos unsafe.Pointer |
| | | ret := C.track(s.handle, unsafe.Pointer(&data[0]), C.int(w), C.int(h), C.int(c), C.int(ch), &finfos, &fCount) |
| | | |
| | | if ret > 0 { |
| | | faces := CFaceInfoArrayToGoArray(finfos, int(fCount)) |
| | | //if len(faces) > 0{ |
| | | // fmt.Println("faces detected:", len(faces)) |
| | | //} |
| | | return faces |
| | | } |
| | | return nil |
| | | } |
| | | |
| | | // FaceInfo2FacePos info -> pos |
| | |
| | | } |
| | | |
| | | // TrackerResize init face tracker |
| | | func (s *SDKFace) TrackerResize(w, h, ch int) int { |
| | | func (s *SDKFace) TrackerResize(w, h, ch int) bool { |
| | | |
| | | if !s.tracker { |
| | | s.printLog("->face--> TrackerResize Failed, No Tracker Init") |
| | | return -1 |
| | | return false |
| | | } |
| | | return int(C.c_api_face_track_resize(C.int(ch), C.int(w), C.int(h))) |
| | | 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 |
| | |
| | | |
| | | //运行sd |
| | | dec := FaceInfo2FacePos(d) |
| | | p := s.Propertize(dec, data, w, h, c, dchan) |
| | | p := s.Propertize(dec, data, w, h, c, 0) |
| | | feat := s.Extract(dec, data, w, h, c, dchan) |
| | | |
| | | /// filter rules |