---
panlei
2019-12-17 3ff1af612ee690063dceb5aa9a6e9791e6d9a943
algorithm/middleware/insertDataToEs.go
@@ -133,125 +133,159 @@
      faces = PutFace(faces, results.([]structure.Result))
      //logger.Info("整理后的数据:",faces)
      if faces != nil {
         logger.Warn("face不为nil")
         logger.Warn("目标不为nil")
         var imgMaxUrl []string = []string{}
         var picTime string = ""
         for _, face := range faces {
            // 上传大图
            // 解压缩并上传图片
            bdata, err := util.UnCompress(msg.Data)
            if err != nil {
               panic("解压缩图片时出现错误")
            }
            alarmRules := []AlarmRule{}
            //logger.Info("循环每一个目标")
            for _, faceResult := range face.rules {
               alarm := ChangeToString(faceResult.DefenceState, faceResult.AlarmLevel)
               alarmRules = append(alarmRules, AlarmRule{faceResult.RuleGroupId, alarm, faceResult.RuleText, faceResult.DefenceState, ""})
            }
            i := protomsg.Image{}
            err = proto.Unmarshal(bdata, &i)
            // 先传小图,再传大图,防止脸上有线
            bytes := util.SubImg(i, int(face.Location.X), int(face.Location.Y), int(face.Location.X+face.Location.Width), int(face.Location.Y+face.Location.Height),face.Type)
            resp, err := util.PostFormBufferData1(weedfsUrl, bytes, uuid.NewV4().String())
            if err != nil {
               logger.Error("上传小图出错")
            }
            // 上传大图
            if len(imgMaxUrl) == 0 {
               bigPhotoUrl := make(map[string]interface{})
               bigPhotoUrl, err = util.DrawPolygonOnImageForFace(msg.Cid, i, msg.RuleResult["face"].([]structure.Result), weedfsUrl)
               logger.Debug("========大图路径:", bigPhotoUrl)
               imgMaxUrl = append(imgMaxUrl, bigPhotoUrl["fileUrl"].(string))
               picTime = i.Timestamp
            }
            lable, lableAttach := Feature2Jsonstr(*face)
            var target = new(Target)
            target.TargetId = face.Id
            target.TargetScore = face.Score
            target.TargetType = face.Type
            target.Feature = face.Feature
            target.PicSmUrl = resp["fileUrl"].(string)
            target.TargetLocation = Points{TopLeft: Point{face.Location.X, face.Location.Y}, BottomRight: Point{face.Location.X + face.Location.Width, face.Location.Y + face.Location.Height}}
            target.AttachTarget = SourceTarget{face.AttachArg.Id,face.AttachArg.Score,face.AttachArg.Type,face.AttachArg.Feature,"",Points{TopLeft: Point{face.AttachArg.Location.X, face.AttachArg.Location.Y}, BottomRight: Point{face.AttachArg.Location.X + face.AttachArg.Location.Width, face.AttachArg.Location.Y + face.AttachArg.Location.Height}}}
            var targetInfos []Target
            targetInfos = append(targetInfos, *target)
            sdkname := ""
            id := face.Uuid
            switch key {
            case "face":
               sdkname = "人脸"
            case "plate":
               sdkname = "车牌识别"
            case "track":
               sdkname = "跟踪"
               logger.Info("数据到了此处")
               if len(face.Liker) == 1{
                  id = face.Liker[0].TargetId
               }
            }
            //logger.Info("人脸目标target:",targetInfos)
            pervideo := PerVideoPicture{LinkInfo{
               id,
               msg.Cid,
               msg.Push.Cam.Addr,
               msg.Push.Cam.Name,
               picTime,
               imgMaxUrl,
               msg.Tasklab.Taskid,
               msg.Tasklab.Taskname,
               sdkname,
               "",
               alarmRules,
               time.Now().Format("2006-01-02 15:04:05"), // 只检测,没有比对时间
               lable,
               lableAttach,
               "",
               msg.Push.ServerId,
               msg.Push.ServerName,
               msg.Push.LocalIp,
               "",
               true,
               false,
               false,
               false,
               face.Liker,
               targetInfos,
            },
               "",
               []*LinkInfo{},
            }
            requstbody, err := json.Marshal(pervideo)
            if err != nil {
               logger.Info("json parse error ", err)
               return
            }
            if key == "track" && len(face.Liker) == 1{
               esid := face.Liker[0].TargetId
               returnMsg, err1 := esutil.AppendTargetInfo(esid, string(requstbody),structure.EsInfo.EsIndex.AIOcean.IndexName,serverIp,serverPort)
               if err1 != nil {
                  logger.Error("追加数据出错!---", err1)
               } else {
                  logger.Info("插入es返回的信息:", returnMsg)
                  // 发出录像信号
                  ruleserver.AddLxMessage(&protomsg.VideotapeInfo{EsDataId: face.Uuid, CameraId: msg.Cid, TaskId: msg.Tasklab.Taskid, VideoUrl: msg.Push.VideoUrl, ImgId: i.Id, SdkIds: []string{"-1"}, Type: 1})
               }
            if face.Uuid == "" {
               // 比到了相似的人但在此摄像机下已经出现过,最后不能插入数据库
               logger.Info("冗余数据,并不入库")
            } else {
               resp1, err1 := insertdata.EsReq("POST", videoPersonUrl, requstbody)
               if err1 != nil {
                  logger.Error("上传ES出错!---", err1)
               // 上传大图
               // 解压缩并上传图片
               bdata, err := util.UnCompress(msg.Data)
               if err != nil {
                  panic("解压缩图片时出现错误")
               }
               alarmRules := []AlarmRule{}
               //logger.Info("循环每一个目标")
               for _, faceResult := range face.rules {
                  alarm := ChangeToString(faceResult.DefenceState, faceResult.AlarmLevel)
                  alarmRules = append(alarmRules, AlarmRule{faceResult.RuleGroupId, alarm, faceResult.RuleText, faceResult.DefenceState, ""})
               }
               i := protomsg.Image{}
               err = proto.Unmarshal(bdata, &i)
               // 先传小图,再传大图,防止脸上有线
               bytes := util.SubImg(i, int(face.Location.X), int(face.Location.Y), int(face.Location.X+face.Location.Width), int(face.Location.Y+face.Location.Height),face.Type)
               resp, err := util.PostFormBufferData1(weedfsUrl, bytes, uuid.NewV4().String())
               if err != nil {
                  logger.Error("上传小图出错")
               }
               // 上传大图
               if len(imgMaxUrl) == 0 {
                  bigPhotoUrl := make(map[string]interface{})
                  bigPhotoUrl, err = util.DrawPolygonOnImageForFace(msg.Cid, i, msg.RuleResult["face"].([]structure.Result), weedfsUrl)
                  logger.Debug("========大图路径:", bigPhotoUrl)
                  imgMaxUrl = append(imgMaxUrl, bigPhotoUrl["fileUrl"].(string))
                  picTime = i.Timestamp
               }
               lable, lableAttach := Feature2Jsonstr(*face)
               var target = new(Target)
               target.TargetId = face.Id
               target.TargetScore = face.Score
               target.TargetType = face.Type
               target.Feature = face.Feature
               target.PicSmUrl = resp["fileUrl"].(string)
               target.TargetLocation = Points{TopLeft: Point{face.Location.X, face.Location.Y}, BottomRight: Point{face.Location.X + face.Location.Width, face.Location.Y + face.Location.Height}}
               target.AttachTarget = SourceTarget{face.AttachArg.Id,face.AttachArg.Score,face.AttachArg.Type,face.AttachArg.Feature,"",Points{TopLeft: Point{face.AttachArg.Location.X, face.AttachArg.Location.Y}, BottomRight: Point{face.AttachArg.Location.X + face.AttachArg.Location.Width, face.AttachArg.Location.Y + face.AttachArg.Location.Height}}}
               var targetInfos []Target
               targetInfos = append(targetInfos, *target)
               sdkname := ""
               switch key {
               case "face":
                  sdkname = "人脸"
               case "plate":
                  sdkname = "车牌识别"
               case "track":
                  sdkname = "跟踪"
               }
               //logger.Info("人脸目标target:",targetInfos)
               if key == "track" && len(face.Liker) == 1{
                  logger.Info("确认过眼神是要追加的数据")
                  pervideo1 := LinkInfo{
                     face.Liker[0].TargetId,
                     msg.Cid,
                     msg.Push.Cam.Addr,
                     msg.Push.Cam.Name,
                     picTime,
                     imgMaxUrl,
                     msg.Tasklab.Taskid,
                     msg.Tasklab.Taskname,
                     sdkname,
                     "",
                     alarmRules,
                     time.Now().Format("2006-01-02 15:04:05"), // 只检测,没有比对时间
                     lable,
                     lableAttach,
                     "",
                     msg.Push.ServerId,
                     msg.Push.ServerName,
                     msg.Push.LocalIp,
                     "",
                     true,
                     false,
                     false,
                     false,
                     face.Liker,
                     targetInfos,
                  }
                  requstbody, err := json.Marshal(pervideo1)
                  if err != nil {
                     logger.Info("json parse error ", err)
                     return
                  }
                  esid := face.Liker[0].TargetId
                  logger.Info("liker信息:",*face.Liker[0])
                  returnMsg, err1 := esutil.AppendTargetInfo(esid, string(requstbody),structure.EsInfo.EsIndex.AIOcean.IndexName,serverIp,serverPort)
                  if err1 != nil {
                     logger.Error("追加数据出错!---", err1)
                  } else {
                     logger.Info("插入es返回的信息:", returnMsg)
                     // 发出录像信号
                     ruleserver.AddLxMessage(&protomsg.VideotapeInfo{EsDataId: face.Uuid, CameraId: msg.Cid, TaskId: msg.Tasklab.Taskid, VideoUrl: msg.Push.VideoUrl, ImgId: i.Id, SdkIds: []string{"-1"}, Type: 1})
                  }
               } else {
                  logger.Info("插入es返回的信息:", resp1)
                  // 发出录像信号
                  ruleserver.AddLxMessage(&protomsg.VideotapeInfo{EsDataId: face.Uuid, CameraId: msg.Cid, TaskId: msg.Tasklab.Taskid, VideoUrl: msg.Push.VideoUrl, ImgId: i.Id, SdkIds: []string{"-1"}, Type: 1})
                  logger.Info("新增数据")
                  pervideo := PerVideoPicture{LinkInfo{
                     face.Uuid,
                     msg.Cid,
                     msg.Push.Cam.Addr,
                     msg.Push.Cam.Name,
                     picTime,
                     imgMaxUrl,
                     msg.Tasklab.Taskid,
                     msg.Tasklab.Taskname,
                     sdkname,
                     "",
                     alarmRules,
                     time.Now().Format("2006-01-02 15:04:05"), // 只检测,没有比对时间
                     lable,
                     lableAttach,
                     "",
                     msg.Push.ServerId,
                     msg.Push.ServerName,
                     msg.Push.LocalIp,
                     "",
                     true,
                     false,
                     false,
                     false,
                     face.Liker,
                     targetInfos,
                  },
                     "",
                     []*LinkInfo{},
                  }
                  requstbody, err := json.Marshal(pervideo)
                  if err != nil {
                     logger.Info("json parse error ", err)
                     return
                  }
                  resp1, err1 := insertdata.EsReq("POST", videoPersonUrl, requstbody)
                  if err1 != nil {
                     logger.Error("上传ES出错!---", err1)
                  } else {
                     logger.Info("插入es返回的信息:", resp1)
                     // 发出录像信号
                     ruleserver.AddLxMessage(&protomsg.VideotapeInfo{EsDataId: face.Uuid, CameraId: msg.Cid, TaskId: msg.Tasklab.Taskid, VideoUrl: msg.Push.VideoUrl, ImgId: i.Id, SdkIds: []string{"-1"}, Type: 1})
                  }
               }
            }
         }
      }
}
// 归置人脸