package sdk
|
|
import (
|
"analysis/logo"
|
"analysis/work"
|
"context"
|
"fmt"
|
"plugin"
|
"time"
|
|
"basic.com/libgowrapper/sdkstruct.git"
|
"basic.com/pubsub/protomsg.git"
|
"github.com/gogo/protobuf/proto"
|
)
|
|
// VehicleDetector plate id
|
type VehicleDetector struct {
|
licSrvPath string
|
modelPath string
|
|
handle interface{}
|
fnInit func() interface{}
|
fnFree func(interface{})
|
fnRun func(interface{}, []byte, int, int, int, int, int, int, int) []sdkstruct.CVehicleITSResult
|
}
|
|
// NewVehicleDetector plate
|
func NewVehicleDetector(licSrv, model string, w, h int) *VehicleDetector {
|
soFile := "libvehicle.so"
|
|
plug, err := plugin.Open(soFile)
|
if err != nil {
|
logo.Errorln("Open: ", soFile, " error: ", err)
|
return nil
|
}
|
|
fnInit, _ := LoadFunc(plug, soFile, "NewSDK")
|
fnFree, _ := LoadFunc(plug, soFile, "Free")
|
fnRun, _ := LoadFunc(plug, soFile, "Run")
|
|
return &VehicleDetector{
|
licSrvPath: licSrv,
|
modelPath: model,
|
|
handle: nil,
|
fnInit: fnInit.(func() interface{}),
|
fnFree: fnFree.(func(interface{})),
|
fnRun: fnRun.(func(interface{}, []byte, int, int, int, int, int, int, int) []sdkstruct.CVehicleITSResult),
|
}
|
}
|
|
// Init impl
|
func (d *VehicleDetector) Init() bool {
|
h := d.fnInit()
|
if h == nil {
|
logo.Errorln("INIT VEHICLE SDK ERROR")
|
return false
|
}
|
d.handle = h
|
|
logo.Infoln("RUN VEHICLE SDK")
|
|
return true
|
}
|
|
// Run impl
|
func (d *VehicleDetector) Run(ctx context.Context, in <-chan work.MsgRS, out chan<- work.MsgRS, typ string) {
|
tm := time.Now()
|
sc := 0
|
|
for {
|
select {
|
case <-ctx.Done():
|
d.fnFree(d.handle)
|
return
|
default:
|
rMsg := <-in
|
if !validRemoteMessage(rMsg, typ) {
|
ejectResult(nil, rMsg, out)
|
continue
|
}
|
|
i := unpackImage(rMsg, typ)
|
if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 {
|
ejectResult(nil, rMsg, out)
|
continue
|
}
|
|
imgW, imgH := int(i.Width), int(i.Height)
|
|
vehicle := d.fnRun(d.handle, i.Data, imgW, imgH, 3, 0, 0, imgW, imgH)
|
plateids := convert2ProtoPlateIDResultVehicle(vehicle)
|
|
plateresult := protomsg.PlateIDResult{Result: plateids}
|
data, err := proto.Marshal(&plateresult)
|
if err != nil {
|
fmt.Println("PLATE ID DETECTOR MARSHAL PROTO PLATE IDS ERROR", err)
|
data = nil
|
}
|
|
ejectResult(data, rMsg, out)
|
|
var id, name string
|
if rMsg.Msg.Tasklab != nil {
|
id, name = rMsg.Msg.Tasklab.Taskid, rMsg.Msg.Tasklab.Taskname
|
}
|
logo.Infoln("CAMERAID: ", rMsg.Msg.Cid, " TASKID: ", id, " TASKNAME: ", name, " DETECT VEHICLE PLATE ID COUNT: ", len(vehicle))
|
|
sc++
|
if sc == 25 {
|
logo.Infoln("PLATE ID DETECTOR RUN 25 FRAME USE TIME: ", time.Since(tm))
|
sc = 0
|
tm = time.Now()
|
}
|
|
if time.Since(tm) > time.Second {
|
logo.Infof("PLATE ID DETECTOR RUN %d FRAME USE TIME: %v", sc, time.Since(tm))
|
sc = 0
|
tm = time.Now()
|
}
|
|
}
|
}
|
}
|
|
func convert2ProtoPlateIDResultVehicle(obj []sdkstruct.CVehicleITSResult) []*protomsg.PlateIDVehicle {
|
ret := []*protomsg.PlateIDVehicle{}
|
|
for _, v := range obj {
|
|
rcPlateID := &protomsg.Rect{
|
Left: v.RcLocation.Left,
|
Right: v.RcLocation.Right,
|
Top: v.RcLocation.Top,
|
Bottom: v.RcLocation.Bottom,
|
}
|
rcCar := &protomsg.Rect{
|
Left: v.RcCarLocation.Left,
|
Right: v.RcCarLocation.Right,
|
Top: v.RcCarLocation.Top,
|
Bottom: v.RcCarLocation.Bottom,
|
}
|
|
lic := string(v.License[:])
|
end := len(lic)
|
for i := len(lic) - 1; i >= 0; i-- {
|
if lic[i] != '\000' {
|
end = i + 1
|
break
|
}
|
}
|
if end > 0 {
|
lic = lic[:end]
|
}
|
|
obj := &protomsg.PlateIDVehicle{
|
NPlateFlag: v.NPlateFlag,
|
NColor: v.NColor,
|
NType: v.NType,
|
License: lic,
|
NConfidence: v.NConfidence,
|
NCharNum: v.NCharNum,
|
NCharConfidence: v.NCharConfidence[:],
|
RcLocation: rcPlateID,
|
RcCarLocation: rcCar,
|
VehicleType: string(v.VehicleType[:]),
|
VehicleBrand: string(v.VehicleBrand[:]),
|
VehicleSub: string(v.VehicleSub[:]),
|
VehicleType1: string(v.VehicleType1[:]),
|
FvdConf: v.FVDConf,
|
FConfdence: v.FConfdence,
|
NVehicleSubModel: v.NVehicleSubModel,
|
NVehicleBright: v.NVehicleBright,
|
NVehicleColor1: v.NVehicleColor1,
|
NVehicleColor2: v.NVehicleColor2,
|
}
|
|
ret = append(ret, obj)
|
}
|
return ret
|
}
|
|
// hr := convert2ProtoHumanTrackResult(res)
|
// result := protomsg.HumanTrackResult{Result: hr[0]}
|
// data, err := proto.Marshal(&result)
|
// if err != nil {
|
// fmt.Println("HUMAN TRACKER MARSHAL PROTO PLATE IDS ERROR", err)
|
// data = nil
|
// }
|
|
// func convert2ProtoHumanTrackResult(obj []sdkstruct.FgResult) [][]*protomsg.HumanTrack {
|
// ret := [][]*protomsg.HumanTrack{}
|
// for _, v := range obj {
|
// res := []*protomsg.HumanTrack{}
|
// for i := 0; i < int(v.FgNum); i++ {
|
// r := v.Fginfo[i]
|
|
// rect := protomsg.Rect{
|
// Left: r.Left,
|
// Right: r.Right,
|
// Top: r.Top,
|
// Bottom: r.Bottom,
|
// }
|
// pr := &protomsg.HumanTrack{
|
// RcHuman: &rect,
|
// Confidence: r.Confidence,
|
// X: r.X,
|
// Y: r.Y,
|
// Id: r.ID,
|
// Feature: r.Feature[:],
|
// }
|
// res = append(res, pr)
|
// }
|
// ret = append(ret, res)
|
// }
|
// return ret
|
// }
|