From b7340a34ff68f018a4aa0e7aada3b7feaabd2fe1 Mon Sep 17 00:00:00 2001 From: chenshijun <csj_sky@126.com> Date: 星期二, 22 十月 2019 14:52:49 +0800 Subject: [PATCH] 增加人脸跟踪检测的通道分辨率调整接口face_track_resize --- gosdk.go | 144 +++++++++++++++++++++++++++++++---------------- 1 files changed, 94 insertions(+), 50 deletions(-) diff --git a/gosdk.go b/gosdk.go index 3e230d4..42b4dcd 100644 --- a/gosdk.go +++ b/gosdk.go @@ -16,8 +16,12 @@ // YoloHandle wrap C type YoloHandle struct { - handle C.YoloHandle + handle C.YoloHandle + LastYoloObjs []CObjTrackInfo //yolo璺熻釜鐨勪笂涓�甯т俊鎭� + LastTrackID uint64 //yolo 琚娇鐢ㄧ殑ID } + +const RatioInterTrack = 50 //璺熻釜鍒ゆ柇閲嶅彔闃堝�� // SDKImage sdk image type SDKImage struct { @@ -25,10 +29,6 @@ Width int Height int } - -var LastYoloObjs []CObjTrackInfo //yolo璺熻釜鐨勪笂涓�甯т俊鎭� -var LastTrackID uint64 = 0 //yolo 琚娇鐢ㄧ殑ID -const RatioInterTrack = 50 //璺熻釜鍒ゆ柇閲嶅彔闃堝�� // InitYolo init yolo sdk func InitYolo(fc, fw, fn string, gi int) *YoloHandle { @@ -43,7 +43,7 @@ g := C.int(gi) p := C.c_api_yolo_init(c, w, n, g) - return &YoloHandle{p} + return &YoloHandle{handle: p} } // InitFaceDetector init face detector @@ -69,6 +69,12 @@ C.c_api_face_tracker_init(C.int(tm), C.int(gi), C.int(w), C.int(h), C.int(maxFaces), C.int(interval), C.int(sample)) } +// ResizeFaceTracker init face tracker +func ResizeFaceTracker(ch, w, h int) int { + + return int(C.c_api_face_track_resize(C.int(ch), C.int(w), C.int(h))) +} + // Free free sdk func Free() { C.c_api_release() @@ -84,7 +90,6 @@ 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)) } return nil @@ -113,7 +118,6 @@ 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)) - defer C.free(unsafe.Pointer(p)) b := C.GoBytes(unsafe.Pointer(p), featLen) return b } @@ -122,36 +126,6 @@ func FaceCompare(feat1 []byte, feat2 []byte) float32 { res := C.c_api_face_compare((*C.uchar)(unsafe.Pointer(&feat1[0])), (*C.uchar)(unsafe.Pointer(&feat2[0]))) return float32(res) -} - -// FaceTrackOnly face tracker face only -func FaceTrackOnly(img SDKImage, ch int) []CRECT { - data := img.Data - w := img.Width - h := img.Height - - var fCount C.int - rect := C.c_api_face_track_only(&fCount, (*C.uchar)(unsafe.Pointer(&data[0])), C.int(w), C.int(h), C.int(ch)) - if rect != nil { - defer C.free(unsafe.Pointer(rect)) - return CRECTArrayToGoArray(unsafe.Pointer(rect), int(fCount)) - } - return nil -} - -// FaceTrackDetect face tracker face detect -func FaceTrackDetect(img SDKImage, ch int) []CFaceInfo { - data := img.Data - w := img.Width - h := img.Height - - var fCount C.int - finfo := C.c_api_face_track_only(&fCount, (*C.uchar)(unsafe.Pointer(&data[0])), C.int(w), C.int(h), C.int(ch)) - if finfo != nil { - defer C.free(unsafe.Pointer(finfo)) - return CFaceInfoArrayToGoArray(unsafe.Pointer(finfo), int(fCount)) - } - return nil } // FaceTrackingInfo face track info @@ -171,7 +145,6 @@ if cFinfo == nil { return } - defer C.free(unsafe.Pointer(cFinfo)) goFinfo := CFaceInfoArrayToGoArray(unsafe.Pointer(cFinfo), int(fCount)) // 绌猴紝娣诲姞 @@ -216,6 +189,42 @@ } } +func FaceInfo2FacePos(face CFaceInfo) (fPos CFacePos) { + fPos.RcFace = face.RcFace + fPos.PtLeftEye = face.PtLeftEye + fPos.PtRightEye = face.PtRightEye + fPos.PtNose = face.PtNose + fPos.PtMouth = face.PtMouth + fPos.FAngle.Yaw = face.FAngle.Yaw + fPos.FAngle.Pitch = face.FAngle.Pitch + fPos.FAngle.Roll = face.FAngle.Roll + fPos.FAngle.Confidence = face.FAngle.Confidence + + copy(fPos.PFacialData[:], face.PFacialData[:512]) + + return fPos +} + +// FaceTrackSimple face tracking info +func FaceTrackSimple(img SDKImage, ch int) (faces []CFaceInfo) { + data := img.Data + w := img.Width + h := img.Height + + 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 faces + } + faces = CFaceInfoArrayToGoArray(unsafe.Pointer(cFinfo), int(fCount)) + //if len(faces) > 0{ + // fmt.Println("faces detected:", len(faces)) + //} + + return faces +} + // YoloDetect yolo detect func YoloDetect(handle *YoloHandle, img SDKImage, thrsh float32, umns int) []CObjInfo { @@ -228,7 +237,6 @@ cobjinfo := C.c_api_yolo_detect(handle.handle, &count, (*C.uchar)(unsafe.Pointer(&data[0])), C.int(w), C.int(h), C.float(thrsh), C.int(umns)) if cobjinfo != nil { - defer C.free(unsafe.Pointer(cobjinfo)) return CYoloObjInfoArrayToGoArray(unsafe.Pointer(cobjinfo), int(count)) } return nil @@ -288,14 +296,8 @@ var tmp CObjTrackInfo //LastYoloObjs detectObjs := YoloDetect(handle, img, thrsh, umns) - for i := 0; i < len(detectObjs); i++ { - if detectObjs[i].Typ != 0 { - detectObjs = append(detectObjs[:i], detectObjs[i+1:]...) //浠庢娴嬬洰鏍囬噷鍒犻櫎宸茬粡鏌ュ埌鐨勮窡韪洰鏍� - i-- - } - } - for _, vLast := range LastYoloObjs { + for _, vLast := range handle.LastYoloObjs { for i := 0; i < len(detectObjs); i++ { //fmt.Println("vNew.Typ:", vNew.Typ) if vLast.ObjInfo.Typ == detectObjs[i].Typ { //鍚屼竴绫诲埆锛屾瘮濡傞兘鏄汉浣� @@ -318,8 +320,8 @@ if len(detectObjs) > 0 { for _, vAdd := range detectObjs { tmp.ObjInfo = vAdd - tmp.ID = LastTrackID - LastTrackID++ + tmp.ID = handle.LastTrackID + handle.LastTrackID++ allObjs = append(allObjs, tmp) newObjs = append(newObjs, tmp) @@ -327,7 +329,49 @@ } //鍒锋柊涓婁竴甯х殑璺熻釜鐩爣 - LastYoloObjs = allObjs + handle.LastYoloObjs = allObjs return allObjs, newObjs } + +// YoloDetectTrack2 yolo detect (鍙瘑鍒汉) +func YoloDetectTrack2(handle *YoloHandle, LastYoloObjs []CObjTrackInfo, LastTrackID *uint64, img SDKImage, thrsh float32, umns int) (allObjs []CObjTrackInfo, newObjs []CObjTrackInfo) { + + var tmp CObjTrackInfo + //LastYoloObjs + detectObjs := YoloDetect(handle, img, thrsh, umns) + + for _, vLast := range LastYoloObjs { + for i := 0; i < len(detectObjs); i++ { + //fmt.Println("vNew.Typ:", vNew.Typ) + if vLast.ObjInfo.Typ == detectObjs[i].Typ { //鍚屼竴绫诲埆锛屾瘮濡傞兘鏄汉浣� + ratio := countInterAreaOfTwoRect(vLast.ObjInfo.RcObj, detectObjs[i].RcObj) + if ratio >= RatioInterTrack { + //update LastYoloObjs + vLast.ObjInfo.RcObj = detectObjs[i].RcObj + vLast.ObjInfo.Prob = detectObjs[i].Prob + + allObjs = append(allObjs, vLast) + detectObjs = append(detectObjs[:i], detectObjs[i+1:]...) //浠庢娴嬬洰鏍囬噷鍒犻櫎宸茬粡鏌ュ埌鐨勮窡韪洰鏍� + i-- + break //涓婁竴甯ц窡韪殑鐩爣宸茬粡鎵惧埌锛屾棤闇�寰�涓嬪鐞嗗叾浠栨娴嬬洰鏍� + } + } + } + } + + //澶勭悊鏂板嚭鐜扮殑鐩爣 + id := *LastTrackID + if len(detectObjs) > 0 { + for _, vAdd := range detectObjs { + tmp.ObjInfo = vAdd + tmp.ID = id + id++ + + allObjs = append(allObjs, tmp) + newObjs = append(newObjs, tmp) + } + } + *LastTrackID = id + return allObjs, newObjs +} -- Gitblit v1.8.0