package sdk
|
|
import (
|
"analysis/logo"
|
"analysis/work"
|
"analysis/work/cache"
|
"context"
|
"encoding/binary"
|
"math"
|
|
"basic.com/pubsub/protomsg.git"
|
"basic.com/ruleForSdk.git"
|
|
"github.com/gogo/protobuf/proto"
|
sq "github.com/yireyun/go-queue"
|
)
|
|
// Engine interface sdk
|
type Engine interface {
|
Init() bool
|
Run(context.Context, <-chan work.MsgRS, chan<- work.MsgRS, string)
|
}
|
|
func createQueue(length int) *sq.EsQueue {
|
q := sq.NewQueue(uint32(length))
|
for i := 0; i < length; i++ {
|
q.Put(i)
|
}
|
return q
|
}
|
|
func ejectResult(res []byte, msg work.MsgRS, out chan<- work.MsgRS) {
|
//压缩数据
|
// rData, err := work.Compress(res)
|
// if err != nil {
|
// logo.Errorln("compress result error: ", err)
|
// return
|
// }
|
|
if res == nil {
|
out <- msg
|
return
|
}
|
index := int(msg.Msg.Tasklab.Index)
|
|
if index >= len(msg.Msg.Tasklab.Sdkinfos) {
|
return
|
}
|
|
// var tmpD []byte
|
// var tmpE error
|
// tmpD, tmpE = proto.Marshal(&msg.Msg)
|
// if tmpE == nil {
|
// logo.Infoln(msg.Msg.Tasklab.Sdkinfos[index].Sdktype, " orig SDK DATA: ", len(tmpD), " index :", index)
|
// }
|
|
msg.Msg.Tasklab.Sdkinfos[index].Sdkdata = res
|
|
// tmpD, tmpE = proto.Marshal(&msg.Msg)
|
// if tmpE == nil {
|
// logo.Infoln(msg.Msg.Tasklab.Sdkinfos[index].Sdktype, " after SDK DATA: ", len(tmpD), " index :", index)
|
// }
|
|
// if index == 1{
|
// logo.Infoln(msg.Msg.Tasklab.Sdkinfos[index].Sdktype, " orig SDK DATA: ", len(res), " index :", index)
|
// }
|
|
out <- msg
|
}
|
|
func byteToFloat32(bytes []byte) float32 {
|
bits := binary.LittleEndian.Uint32(bytes)
|
return math.Float32frombits(bits)
|
}
|
|
func float32ToByte(float float32) []byte {
|
bits := math.Float32bits(float)
|
bytes := make([]byte, 4)
|
binary.LittleEndian.PutUint32(bytes, bits)
|
return bytes
|
}
|
|
/////////////////////////////////////////////////////////////
|
func validRemoteMessage(msg work.MsgRS, fnName string) bool {
|
if msg.Msg.Tasklab == nil {
|
logo.Errorf("%s recieve msg nil\n", fnName)
|
return false
|
}
|
|
sdkLen := len(msg.Msg.Tasklab.Sdkinfos)
|
if sdkLen == 0 {
|
logo.Errorf("%s has no sdk info\n", fnName)
|
return false
|
}
|
|
curIndex := int(msg.Msg.Tasklab.Index)
|
if curIndex < 0 || curIndex >= sdkLen {
|
logo.Errorf("%s tasklab index %d error\n", fnName, curIndex)
|
return false
|
}
|
if msg.Msg.Tasklab.Sdkinfos[curIndex].Sdktype != fnName {
|
logo.Errorf("%s is different from %s\n", fnName, msg.Msg.Tasklab.Sdkinfos[curIndex].Sdktype)
|
return false
|
}
|
return true
|
}
|
|
func unpackImage(msg work.MsgRS, fnName string) *protomsg.Image {
|
// 解压获取传入的数据
|
bData, err := work.UnCompress(msg.Msg.Data)
|
if err != nil {
|
logo.Errorf("%s uncompress image failed\n", fnName)
|
return nil
|
}
|
// 反序列化数据得到sdk入参
|
i := &protomsg.Image{}
|
err = proto.Unmarshal(bData, i)
|
if err != nil {
|
logo.Errorf("%s protobuf decode CameraImage error : %s\n", fnName, err.Error())
|
return nil
|
}
|
if i.Data == nil {
|
logo.Errorf("%s protomsg.Image data null\n", fnName)
|
return nil
|
}
|
return i
|
}
|
|
////////////////////////////////////////////////////////////
|
func filter(tid, sid string, score, angle float32, size int) bool {
|
return true
|
data := ruleForSdk.TargetData{
|
TaskId: tid,
|
SdkId: sid,
|
Score: score * 100.0,
|
Size: size,
|
Angle: angle,
|
}
|
rules := cache.GetTaskSdkRules(data.TaskId)
|
if rules == nil {
|
return true
|
}
|
logo.Infoln("TASKSDKRULE: ", rules)
|
return ruleForSdk.Judge(data, rules)
|
}
|