From 5a43718b678531f0db9073ef636ed74624154549 Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期三, 11 十二月 2019 13:27:56 +0800 Subject: [PATCH] update --- goyolo.go | 147 +++++++++++++++++++++--------------------------- 1 files changed, 64 insertions(+), 83 deletions(-) diff --git a/goyolo.go b/goyolo.go index 11a8bdd..742a70e 100644 --- a/goyolo.go +++ b/goyolo.go @@ -1,73 +1,37 @@ package main /* -#cgo CFLAGS: -I${SRCDIR}/darknet/include -I/usr/local/cuda-8.0/include -w -g -#cgo CXXFLAGS: -I${SRCDIR}/darknet/include -I/usr/local/cuda-8.0/include -w -g -std=c++11 -#cgo LDFLAGS: -L/usr/local/cuda-8.0/lib64 -L${SRCDIR}/darknet/lib -#cgo LDFLAGS: -Wl,-rpath,${SRCDIR}/darknet/lib +#cgo CFLAGS: -I${SRCDIR}/sdk/include -w -g +#cgo CXXFLAGS: -I${SRCDIR}/sdk/include -w -g -std=c++11 +#cgo LDFLAGS: -L/usr/local/cuda-8.0/lib64 -L${SRCDIR}/sdk/lib +#cgo LDFLAGS: -Wl,-rpath,${SRCDIR}/sdk/lib #cgo LDFLAGS: -ldarknet -lcudart -lcublas -lcurand -lcudnn -lrt -ldl -lpthread #include <stdlib.h> #include "cyolo.h" */ import "C" -import "unsafe" +import ( + "unsafe" -// CPOINT pt -type CPOINT struct { - X int32 - Y int32 -} + "basic.com/libgowrapper/sdkstruct.git" +) -// CRECT rc -type CRECT struct { - Left int32 - Top int32 - Right int32 - Bottom int32 -} - -// CIMAGE img -type CIMAGE struct { - Data *uint8 - Width int32 - Height int32 - Channel int32 - Pad_cgo_0 [4]byte -} - -// CObjInfo yolo -type CObjInfo struct { - RcObj CRECT - Typ int32 - Prob float32 -} - -// CObjTrackInfo track yolo objs info -type CObjTrackInfo struct { - ObjInfo CObjInfo - ID uint64 +type trackInfo struct { + lastTrackObjs []sdkstruct.CObjTrackInfo + lastTrackID uint64 } // YoloHandle wrap C type YoloHandle struct { - handle unsafe.Pointer - LastYoloObjs []CObjTrackInfo //yolo璺熻釜鐨勪笂涓�甯т俊鎭� - LastTrackID uint64 //yolo 琚娇鐢ㄧ殑ID + handle unsafe.Pointer + tracker map[string]*trackInfo } // RatioInterTrack 璺熻釜鍒ゆ柇閲嶅彔闃堝�� const RatioInterTrack = 50 //璺熻釜鍒ゆ柇閲嶅彔闃堝�� -// SDKImage sdk image -type SDKImage struct { - Data []byte - Width int - Height int - Channel int -} - -// NewYolo init yolo sdk -func NewYolo(fc, fw, fn string, gi int) *YoloHandle { +// NewSDK init yolo sdk +func NewSDK(fc, fw, fn string, gi int) interface{} { c := C.CString(fc) defer C.free(unsafe.Pointer(c)) @@ -79,24 +43,26 @@ g := C.int(gi) p := C.init(c, w, n, g) - return &YoloHandle{handle: p} + return &YoloHandle{ + handle: p, + tracker: make(map[string]*trackInfo), + } } // Free free -func (y *YoloHandle) Free() { - if y != nil { - if y.handle != nil { - C.release(y.handle) - } +func Free(i interface{}) { + y := i.(*YoloHandle) + if y != nil && y.handle != nil { + C.release(y.handle) } } // CYoloObjInfoArrayToGoArray convert cObjInfo array to go -func CYoloObjInfoArrayToGoArray(cArray unsafe.Pointer, count int) (goArray []CObjInfo) { +func CYoloObjInfoArrayToGoArray(cArray unsafe.Pointer, count int) (goArray []sdkstruct.CObjInfo) { p := uintptr(cArray) for i := 0; i < count; i++ { - j := *(*CObjInfo)(unsafe.Pointer(p)) + j := *(*sdkstruct.CObjInfo)(unsafe.Pointer(p)) goArray = append(goArray, j) p += unsafe.Sizeof(j) } @@ -104,15 +70,7 @@ } // YoloDetect yolo detect -func (y *YoloHandle) YoloDetect(img SDKImage, thrsh float32, umns int) []CObjInfo { - - data := img.Data - w := img.Width - h := img.Height - c := img.Channel - if c == 0 { - c = 3 - } +func YoloDetect(y *YoloHandle, data []byte, w, h, c int, thrsh float32, umns int) []sdkstruct.CObjInfo { var count C.int var cobjinfo unsafe.Pointer @@ -122,15 +80,16 @@ C.float(thrsh), C.int(umns), &cobjinfo, &count) - if ret == 0 { + if ret > 0 { + defer C.free(cobjinfo) return CYoloObjInfoArrayToGoArray(unsafe.Pointer(cobjinfo), int(count)) } return nil } // YoloObjName obj name by type -func (y *YoloHandle) YoloObjName(typ int) string { - p := C.obj_name_by_type(y.handle, C.int(typ)) +func YoloObjName(typ int) string { + p := C.obj_name_by_type(C.int(typ)) return C.GoString(p) } @@ -149,7 +108,7 @@ return b } -func countInterAreaOfTwoRect(rect1 CRECT, rect2 CRECT) int32 { +func countInterAreaOfTwoRect(rect1 sdkstruct.CRECT, rect2 sdkstruct.CRECT) int32 { xMin := min(rect1.Left, rect2.Left) yMin := min(rect1.Top, rect2.Top) xMax := max(rect1.Right, rect2.Right) @@ -176,14 +135,14 @@ return ratio } -// YoloDetectTrack yolo detect (鍙瘑鍒汉) -func (y *YoloHandle) YoloDetectTrack(img SDKImage, thrsh float32, umns int) (allObjs []CObjTrackInfo, newObjs []CObjTrackInfo) { +// YoloDetectTrack2 yolo detect (鍙瘑鍒汉) +func YoloDetectTrack2(y *YoloHandle, LastYoloObjs []sdkstruct.CObjTrackInfo, LastTrackID *uint64, data []byte, w, h, c int, thrsh float32, umns int) (allObjs []sdkstruct.CObjTrackInfo, newObjs []sdkstruct.CObjTrackInfo) { - var tmp CObjTrackInfo + var tmp sdkstruct.CObjTrackInfo //LastYoloObjs - detectObjs := y.YoloDetect(img, thrsh, umns) + detectObjs := YoloDetect(y, data, w, h, c, thrsh, umns) - for _, vLast := range y.LastYoloObjs { + for _, vLast := range LastYoloObjs { for i := 0; i < len(detectObjs); i++ { //fmt.Println("vNew.Typ:", vNew.Typ) if vLast.ObjInfo.Typ == detectObjs[i].Typ { //鍚屼竴绫诲埆锛屾瘮濡傞兘鏄汉浣� @@ -203,19 +162,41 @@ } //澶勭悊鏂板嚭鐜扮殑鐩爣 + id := *LastTrackID if len(detectObjs) > 0 { for _, vAdd := range detectObjs { tmp.ObjInfo = vAdd - tmp.ID = y.LastTrackID - y.LastTrackID++ + tmp.ID = id + id++ allObjs = append(allObjs, tmp) newObjs = append(newObjs, tmp) } } - - //鍒锋柊涓婁竴甯х殑璺熻釜鐩爣 - y.LastYoloObjs = allObjs - + *LastTrackID = id return allObjs, newObjs } + +// Run yolo detect (鍙瘑鍒汉) +func Run(i interface{}, id string, data []byte, w, h, c int, thrsh float32, umns int) ([]sdkstruct.CObjTrackInfo, []sdkstruct.CObjTrackInfo) { + if data == nil || w <= 0 || h <= 0 { + return nil, nil + } + y := i.(*YoloHandle) + + channel := c + if channel == 0 { + channel = 3 + } + + v, ok := y.tracker[id] + if !ok { + i := &trackInfo{} + y.tracker[id] = i + v = i + } + whole, recent := YoloDetectTrack2(y, v.lastTrackObjs, &v.lastTrackID, data, w, h, channel, thrsh, umns) + y.tracker[id].lastTrackObjs = whole + y.tracker[id].lastTrackID = v.lastTrackID + return whole, recent +} -- Gitblit v1.8.0