From 06d9122600054934d4793f8a55fe10289f410743 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期五, 15 十一月 2019 10:07:46 +0800
Subject: [PATCH] add comment
---
gosdk.go | 247 ++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 241 insertions(+), 6 deletions(-)
diff --git a/gosdk.go b/gosdk.go
index 240cfdb..5f2088e 100644
--- a/gosdk.go
+++ b/gosdk.go
@@ -1,11 +1,11 @@
package gosdk
/*
-#cgo CFLAGS: -I. -I./sdk/face/include -I./sdk/darknet/include -I/usr/local/cuda/include -w -g
-#cgo CXXFLAGS: -I. -I./sdk/face/include -I./sdk/darknet/include -I/usr/local/cuda/include -w -g -std=c++11
-#cgo LDFLAGS: -L/usr/local/cuda/lib64 -L${SRCDIR}/sdk/face/lib/gpu -L${SRCDIR}/sdk/darknet/lib
+#cgo CFLAGS: -I. -I./sdk/face/include -I./sdk/darknet/include -I/usr/local/cuda/include -I./sdk/plate/include -w -g
+#cgo CXXFLAGS: -I. -I./sdk/face/include -I./sdk/darknet/include -I/usr/local/cuda/include -I./sdk/plate/include -w -g -std=c++11
+#cgo LDFLAGS: -L/usr/local/cuda/lib64 -L${SRCDIR}/sdk/face/lib/gpu -L${SRCDIR}/sdk/darknet/lib -L${SRCDIR}/sdk/plate/lib
#cgo LDFLAGS: -Wl,-rpath,${SRCDIR}/sdk/face/lib/gpu:${SRCDIR}/sdk/darknet/lib
-#cgo LDFLAGS: -ldarknet -lTHFaceImage -lTHFeature -lTHFaceProperty -lTHFaceTracking -lcudart -lcublas -lcurand -lrt -ldl -lpthread
+#cgo LDFLAGS: -ldarknet -lTHFaceImage -lTHFeature -lTHFaceProperty -lTHFaceTracking -lcudart -lcublas -lcurand -lrt -ldl -lpthread -lthplateid
#include <stdlib.h>
#include "csdk.h"
*/
@@ -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 {
@@ -39,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
@@ -63,6 +67,12 @@
func InitFaceTracker(tm, gi, w, h, maxFaces, interval, sample int) {
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
@@ -212,6 +222,43 @@
}
}
+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
+ }
+ defer C.free(unsafe.Pointer(cFinfo))
+ 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 {
@@ -236,3 +283,191 @@
return C.GoString(p)
}
+
+func max(a, b int32) int32 {
+ if a < b {
+ return b
+ }
+ return a
+}
+
+func min(a, b int32) int32 {
+ if a < b {
+ return a
+ }
+ return b
+}
+
+func countInterAreaOfTwoRect(rect1 CRECT, rect2 CRECT) int32 {
+ xMin := min(rect1.Left, rect2.Left)
+ yMin := min(rect1.Top, rect2.Top)
+ xMax := max(rect1.Right, rect2.Right)
+ yMax := max(rect1.Bottom, rect2.Bottom)
+
+ wRect1 := rect1.Right - rect1.Left
+ hRect1 := rect1.Bottom - rect1.Top
+
+ wRect2 := rect2.Right - rect2.Left
+ hRect2 := rect2.Bottom - rect2.Top
+
+ wInter := wRect1 + wRect2 - (xMax - xMin)
+ hInter := hRect1 + hRect2 - (yMax - yMin)
+
+ if (wInter <= 0) || (hInter <= 0) {
+ return 0
+ }
+
+ areaInter := wInter * hInter
+ areaRect1 := wRect1 * hRect1
+ areaRect2 := wRect2 * hRect2
+ ratio := areaInter * 100 / min(areaRect1, areaRect2)
+
+ return ratio
+}
+
+// YoloDetect yolo detect (鍙瘑鍒汉)
+func YoloDetectTrack(handle *YoloHandle, img SDKImage, thrsh float32, umns int) (allObjs []CObjTrackInfo, newObjs []CObjTrackInfo) {
+
+ var tmp CObjTrackInfo
+ //LastYoloObjs
+ detectObjs := YoloDetect(handle, img, thrsh, umns)
+
+ 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 { //鍚屼竴绫诲埆锛屾瘮濡傞兘鏄汉浣�
+ 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 //涓婁竴甯ц窡韪殑鐩爣宸茬粡鎵惧埌锛屾棤闇�寰�涓嬪鐞嗗叾浠栨娴嬬洰鏍�
+ }
+ }
+ }
+ }
+
+ //澶勭悊鏂板嚭鐜扮殑鐩爣
+ if len(detectObjs) > 0 {
+ for _, vAdd := range detectObjs {
+ tmp.ObjInfo = vAdd
+ tmp.ID = handle.LastTrackID
+ handle.LastTrackID++
+
+ allObjs = append(allObjs, tmp)
+ newObjs = append(newObjs, tmp)
+ }
+ }
+
+ //鍒锋柊涓婁竴甯х殑璺熻釜鐩爣
+ 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
+}
+
+func DefaultPlateIDSDKConfig() *CPlateIDCfg{
+ return &CPlateIDCfg{
+ FastMemorySize: 32,
+ MemorySize: 400,
+ MinPlateWidth: 60,
+ MaxPlateWidth: 400,
+ MaxImageWidth: 4096,
+ MaxImageHeight: 2160,
+ IsFieldImage: 0,
+ MovingImage: 1,
+ OrderOpt: 0,
+ LeanCorrection: 1,
+ ImproveSpeed: 0,
+ CarLogo: 1,
+ LotDetect: 1,
+ Shadow: 1,
+ ShieldRailing: 1,
+ CarModel: 1,
+ LocateTh: 5,
+ OCRTh: 2,
+
+ Individual: 1,
+ TwoRowYellow: 1,
+ ArmPolice: 1,
+ ArmPolice2: 1,
+ TwoRowArmy: 1,
+ Tractor: 1,
+ Embassy: 1,
+ ChangNei: 1,
+ MinHang: 1,
+ Consulate: 1,
+ NewEnergy: 1,
+ OnlyTwoRowYellow: 0,
+ OnlyLocation: 0,
+ }
+}
+
+// InitPlateIDDetector init plateid detector
+func InitPlateIDDetector(config *CPlateIDCfg, soPath []byte) {
+ C.c_api_plate_id_init((*C.cPlateIDCfg)(unsafe.Pointer(config)), (*C.char)(unsafe.Pointer(&soPath[0])))
+}
+
+// PlateIDDetect plateid detect
+func PlateIDDetect(img SDKImage) []CPlateIDResult {
+ data := img.Data
+ w := img.Width
+ h := img.Height
+
+ var count C.int
+ cppos := C.c_api_plate_id_detect(&count, (*C.uchar)(unsafe.Pointer(&data[0])), C.int(w), C.int(h))
+ if cppos != nil {
+ defer C.free(unsafe.Pointer(cppos))
+ return CPlateIDPosArrayToGoArray(unsafe.Pointer(cppos), int(count))
+ }
+ return nil
+}
+
+func FreePlateIdDetector() int{
+ return int(C.c_api_plate_id_free())
+}
\ No newline at end of file
--
Gitblit v1.8.0