zhangmeng
2019-12-13 2d25b62b60da018412ed164b6fd29470498cea17
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package sdk
 
import (
    "analysis/goconv"
    "analysis/logo"
    "analysis/work"
    "context"
    "time"
 
    "basic.com/valib/gosdk.git"
)
 
// PlateIDDetector plate id
type PlateIDDetector struct {
    config     *gosdk.CPlateIDCfg
    cfgCloud   *gosdk.CPlateIDCloudCfg
    licSrvPath string
    modelPath  string
    fn         func(*gosdk.SDKImage, *gosdk.CRECT, int, int, *work.MsgRS, chan<- work.MsgRS)
}
 
// NewPlateIDDetector plate
func NewPlateIDDetector(licSrv, model string, w, h int) *PlateIDDetector {
 
    vehicleR := true
    eparkingR := false
    cloudR := false
    // vehicle
    if vehicleR {
        // ScenceMode        int // 0-顶装,侧装,顶装路径,停车场,公安
        // ModelMode         int //0-不修改,1修改模型
        return &PlateIDDetector{
            licSrvPath: licSrv,
            modelPath:  model,
            fn:         vehicle,
        }
    }
    // eparking no model
    if eparkingR {
        cfg := gosdk.DefaultPlateIDSDKConfig()
        cfg.MaxImageWidth, cfg.MaxImageHeight = int32(w), int32(h)
        cfg.MovingImage = 0
        cfg.MinHang, cfg.ChangNei = 0, 0
 
        return &PlateIDDetector{
            config:     cfg,
            cfgCloud:   nil,
            licSrvPath: licSrv,
            modelPath:  "",
            fn:         eparking,
        }
 
    }
 
    // cloud
    if cloudR {
        cfg := gosdk.DefaultPlateIDCloudSDKConfig()
 
        return &PlateIDDetector{
            config:     nil,
            cfgCloud:   cfg,
            licSrvPath: licSrv,
            modelPath:  model,
            fn:         cloud,
        }
    }
    return nil
}
 
// Init impl
func (d *PlateIDDetector) Init() bool {
    if d.config != nil {
        gosdk.InitPlateIDDetector(d.config, []byte(d.licSrvPath))
        logo.Infoln("RUN PLATE ID")
    } else if d.cfgCloud != nil {
        ret := gosdk.InitPlateIDCloudSDKDetector(d.cfgCloud, []byte(d.licSrvPath), []byte(d.modelPath))
        if ret != 0 {
            logo.Errorf("INIT PLATE ID CLOUD SDK ERROR: %d\n", ret)
            return false
        }
        logo.Infoln("RUN CLOUD PLATE ID")
    } else {
        ret := gosdk.InitVehicleITSDetector(2, 0, []byte(d.licSrvPath), []byte(d.modelPath))
        if ret != 0 {
            logo.Errorf("INIT PLATE ID VEHICLE SDK ERROR: %d\n", ret)
            return false
        }
        logo.Infoln("RUN VEHICLE PLATE ID")
    }
    return true
}
 
// Run impl
func (d *PlateIDDetector) Run(ctx context.Context, in <-chan work.MsgRS, out chan<- work.MsgRS, typ string) {
    tm := time.Now()
    sc := 0
 
    for {
        select {
        case <-ctx.Done():
            goconv.Free()
            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)
            bgrData := goconv.YUV2BGR(i.Data, imgW, imgH)
            if bgrData == nil {
                ejectResult(nil, rMsg, out)
                continue
            }
            img := gosdk.SDKImage{Data: bgrData, Width: imgW, Height: imgH}
 
            rcDetect := gosdk.CRECT{
                Left:   0,
                Top:    0,
                Right:  (int32)(imgW),
                Bottom: (int32)(imgH),
            }
 
            mW, mH := 4096, 2160
            if d.config != nil {
                mW, mH = int(d.config.MaxImageWidth), int(d.config.MaxImageHeight)
            }
            d.fn(&img, &rcDetect, mW, mH, &rMsg, out)
 
            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 eparking(img *gosdk.SDKImage, rc *gosdk.CRECT, mW, mH int, rMsg *work.MsgRS, out chan<- work.MsgRS) {
 
    // imgW, imgH := int(img.Width), int(img.Height)
 
    // fx, fy := 1.0, 1.0
 
    // if imgW > mW || imgH > mH {
    //     fx, fy = (float64)(mW)/(float64)(imgW), (float64)(mH)/(float64)(imgH)
    //     img.Data = goconv.ResizeBGR(img.Data, imgW, imgH, mW, mH)
    //     if img.Data == nil {
    //         ejectResult(nil, *rMsg, out)
    //         return
    //     }
    //     imgW, imgH = mW, mH
    // }
 
    // rImg := gosdk.SDKImage{Data: img.Data, Width: imgW, Height: imgH}
    // rRc := &gosdk.CRECT{
    //     Left:   0,
    //     Top:    0,
    //     Right:  int32(imgW),
    //     Bottom: int32(imgH),
    // }
    // result := gosdk.PlateIDDetect(rImg, rRc)
    // count := len(result)
    // if count <= 0 {
    //     ejectResult(nil, *rMsg, out)
    //     return
    // }
 
    // plateids := convert2ProtoPlateIDResult(result, fx, fy)
 
    // plateresult := protomsg.PlateIDResult{Result: plateids}
    // data, err := proto.Marshal(&plateresult)
    // if err != nil {
    //     logo.Errorln("EPARKING PLATE ID DETECTOR MARSHAL PROTO PLATE IDS ERROR", err)
    //     data = nil
    // }
    // ejectResult(data, *rMsg, out)
}
 
// func convert2ProtoPlateIDResult(obj []gosdk.CPlateIDResult, fx, fy float64) []*protomsg.PlateID {
// ret := []*protomsg.PlateID{}
 
// for _, v := range obj {
 
//     rcPlateID := &protomsg.Rect{
//         Left:   (int32)((float64)(v.RcLocation.Left) / fx),
//         Right:  (int32)((float64)(v.RcLocation.Right) / fx),
//         Top:    (int32)((float64)(v.RcLocation.Top) / fy),
//         Bottom: (int32)((float64)(v.RcLocation.Bottom) / fy),
//     }
//     rcLogo := &protomsg.Rect{
//         Left:   (int32)((float64)(v.RcLogoLocation.Left) / fx),
//         Right:  (int32)((float64)(v.RcLogoLocation.Right) / fx),
//         Top:    (int32)((float64)(v.RcLogoLocation.Top) / fy),
//         Bottom: (int32)((float64)(v.RcLogoLocation.Bottom) / fy),
//     }
//     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.PlateID{
//         License:             lic,
//         Color:               string(v.Color[:]),
//         NColor:              v.NColor,
//         NType:               v.NType,
//         NConfidence:         v.NConfidence,
//         NBright:             v.NBright,
//         NDirection:          v.NDirection,
//         RcLocation:          rcPlateID,
//         NTime:               v.NTime,
//         NCarBright:          int32(v.NCarBright),
//         NCarColor:           int32(v.NCarColor),
//         NCarLogo:            int32(v.NCarLogo),
//         NCarType:            int32(v.NCarType),
//         PlateBin:            v.PbyPlateBin[:],
//         NBinPlateWidth:      v.NBinPlateWidth[:],
//         NBinPlateHeight:     v.NBinPlateHeight[:],
//         RcLogoLocation:      rcLogo,
//         NCarModel:           v.NCarModel[:],
//         NCarModelConfidence: v.NCarModelConfidence[:],
//     }
 
//     ret = append(ret, obj)
// }
// return ret
// }