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 // }