---
panlei
2019-12-09 3ab43cffc81d02c1aba37b6df6551bf3c17dc0ef
insertdata/insertDataToEs.go
@@ -1,25 +1,28 @@
package insertdata
import (
   "basic.com/valib/logger.git"
   "encoding/base64"
   "encoding/json"
   "errors"
   "net"
   "ruleprocess/cache"
   "ruleprocess/structure"
   "strconv"
   "time"
   "basic.com/pubsub/protomsg.git"
   "github.com/golang/protobuf/proto"
   "github.com/satori/go.uuid"
   "basic.com/valib/logger.git"
   "ruleprocess/cache"
   "ruleprocess/structure"
   "basic.com/pubsub/protomsg.git"
   "basic.com/pubsub/esutil.git"
   "ruleprocess/ruleserver"
   "ruleprocess/util"
)
var weedfsUrl, videoPersonUrl, personAction string
var serverIp string
var serverPort string
type conf struct {
   PhotoUrl       string `yaml:"photoUrl"`
   VideoPersons   string `yaml:"videoPersons"`
@@ -27,43 +30,6 @@
   ServerIp       string `yaml:"serverIp"`
   ServerPort     string `yaml:"serverPort"`
   DbTablePersons string `yaml:"dbTablePersons"`
}
//func InitInsertEs() {
//   weedfsUrl = "http://"+WeedFs.Ip+":"+strconv.Itoa(WeedFs.UploadPort)+"/submit"
//   videoPersonUrl = "http://"+EsInfo.Masterip+":"+EsInfo.Httpport+"/"+EsInfo.EsIndex.VideoPersons.IndexName+"/"+EsInfo.EsIndex.VideoPersons.IndexType
//   personAction = "http://"+EsInfo.Masterip+":"+EsInfo.Httpport+"/"+EsInfo.EsIndex.Personaction.IndexName+"/"+EsInfo.EsIndex.Personaction.IndexType
//}
// 人脸的数据结构
type PerVideoPicture struct {
   Id              string                `json:"id"`
   CameraId        string                `json:"cameraId"`
   CameraAddr      string                `json:"cameraAddr"`
   CameraName      string                `json:"cameraName"`
   PicDate         string                `json:"picDate"`
   PicMaxUrl       []string              `json:"picMaxUrl"`
   TaskId          string                `json:"taskId"`
   TaskName        string                `json:"taskName"`
   SdkName         string                `json:"sdkName"`
   Content         string                `json:"content"`
   AlarmRules      []AlarmRule           `json:"alarmRules"`
   LikeDate        string                `json:"likeDate"`
   ShowLabels      string                `json:"showLabels"`
   OtherLabels     string                `json:"otherLabels"`
   VideoUrl        string                `json:"videoUrl"`
   AnalyServerId   string                `json:"analyServerId"`
   AnalyServerName string                `json:"analyServerName"`
   AnalyServerIp   string                `json:"analyServerIp"`
   ClusterId       string                `json:"clusterId"`
   IsAlarm         bool                  `json:"isAlarm"`
   IsAckAlarm      bool                  `json:"isAckAlarm"`
   IsCollect       bool                  `json:"isCollect"`
   IsDelete        bool                  `json:"isDelete"`
   BaseInfo        []*structure.BaseInfo `json:"baseInfo"`
   TargetInfo      []Target              `json:"targetInfo"`
   LinkTag         string                `json:"linkTag"`
   LinkTagInfo     []*LinkInfo           `json:"linkTagInfo"`
}
type LinkInfo struct {
@@ -94,12 +60,24 @@
   TargetInfo      []Target              `json:"targetInfo"`
}
type Target struct {
//  统一的数据结构
type PerVideoPicture struct {
   LinkInfo
   LinkTag         string                `json:"linkTag"`
   LinkTagInfo     []*LinkInfo           `json:"linkTagInfo"`
}
type SourceTarget struct {
   TargetId       string  `json:"targetId"`
   TargetScore    float64 `json:"targetScore"`
   TargetType     string  `json:"targetType"`
   Feature        string  `json:"feature"`
   PicSmUrl       string  `json:"picSmUrl"`
   TargetLocation Points  `json:"targetLocation"`
}
type Target struct {
   SourceTarget
   AttachTarget SourceTarget `json:"attachTarget"`
}
type Points struct {
@@ -137,117 +115,152 @@
      panic("配置文件不合法")
   }
   weedfsUrl = "http://" + localConfig1.WebPicIp + ":" + strconv.Itoa(int(localConfig1.WebPicPort)) + "/submit"
   videoPersonUrl = "http://" + localConfig1.AlarmIp + ":" + strconv.Itoa(int(localConfig1.AlarmPort)) + "/" + EsInfo.EsIndex.AIOcean.IndexName + "/" + EsInfo.EsIndex.AIOcean.IndexType
   InsertFace(msg)
   InsertYolo(msg)
   InsertTarget(msg)
   videoPersonUrl = "http://" + localConfig1.AlarmIp + ":" + strconv.Itoa(int(localConfig1.AlarmPort)) + "/" + EsInfo.EsIndex.AIOcean.IndexName + "/" + EsInfo.EsIndex.AIOcean.IndexType+"?refresh=true"
   serverIp = localConfig1.AlarmIp
   serverPort = strconv.Itoa(int(localConfig1.AlarmPort))
   for k,results := range msg.RuleResult {
      if results != nil && len(results.([]structure.Result)) > 0{
         switch k {
         case "yolo":
            InsertYolo(msg,k,results)
         case "face":
            InsertFace(msg,k,results)
         case "plate":
            InsertFace(msg,k,results)
         case "track":
            InsertFace(msg,k,results)
         case "target":
            InsertLastTarget(msg)
         }
      }
   }
}
// 往es中插入人脸类型数据
func InsertFace(msg structure.ResultMsg) {
   if (msg.RuleResult["face"] != nil && len(msg.RuleResult["face"].([]structure.Result)) > 0) || (msg.RuleResult["plate"] != nil && len(msg.RuleResult["plate"].([]structure.Result)) > 0) {
      for key, results := range msg.RuleResult {
         if key == "face" || key == "plate" {
            logger.Info("往ES插人脸数据(或车牌数据)")
            faces := []*ObjAndRules{}
            faces = PutFace(faces, results.([]structure.Result))
            //logger.Info("整理后的数据:",faces)
            if faces != nil {
               var imgMaxUrl []string = []string{}
               var picTime string = ""
               for _, face := range faces {
                  // 上传大图
                  // 解压缩并上传图片
                  bdata, err := util.UnCompress(msg.Data)
                  if err != nil {
                     panic("解压缩图片时出现错误")
                  }
                  alarmRules := []AlarmRule{}
                  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))
                  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.Feature = base64.StdEncoding.EncodeToString(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}}
                  var targetInfos []Target
                  targetInfos = append(targetInfos, *target)
func InsertFace(msg structure.ResultMsg,key string, results interface{}) {
      logger.Info("往es插入抓拍数据")
      faces := []*ObjAndRules{}
      faces = PutFace(faces, results.([]structure.Result))
      //logger.Info("整理后的数据:",faces)
      if faces != nil {
         logger.Warn("face不为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 = base64.StdEncoding.EncodeToString(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,base64.StdEncoding.EncodeToString(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 = "车牌识别"
                  }
                  pervideo := PerVideoPicture{
                     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)
            sdkname := ""
            id := face.Uuid
            switch key {
            case "face":
               sdkname = "人脸"
            case "plate":
               sdkname = "车牌识别"
            case "track":
               sdkname = "人员跟踪"
               if len(face.Liker) == 1{
                  id = face.Liker[0].TargetId
               }
            }
            //logger.Info("人脸目标target:",targetInfos)
                  if err != nil {
                     logger.Info("json parse error ", err)
                     return
                  }
                  resp1, err1 := EsReq("POST", videoPersonUrl, requstbody)
                  if err1 != nil {
                     logger.Error("上传ES出错!---", err1)
                  } else {
                     logger.Info("插入es返回的信息:", sdkname, "---", 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})
                  }
            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),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 {
               resp1, err1 := 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})
               }
            }
         }
      }
   }
}
// 归置人脸
@@ -292,8 +305,7 @@
}
// 往es中插入yolo数据
func InsertYolo(msg structure.ResultMsg) {
   if msg.RuleResult["yolo"] != nil && len(msg.RuleResult["yolo"].([]structure.Result)) > 0 {
func InsertYolo(msg structure.ResultMsg,key string, results interface{}) {
      // 先判断一下数据带的规则标签是否有可以插入的
      flag := false
      for _, res := range msg.RuleResult["yolo"].([]structure.Result) {
@@ -308,7 +320,7 @@
         alarmRules := []AlarmRule{}
         var targetInfos []Target
         url := []string{}
         for _, yoloResult := range msg.RuleResult["yolo"].([]structure.Result) {
         for _, yoloResult := range results.([]structure.Result) {
            if yoloResult.Others.TimeLabel == "01" || yoloResult.Others.TimeLabel == "10" {
               // 拼出sdkname
               //logger.Info("应该进来才对的")
@@ -319,7 +331,7 @@
                  linkInfo = "联动任务"
               }
               alarmRules = append(alarmRules, AlarmRule{yoloResult.RuleGroupId, alarm, yoloResult.RuleText, yoloResult.DefenceState, linkInfo})
               logger.Info("打印任务名称:", )
               //logger.Info("打印任务名称:", )
               // 上传缓存数据的图片拿到url
               if yoloResult.Others.CacheData != nil {
                  //InsertYolo(msg.RuleResult["cacheData"].(ruleserver.ResultMsg))
@@ -358,6 +370,7 @@
                     var target1 = new(Target)
                     target1.TargetId = target.Id
                     target1.TargetScore = target.Score
                     target1.TargetType = "action"
                     target1.TargetLocation = Points{TopLeft: Point{target.Location.X, target.Location.Y}, BottomRight: Point{target.Location.X + target.Location.Width, target.Location.Y + target.Location.Height}}
                     targetInfos = append(targetInfos, *target1)
                  }
@@ -407,7 +420,7 @@
         if resp["fileUrl"] != nil {
            url = append(url, resp["fileUrl"].(string))
            //esDataId := uuid.NewV4().String()
            peraction := PerVideoPicture{
            peraction := PerVideoPicture{LinkInfo{
               msg.Push.PushId,
               msg.Cid,
               msg.Push.Cam.Addr,
@@ -433,6 +446,7 @@
               false,
               nil,
               targetInfos,
            },
               linkTag,
               linkTagInfos,
            }
@@ -461,14 +475,11 @@
            }
         }
      }
   } else {
      logger.Debug("timeLabel条件都不符合!")
   }
}
// 插入目标持续时间数据
func InsertTarget(msg structure.ResultMsg) {
func InsertLastTarget(msg structure.ResultMsg) {
   if msg.RuleResult["target"] != nil && len(msg.RuleResult["target"].([]structure.Result)) > 0 {
      // 先判断一下数据带的规则标签是否有可以插入的
      logger.Info("插入定时目标信息:", len(msg.RuleResult["target"].([]structure.Result)))
@@ -558,6 +569,7 @@
                  var target1 = new(Target)
                  target1.TargetId = target.Id
                  target1.TargetScore = target.Score
                  target1.TargetType = "action"
                  target1.TargetLocation = Points{TopLeft: Point{target.Location.X, target.Location.Y}, BottomRight: Point{target.Location.X + target.Location.Width, target.Location.Y + target.Location.Height}}
                  targetInfos = append(targetInfos, *target1)
               }
@@ -568,7 +580,7 @@
                  return
               }
               //esDataId := uuid.NewV4().String()
               peraction := PerVideoPicture{
               peraction := PerVideoPicture{LinkInfo{
                  msg.Push.PushId,
                  msg.Cid,
                  msg.Push.Cam.Addr,
@@ -594,6 +606,7 @@
                  false,
                  nil,
                  targetInfos,
               },
                  linkTag,
                  linkTagInfos,
               }
@@ -749,11 +762,11 @@
   case "plate":
      // 车牌运动方向,0 unknown, 1 left, 2 right, 3 up, 4 down
      logger.Info("车辆车牌信息:", obj.Car.NColor, obj.Car.NCarColor, obj.Car.NCarLogo, obj.Car.NCarType, obj.Car.NDirection)
      lable = obj.Car.License + "/" + cache.GetDic("nColor"+strconv.Itoa(int(obj.Car.NColor))) + "车牌" + "/" +
         cache.GetDic("nCarColor"+strconv.Itoa(int(obj.Car.NCarColor))) + "车辆" + "/" + "品牌:" + cache.GetDic("nCarLogo"+strconv.Itoa(int(obj.Car.NCarLogo))) + "/" + "型号:" + cache.GetDic("nCarType"+strconv.Itoa(int(obj.Car.NCarType)))
      lableAttach = "运动方向:" + cache.GetDic("nDirection"+strconv.Itoa(int(obj.Car.NDirection))) + "/" + "车牌置信度:" + strconv.Itoa(int(obj.Car.NConfidence)) + "/" +
         "车牌亮度:" + strconv.Itoa(int(obj.Car.NBright)) + "/" + "车的亮度:" + strconv.Itoa(int(obj.Car.NCarBright)) + "/" + "识别时间:" + strconv.Itoa(int(obj.Car.NTime))
      lable = obj.Car.License + "/" + cache.GetDic("nVehicleColor1"+strconv.Itoa(int(obj.Car.NVehicleColor1))) + "车辆" + "/" +
         cache.GetDic("nColor"+strconv.Itoa(int(obj.Car.NColor))) + "车牌"
      lableAttach = "车牌置信度:" + strconv.Itoa(int(obj.Car.NConfidence)) + "/" + "车的亮度:" + strconv.Itoa(int(obj.Car.NVehicleBright)) +"/"+ "车牌类型"+cache.GetDic("nType"+strconv.Itoa(int(obj.Car.NType)))+
         "车身辅颜色:"+cache.GetDic("nVehicleColor2"+strconv.Itoa(int(obj.Car.NVehicleColor2)))+"/"+"是否识别到车牌:"+cache.GetDic("nVehicleColor1"+strconv.Itoa(int(obj.Car.NVehicleColor1)))+"/"+
         "车辆置信度"+strconv.Itoa(int(obj.Score))+"/"+"品牌:" +obj.Car.VehicleType1
   }
   return lable, lableAttach
}