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" "ruleprocess/ruleserver" "ruleprocess/util" ) var weedfsUrl, videoPersonUrl, personAction string type conf struct { PhotoUrl string `yaml:"photoUrl"` VideoPersons string `yaml:"videoPersons"` PersonAction string `yaml:"personAction"` 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"` 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"` Sex string `json:"sex"` Age int32 `json:"age"` AgeDescription string `json:"ageDescription"` Race string `json:"race"` SmileLevel int32 `json:"smileLevel"` BeautyLevel int32 `json:"beautyLevel"` FaceFeature string `json:"faceFeature"` PicSmUrl []string `json:"picSmUrl"` VideoUrl string `json:"videoUrl"` AnalyServerId string `json:"analyServerId"` AnalyServerName string `json:"analyServerName"` AnalyServerIp string `json:"analyServerIp"` ClusterId string `json:"clusterId"` LinkId string `json:"linkId"` DetectScore float64 `json:"detectScore"` IsAlarm int `json:"isAlarm"` IsAckAlarm int `json:"isAckAlarm"` IsCollect int `json:"isCollect"` IsDelete int `json:"isDelete"` BaseInfo []*structure.BaseInfo `json:"baseInfo"` TargetInfo Target } type Target struct { TargetId string `json:"targetId"` TargetScore float64 `json:"targetScore"` TargetLocation Points } type Points struct { TopLeft Point `json:"topLeft"` BottomRight Point `json:"bottomRight"` } type Point struct { Lat float64 `json:"lat"` Lon float64 `json:"lon"` } // yolo行为的数据结构 type Personaction struct { Id string `json:"id"` CameraId string `json:"cameraId"` CameraName string `json:"cameraName"` CameraAddr string `json:"cameraAddr"` TaskId string `json:"taskId"` TaskName string `json:"taskName"` SdkName string `json:"sdkName"` Content string `json:"content"` AlarmRules []AlarmRule `json:"alarmRules"` AnalyServerId string `json:"analyServerId"` AnalyServerName string `json:"analyServerName"` AnalyServerIp string `json:"analyServerIp"` ClusterId string `json:"clusterId"` PicSmUrl []string `json:"picSmUrl"` PicDate string `json:"picDate"` LinkId string `json:"linkId"` VideoUrl string `json:"videoUrl"` IsAlarm int `json:"isAlarm"` IsAckAlarm int `json:"isAckAlarm"` IsCollect int `json:"isCollect"` IsDelete int `json:"isDelete"` TargetInfo []Target } type AlarmRule struct { GroupId string `json:"groupId"` AlarmLevel string `json:"alarmLevel"` RuleText string `json:"ruleText"` DefenceState bool `json:"defenceState"` IsLink bool `json:"isLink"` LinkInfo string `json:"linkInfo"` } // 一个face对多个规则组的归置人脸的结构体 type FaceAndRules struct { structure.Arg rules []structure.Result } // 往ES插数据 //func InsertToEs(msg ruleserver.ResultMsg) { // var timeLabel string // // 直接从规则的标签数据里拿符合规则的人脸结果 // if msg.RuleResult["timeLabel"] != nil { // timeLabel = msg.RuleResult["timeLabel"].(string) // } // logger.Debug("插入数据前看看报警标志位:", timeLabel) // if timeLabel == "01" { // 无定时器状态要插入的报警数据 // InsertFace(msg) // flag := ruleserver.BodyIsSame(msg.SdkMessage) // if !flag { // InsertYolo(msg) // } // } // if timeLabel == "10" { // 定时器状态要插入的首帧报警数据。连带着定时器开启时的那帧 // InsertFace(msg) // InsertYolo(msg) // } // //if timeLabel == "12" { // 并非报警数据,只是状态改变的数据 // // //ChangeStatusFace(msg) // // ChangeStatusYolo(msg) // //} //} func InsertToEs(msg structure.ResultMsg) { defer func() { if err := recover(); err != nil { logger.Error("es模块儿的异常捕获:", err) } }() localConfig1, err := cache.GetServerInfo() if err != nil { panic("配置文件不合法") } weedfsUrl = "http://" + localConfig1.WebPicIp + ":" + strconv.Itoa(int(localConfig1.WebPicPort)) + "/submit" videoPersonUrl = "http://" + localConfig1.AlarmIp + ":" + strconv.Itoa(int(localConfig1.AlarmPort)) + "/" + EsInfo.EsIndex.VideoPersons.IndexName + "/" + EsInfo.EsIndex.VideoPersons.IndexType personAction = "http://" + localConfig1.AlarmIp + ":" + strconv.Itoa(int(localConfig1.AlarmPort)) + "/" + EsInfo.EsIndex.Personaction.IndexName + "/" + EsInfo.EsIndex.Personaction.IndexType InsertFace(msg, "") InsertYolo(msg, "") } // 往es中插入人脸数据 func InsertFace(msg structure.ResultMsg, linkId string) { if msg.RuleResult["face"] != nil && len(msg.RuleResult["face"].([]structure.FaceResult)) > 0 { logger.Info("往ES插人脸数据") faces := []*FaceAndRules{} faces = PutFace(faces, msg) //logger.Info("整理后的数据:",faces) if faces != nil { var imgMaxUrl string = "" var picTime string = "" for _, face := range faces { // 上传大图 // 解压缩并上传图片 bdata, err := util.UnCompress(msg.Data) if err != nil { panic("解压缩图片时出现错误") } alarmRules := []AlarmRule{} logger.Warn("人脸id为:", face.Id, "人脸的规则长度为:", len(face.rules)) //os.Exit(1) for _, faceResult := range face.rules { alarm := ChangeToString(faceResult.DefenceState, faceResult.AlarmLevel) alarmRules = append(alarmRules, AlarmRule{faceResult.RuleGroupId, alarm, faceResult.RuleText, faceResult.DefenceState, faceResult.IsLink, ""}) } i := protomsg.Image{} err = proto.Unmarshal(bdata, &i) if imgMaxUrl == "" { bigPhotoUrl := make(map[string]interface{}) bigPhotoUrl, err = util.DrawPolygonOnImageForFace(msg.Cid, i, msg.RuleResult["face"].([]structure.FaceResult), weedfsUrl) logger.Debug("========大图路径:", bigPhotoUrl) imgMaxUrl = bigPhotoUrl["fileUrl"].(string) picTime = i.Timestamp } // 人脸检测,没有相似的底库人员 localConfig, err := cache.GetServerInfo() if err != nil { logger.Error("查询本机信息失败!") } serverIp, err := GetLocalIP() // 查询cameraName camera, err := cache.GetCameraById(msg.Cid) if err != nil { logger.Error("查询摄像机信息失败") } 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("上传小图出错") } //logger.Info("================小图地址:", resp["fileUrl"].(string)) sex := "" logger.Info(sex) if face.ThftRes.Gender == 1 { sex = "男" } else { sex = "女" } race := getRaceString(face.ThftRes.Race) ageDescription := getDescription(face.ThftRes.Age) logger.Info(ageDescription) esDataId := uuid.NewV4().String() linksId := "" if linkId != "" { linksId = linkId } var target = new(Target) target.TargetId = strconv.FormatUint(face.Id, 10) target.TargetScore = face.Score 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}} //logger.Info("人脸的id:",strconv.FormatUint(face.Id, 10)) logger.Info("%%%%%%%%%%%%%%%%%%%%%目标信息:",*target) pervideo := PerVideoPicture{ esDataId, msg.Cid, camera.Addr, picTime, imgMaxUrl, msg.Tasklab.Taskid, msg.Tasklab.Taskname, "人脸", "", alarmRules, time.Now().Format("2006-01-02 15:04:05"), // 只检测,没有比对时间 sex, //暂改为人脸id strconv.FormatUint(face.Id, 10) face.ThftRes.Age, ageDescription, // 暂改为分值fmt.Sprintf("%.2f",face.Score) race, face.ThftRes.Smile, face.ThftRes.Beauty, base64.StdEncoding.EncodeToString(face.Feature), []string{resp["fileUrl"].(string)}, "暂无集群", localConfig.ServerId, localConfig.ServerName, serverIp, "", linksId, face.Score, 1, 0, 0, 0, face.Liker, *target, } requstbody, err := json.Marshal(pervideo) 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返回的信息:", resp1) // 发出录像信号 ruleserver.AddLxMessage(&protomsg.VideotapeInfo{EsDataId: esDataId, CameraId: msg.Cid, TaskId: msg.Tasklab.Taskid, ImgId: i.Id, SdkIds: []string{}, Type: 1}) } } } } } // 归置人脸 func PutFace(faces []*FaceAndRules, msg structure.ResultMsg) []*FaceAndRules { if msg.RuleResult["face"] != nil && len(msg.RuleResult["face"].([]structure.FaceResult)) > 0 { for _, faceResult := range msg.RuleResult["face"].([]structure.FaceResult) { faces = hebingFace(faces, faceResult) } return faces } else { return nil } } func hebingFace(faces []*FaceAndRules, faceResult structure.FaceResult) []*FaceAndRules { for _, arg := range faceResult.Args { // 拿到每一张人脸 logger.Info("归置人脸时相似者的数量:", len(arg.Liker)) flag := false for _, face := range faces { for _, lik := range face.Liker { logger.Warn("--------合并人脸时相似者:", lik.PersonId, lik.TableName) } if arg.Id == face.Id { flag = true face.rules = append(face.rules, faceResult.Result) // 相似者去重归并 for _, liker := range arg.Liker { flag1 := true for _, liker1 := range face.Liker { if liker.PersonId == liker1.PersonId { flag1 = false } } if flag1 { face.Liker = append(face.Liker, liker) } } //face.Liker = append(face.Liker,arg.Liker...) } } if !flag { faces = append(faces, &FaceAndRules{arg, []structure.Result{faceResult.Result}}) } } return faces } // 往es中插入yolo数据 func InsertYolo(msg structure.ResultMsg, linkId string) { if msg.RuleResult["yolo"] != nil && len(msg.RuleResult["yolo"].([]structure.Result)) > 0 { // 先判断一下数据带的规则标签是否有可以插入的 flag := false for _, res := range msg.RuleResult["yolo"].([]structure.Result) { //logger.Info("定时器打的数字标签:",res.Others.TimeLabel) if res.Others.TimeLabel == "01" || res.Others.TimeLabel == "10" { flag = true } } if flag { logger.Info("往ES插yolo数据") var sdkNames string = "" alarmRules := []AlarmRule{} var targetInfos []Target url := []string{} for _, yoloResult := range msg.RuleResult["yolo"].([]structure.Result) { if yoloResult.Others.TimeLabel == "01" || yoloResult.Others.TimeLabel == "10" { // 拼出sdkname //logger.Info("应该进来才对的") sdkNames = sdkNames + yoloResult.SdkName alarm := ChangeToString(yoloResult.DefenceState, yoloResult.AlarmLevel) linkInfo := "" if yoloResult.IsLink { linkInfo = "联动任务" } alarmRules = append(alarmRules, AlarmRule{yoloResult.RuleGroupId, alarm, yoloResult.RuleText, yoloResult.DefenceState, yoloResult.IsLink, linkInfo}) // 上传缓存数据的图片拿到url if yoloResult.Others.CacheData != nil { //InsertYolo(msg.RuleResult["cacheData"].(ruleserver.ResultMsg)) // 把缓存的数据上传后得到地址存进去 // 解压缩并上传图片 msgs := yoloResult.Others.CacheData for _, msg1 := range msgs { bdata, err := util.UnCompress(msg1.Data) if err != nil { panic("解压缩图片时出现错误") } i := protomsg.Image{} err = proto.Unmarshal(bdata, &i) resp1, err1 := util.DrawPolygonOnImageForYolo(msg1.Cid, i, msg1.RuleResult["yolo"].([]structure.Result), weedfsUrl) if err1 != nil { logger.Error("缓存数据画框或上传图片服务器出错", err) } else { logger.Info("上传的图片信息:", resp1) } if resp1["fileUrl"] != nil { url = append(url, resp1["fileUrl"].(string)) } } } // 装配目标信息数据 for _,target := range yoloResult.Location { // 去重添加 logger.Info("装配前的原始数据是:",target) var flag = true for _,selectTarget := range targetInfos { if strconv.FormatUint(target.TargetId, 10) == selectTarget.TargetId { flag = false break } } if flag { var target1 = new(Target) target1.TargetId = strconv.FormatUint(target.TargetId, 10) target1.TargetScore = target.TargetScore target1.TargetLocation = Points{TopLeft:Point{target.X,target.Y},BottomRight:Point{target.X+target.Width,target.Y+target.Height}} targetInfos = append(targetInfos,*target1) } } } } linkFlag := false for _, yoloResult := range msg.RuleResult["yolo"].([]structure.Result) { if (yoloResult.Others.TimeLabel == "01" || yoloResult.Others.TimeLabel == "10") && yoloResult.Others.LinkCache != nil && len(yoloResult.Others.LinkCache) > 1 { linkId := uuid.NewV4().String() for _, msg2 := range yoloResult.Others.LinkCache { logger.Warn("插入联动数据", "此帧数据的id为", msg2.Cid) InsertYolo(msg2, linkId) linkFlag = true } } } if linkFlag { // 联动数据中包含本帧数据,插入联动数据后就不需要插入本帧数据了 logger.Warn("成功插入两个联动图片") //os.Exit(1) return } //logger.Info("--------走到这儿就不一样") isAlarm := 0 resp := make(map[string]interface{}) // 解压缩并上传图片 bdata, err := util.UnCompress(msg.Data) if err != nil { panic("解压缩图片时出现错误") } i := protomsg.Image{} err = proto.Unmarshal(bdata, &i) if len(alarmRules) > 0 { isAlarm = 1 //resp, err = util.PostFormBufferData(weedfsUrl, i, uuid.NewV4().String()) resp, err = util.DrawPolygonOnImageForYolo(msg.Cid, i, msg.RuleResult["yolo"].([]structure.Result), weedfsUrl) if err != nil { logger.Error("画框或上传图片服务器出错", err) return } else { logger.Info("上传的图片信息:", resp) } } else { isAlarm = 0 // 不是报警数据不存 return } // logger.Println("图片上传返回值:", resp) // 查询本机信息 localConfig, err := cache.GetServerInfo() if err != nil { logger.Error("查询本机信息失败!") } // 查询cameraName camera, err := cache.GetCameraById(msg.Cid) if err != nil { logger.Error("查询摄像机信息失败") } logger.Info("摄像机信息:",camera) serverIp, err := GetLocalIP() logger.Info("%%%%%%%%%%%%%%%%%yolo的target信息:",targetInfos) if resp["fileUrl"] != nil { url = append(url, resp["fileUrl"].(string)) esDataId := uuid.NewV4().String() linksId := "" if linkId != "" { linksId = linkId } peraction := Personaction{ esDataId, msg.Cid, camera.Name, camera.Addr, msg.Tasklab.Taskid, msg.Tasklab.Taskname, sdkNames, "", alarmRules, localConfig.ServerId, localConfig.ServerName, serverIp, "", url, i.Timestamp, linksId, "", isAlarm, 0, 0, 0, targetInfos, } requstbody, err := json.Marshal(peraction) if err != nil { logger.Info("json parse error ", err) return } resp1, err2 := EsReq("POST", personAction, requstbody) if err2 != nil { logger.Error("往ES插入数据失败", err) } else { logger.Debug("插入es返回的数据信息是:", resp1) // 发出录像信号 ruleserver.AddLxMessage(&protomsg.VideotapeInfo{EsDataId: esDataId, CameraId: msg.Cid, TaskId: msg.Tasklab.Taskid, ImgId: i.Id, SdkIds: []string{}, Type: 2}) logger.Warn("__________________________________________往ES插入yolo数据成功") //os.Exit(1) } } } else { logger.Debug("timeLabel条件都不符合!") } } } // 获取本机ip func GetLocalIP() (ipv4 string, err error) { var ( addrs []net.Addr addr net.Addr ipNet *net.IPNet // IP地址 isIpNet bool ) // 获取所有网卡 if addrs, err = net.InterfaceAddrs(); err != nil { return } // 取第一个非lo的网卡IP for _, addr = range addrs { // 这个网络地址是IP地址: ipv4, ipv6 if ipNet, isIpNet = addr.(*net.IPNet); isIpNet && !ipNet.IP.IsLoopback() { // 跳过IPV6 if ipNet.IP.To4() != nil { ipv4 = ipNet.IP.String() // 192.168.1.1 return } } } err = errors.New("ipv4 not found") return } // 把报警等级转化成汉字 func ChangeToString(defenceState bool, i int32) string { alarm := "" if defenceState { if i == 1 { alarm = "一级" } if i == 2 { alarm = "二级" } if i == 3 { alarm = "三级" } if i == 4 { alarm = "四级" } if i == 5 { alarm = "五级" } } else { alarm = "撤防" } return alarm } //获取年龄描述 func getDescription(age int32) string { ageInfo := "青年" if age > 0 && age < 7 { ageInfo = "童年" } else if age >= 7 && age < 18 { ageInfo = "少年" } else if age >= 18 && age < 40 { ageInfo = "青年" } else if age >= 40 && age < 65 { ageInfo = "中年" } else if age >= 65 { ageInfo = "老年" } return ageInfo } func getRaceString(i int32) string { race := "" if i == 1 { race = "白人" } else if i == 2 { race = "黄人" } else { race = "黑人" } return race }