zhangmeng
2019-12-17 beddc35b29caf6efe997dcbc08a9eba9742e3cd3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package sdk
 
import (
    "analysis/logo"
    "analysis/work"
    "context"
    "fmt"
    "plugin"
 
    "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
 
    list *LockList
 
    handle interface{}
    fnInit func(int, int, string, string, func(...interface{})) 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,
 
        list: NewLockList(6),
 
        handle: nil,
        fnInit: fnInit.(func(int, int, string, string, func(...interface{})) 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(2, 0, d.licSrvPath, d.modelPath, logo.Infoln)
    if h == nil {
        logo.Errorln("INIT VEHICLE SDK ERROR")
        return false
    }
    d.handle = h
 
    logo.Infoln("RUN VEHICLE SDK")
 
    return true
}
 
func (d *VehicleDetector) detect(rMsg work.MsgRS, out chan<- work.MsgRS, typ string) {
    i := unpackImage(rMsg, typ)
    if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 {
        ejectResult(nil, rMsg, out)
        return
    }
 
    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))
 
}
 
// Run impl
func (d *VehicleDetector) Run(ctx context.Context, in <-chan work.MsgRS, out chan<- work.MsgRS, typ string) {
    FlowSimple(ctx, in, out, typ, d.list.Push, d.list.Pop, d.detect, func() { d.fnFree(d.handle) })
}
 
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
}