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 |  126 +++++++----------------------------------
 1 files changed, 22 insertions(+), 104 deletions(-)

diff --git a/goyolo.go b/goyolo.go
index b7169bc..742a70e 100644
--- a/goyolo.go
+++ b/goyolo.go
@@ -11,51 +11,13 @@
 */
 import "C"
 import (
-	"fmt"
 	"unsafe"
 
-	"basic.com/pubsub/protomsg.git"
-	"github.com/gogo/protobuf/proto"
+	"basic.com/libgowrapper/sdkstruct.git"
 )
 
-// CPOINT pt
-type CPOINT struct {
-	X int32
-	Y int32
-}
-
-// 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 []CObjTrackInfo
+	lastTrackObjs []sdkstruct.CObjTrackInfo
 	lastTrackID   uint64
 }
 
@@ -68,8 +30,8 @@
 // RatioInterTrack 璺熻釜鍒ゆ柇閲嶅彔闃堝��
 const RatioInterTrack = 50 //璺熻釜鍒ゆ柇閲嶅彔闃堝��
 
-// 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))
@@ -88,20 +50,19 @@
 }
 
 // Free free
-func Free(y *YoloHandle) {
-	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)
 	}
@@ -109,7 +70,7 @@
 }
 
 // YoloDetect yolo detect
-func YoloDetect(y *YoloHandle, data []byte, w, h, c int, thrsh float32, umns int) []CObjInfo {
+func YoloDetect(y *YoloHandle, data []byte, w, h, c int, thrsh float32, umns int) []sdkstruct.CObjInfo {
 
 	var count C.int
 	var cobjinfo unsafe.Pointer
@@ -119,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 YoloObjName(y *YoloHandle, 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)
 }
@@ -146,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)
@@ -174,9 +136,9 @@
 }
 
 // YoloDetectTrack2 yolo detect   (鍙瘑鍒汉)
-func YoloDetectTrack2(y *YoloHandle, LastYoloObjs []CObjTrackInfo, LastTrackID *uint64, data []byte, w, h, c int, thrsh float32, umns int) (allObjs []CObjTrackInfo, newObjs []CObjTrackInfo) {
+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 := YoloDetect(y, data, w, h, c, thrsh, umns)
 
@@ -216,10 +178,12 @@
 }
 
 // Run yolo detect   (鍙瘑鍒汉)
-func Run(y *YoloHandle, id string, data []byte, w, h, c int, thrsh float32, umns int) ([]byte, []byte) {
+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
@@ -234,51 +198,5 @@
 	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
-
-	var dWhole, dRecent []byte
-	var err error
-	if len(whole) > 0 {
-
-		infos := convert2ProtoYoloTrack(whole, 1.0, 1.0)
-		p := protomsg.ParamYoloObj{Infos: infos}
-
-		dWhole, err = proto.Marshal(&p)
-		if err != nil {
-			fmt.Println("ydetect track marshal proto yolo obj error", err)
-			dWhole = nil
-		}
-	}
-	if len(recent) > 0 {
-		dRecent = nil
-	}
-	return dWhole, dRecent
-}
-
-func convert2ProtoYoloTrack(obj []CObjTrackInfo, fx, fy float64) []*protomsg.ObjInfo {
-	ret := []*protomsg.ObjInfo{}
-
-	for _, v := range obj {
-		if fx < 1.0 || fy < 1.0 {
-			v.ObjInfo.RcObj.Left = (int32)((float64)(v.ObjInfo.RcObj.Left) / fx)
-			v.ObjInfo.RcObj.Right = (int32)((float64)(v.ObjInfo.RcObj.Right) / fx)
-			v.ObjInfo.RcObj.Top = (int32)((float64)(v.ObjInfo.RcObj.Top) / fy)
-			v.ObjInfo.RcObj.Bottom = (int32)((float64)(v.ObjInfo.RcObj.Bottom) / fy)
-		}
-
-		rect := protomsg.Rect{
-			Left:   v.ObjInfo.RcObj.Left,
-			Right:  v.ObjInfo.RcObj.Right,
-			Top:    v.ObjInfo.RcObj.Top,
-			Bottom: v.ObjInfo.RcObj.Bottom,
-		}
-		obj := protomsg.ObjInfo{
-			RcObj: &rect,
-			Typ:   v.ObjInfo.Typ,
-			Prob:  v.ObjInfo.Prob,
-			ObjID: v.ID,
-		}
-
-		ret = append(ret, &obj)
-	}
-	return ret
+	return whole, recent
 }

--
Gitblit v1.8.0