派生自 libgowrapper/yolo

zhangmeng
2019-12-10 eb19ddaebdf451cee76dc31082a3194a5f9b50ac
goyolo.go
@@ -1,16 +1,22 @@
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 "detector.h"
#include "cyolo.h"
*/
import "C"
import "unsafe"
import (
   "fmt"
   "unsafe"
   "basic.com/pubsub/protomsg.git"
   "github.com/gogo/protobuf/proto"
)
// CPOINT pt
type CPOINT struct {
@@ -48,25 +54,22 @@
   ID      uint64
}
type trackInfo struct {
   lastTrackObjs []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
}
// 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))
@@ -78,11 +81,15 @@
   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() {
func Free(i interface{}) {
   y := i.(*YoloHandle)
   if y != nil {
      if y.handle != nil {
         C.release(y.handle)
@@ -103,17 +110,13 @@
}
// YoloDetect yolo detect
func (y *YoloHandle) YoloDetect(img SDKImage, thrsh float32, umns int) []CObjInfo {
   data := img.Data
   w := img.Width
   h := img.Height
func YoloDetect(y *YoloHandle, data []byte, w, h, c int, thrsh float32, umns int) []CObjInfo {
   var count C.int
   var cobjinfo unsafe.Pointer
   ret := C.detect(y.handle,
      unsafe.Pointer(&data[0]), C.int(w), C.int(h), 3,
      unsafe.Pointer(&data[0]), C.int(w), C.int(h), C.int(c),
      C.float(thrsh), C.int(umns),
      &cobjinfo, &count)
@@ -124,7 +127,8 @@
}
// YoloObjName obj name by type
func (y *YoloHandle) YoloObjName(typ int) string {
func YoloObjName(i interface{}, typ int) string {
   y := i.(*YoloHandle)
   p := C.obj_name_by_type(y.handle, C.int(typ))
   return C.GoString(p)
@@ -171,14 +175,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 []CObjTrackInfo, LastTrackID *uint64, data []byte, w, h, c int, thrsh float32, umns int) (allObjs []CObjTrackInfo, newObjs []CObjTrackInfo) {
   var tmp 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 { //同一类别,比如都是人体
@@ -198,19 +202,126 @@
   }
   //处理新出现的目标
   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) ([]byte, int) {
   if data == nil || w <= 0 || h <= 0 {
      return nil, 0
   }
   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, _ := 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 []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
      }
   }
   return dWhole, len(whole)
}
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
}
// Run2 yolo detect   (只识别人)
func Run2(i interface{}, id string, data []byte, w, h, c int, thrsh float32, umns int) ([]byte, int, []byte, int) {
   if data == nil || w <= 0 || h <= 0 {
      return nil, 0, nil, 0
   }
   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
   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, len(whole), dRecent, len(recent)
}