| | |
| | | package insertdata |
| | | |
| | | import ( |
| | | "basic.com/valib/logger.git" |
| | | "encoding/base64" |
| | | "encoding/json" |
| | | "errors" |
| | | "fmt" |
| | | "net" |
| | | "ruleprocess/cache" |
| | | "ruleprocess/logger" |
| | | "ruleprocess/structure" |
| | | "strconv" |
| | | "time" |
| | | |
| | |
| | | DbTablePersons string `yaml:"dbTablePersons"` |
| | | } |
| | | |
| | | func init() { |
| | | 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 |
| | | } |
| | | //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 []*ruleserver.BaseInfo `json:"baseInfo"` |
| | | 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"` |
| | |
| | | IsAckAlarm int `json:"isAckAlarm"` |
| | | IsCollect int `json:"isCollect"` |
| | | IsDelete int `json:"isDelete"` |
| | | TargetInfo []Target |
| | | } |
| | | |
| | | type AlarmRule struct { |
| | |
| | | AlarmLevel string `json:"alarmLevel"` |
| | | RuleText string `json:"ruleText"` |
| | | DefenceState bool `json:"defenceState"` |
| | | IsLink bool `json:"isLink"` |
| | | LinkInfo string `json:"linkInfo"` |
| | | IsLink bool `json:"isLink"` |
| | | LinkInfo string `json:"linkInfo"` |
| | | } |
| | | |
| | | // 一个face对多个规则组的归置人脸的结构体 |
| | | type FaceAndRules struct { |
| | | ruleserver.Arg |
| | | rules []ruleserver.Result |
| | | structure.Arg |
| | | rules []structure.Result |
| | | } |
| | | |
| | | // 往ES插数据 |
| | |
| | | // // ChangeStatusYolo(msg) |
| | | // //} |
| | | //} |
| | | func InsertToEs(msg ruleserver.ResultMsg) { |
| | | 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, "") |
| | | // 如果标签中含有持续时间首次报警的timeLabel的话则不需要过人体追踪,不然就没的插入了 |
| | | fk := ruleserver.TrackOrNot(msg.RuleResult) |
| | |
| | | } |
| | | |
| | | // 往es中插入人脸数据 |
| | | func InsertFace(msg ruleserver.ResultMsg, linkId string) { |
| | | if msg.RuleResult["face"] != nil && len(msg.RuleResult["face"].([]ruleserver.FaceResult)) > 0 { |
| | | 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) |
| | | faces = PutFace(faces, msg) |
| | | //logger.Info("整理后的数据:",faces) |
| | | if faces != nil { |
| | | for _,face := range faces { |
| | | var imgMaxUrl string = "" |
| | | var picTime string = "" |
| | | for _, face := range faces { |
| | | // 上传大图 |
| | | // 解压缩并上传图片 |
| | | bdata, err := util.UnCompress(msg.Data) |
| | |
| | | panic("解压缩图片时出现错误") |
| | | } |
| | | alarmRules := []AlarmRule{} |
| | | logger.Warn("人脸id为:",face.Id,"人脸的规则长度为:",len(face.rules)) |
| | | logger.Warn("人脸id为:", face.Id, "人脸的规则长度为:", len(face.rules)) |
| | | //os.Exit(1) |
| | | for _,faceResult := range face.rules { |
| | | for _, faceResult := range face.rules { |
| | | alarm := ChangeToString(faceResult.DefenceState, faceResult.AlarmLevel) |
| | | alarmRules = append(alarmRules, AlarmRule{faceResult.RuleGroupId, alarm, faceResult.RuleText, faceResult.DefenceState,faceResult.IsLink,""}) |
| | | alarmRules = append(alarmRules, AlarmRule{faceResult.RuleGroupId, alarm, faceResult.RuleText, faceResult.DefenceState, faceResult.IsLink, ""}) |
| | | } |
| | | i := protomsg.Image{} |
| | | err = proto.Unmarshal(bdata, &i) |
| | | bigPhotoUrl := make(map[string]interface{}) |
| | | bigPhotoUrl, err = util.PostFormBufferData(weedfsUrl, i, uuid.NewV4().String()) |
| | | logger.Debug("========大图路径:", bigPhotoUrl) |
| | | 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 { |
| | |
| | | if linkId != "" { |
| | | linksId = linkId |
| | | } |
| | | logger.Info("人脸的id:",strconv.FormatUint(face.Id, 10)) |
| | | logger.Info("人脸的分值:",face.Score) |
| | | 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, |
| | | i.Timestamp, |
| | | bigPhotoUrl["fileUrl"].(string), |
| | | picTime, |
| | | imgMaxUrl, |
| | | msg.Tasklab.Taskid, |
| | | msg.Tasklab.Taskname, |
| | | "人脸", |
| | | "", |
| | | alarmRules, |
| | | time.Now().Format("2006-01-02 15:04:05"), // 只检测,没有比对时间 |
| | | strconv.FormatUint(face.Id, 10), //暂改为人脸id strconv.FormatUint(face.Id, 10) |
| | | sex, //暂改为人脸id strconv.FormatUint(face.Id, 10) |
| | | face.ThftRes.Age, |
| | | fmt.Sprintf("%.2f",face.Score), // 暂改为分值fmt.Sprintf("%.2f",face.Score) |
| | | ageDescription, // 暂改为分值fmt.Sprintf("%.2f",face.Score) |
| | | race, |
| | | face.ThftRes.Smile, |
| | | face.ThftRes.Beauty, |
| | |
| | | 0, |
| | | 0, |
| | | face.Liker, |
| | | *target, |
| | | } |
| | | requstbody, err := json.Marshal(pervideo) |
| | | |
| | |
| | | } |
| | | |
| | | // 归置人脸 |
| | | func PutFace(faces []*FaceAndRules,msg ruleserver.ResultMsg)[]*FaceAndRules{ |
| | | if msg.RuleResult["face"] != nil && len(msg.RuleResult["face"].([]ruleserver.FaceResult)) > 0 { |
| | | for _, faceResult := range msg.RuleResult["face"].([]ruleserver.FaceResult) { |
| | | 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 |
| | |
| | | return nil |
| | | } |
| | | } |
| | | func hebingFace(faces []*FaceAndRules, faceResult ruleserver.FaceResult) []*FaceAndRules{ |
| | | func hebingFace(faces []*FaceAndRules, faceResult structure.FaceResult) []*FaceAndRules { |
| | | for _, arg := range faceResult.Args { |
| | | // 拿到每一张人脸 |
| | | logger.Info("归置人脸时相似者的数量:",len(arg.Liker)) |
| | | logger.Info("归置人脸时相似者的数量:", len(arg.Liker)) |
| | | flag := false |
| | | for _, face := range faces { |
| | | for _,lik := range face.Liker { |
| | | logger.Warn("--------合并人脸时相似者:",lik.PersonId,lik.TableName) |
| | | 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) |
| | | face.rules = append(face.rules, faceResult.Result) |
| | | // 相似者去重归并 |
| | | for _,liker := range arg.Liker { |
| | | for _, liker := range arg.Liker { |
| | | flag1 := true |
| | | for _,liker1 := range face.Liker { |
| | | 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, liker) |
| | | } |
| | | } |
| | | //face.Liker = append(face.Liker,arg.Liker...) |
| | | } |
| | | } |
| | | if !flag { |
| | | faces = append(faces, &FaceAndRules{arg, []ruleserver.Result{faceResult.Result}}) |
| | | faces = append(faces, &FaceAndRules{arg, []structure.Result{faceResult.Result}}) |
| | | } |
| | | } |
| | | return faces |
| | | } |
| | | |
| | | // 往es中插入yolo数据 |
| | | func InsertYolo(msg ruleserver.ResultMsg, linkId string) { |
| | | if msg.RuleResult["yolo"] != nil && len(msg.RuleResult["yolo"].([]ruleserver.Result)) > 0 { |
| | | 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"].([]ruleserver.Result) { |
| | | for _, res := range msg.RuleResult["yolo"].([]structure.Result) { |
| | | //logger.Info("定时器打的数字标签:",res.Others.TimeLabel) |
| | | if res.Others.TimeLabel == "01" || res.Others.TimeLabel == "10" { |
| | | flag = true |
| | |
| | | logger.Info("往ES插yolo数据") |
| | | var sdkNames string = "" |
| | | alarmRules := []AlarmRule{} |
| | | var targetInfos []Target |
| | | url := []string{} |
| | | for _, yoloResult := range msg.RuleResult["yolo"].([]ruleserver.Result) { |
| | | for _, yoloResult := range msg.RuleResult["yolo"].([]structure.Result) { |
| | | if yoloResult.Others.TimeLabel == "01" || yoloResult.Others.TimeLabel == "10" { |
| | | // 拼出sdkname |
| | | //logger.Info("应该进来才对的") |
| | |
| | | if yoloResult.IsLink { |
| | | linkInfo = "联动任务" |
| | | } |
| | | alarmRules = append(alarmRules, AlarmRule{yoloResult.RuleGroupId, alarm, yoloResult.RuleText, yoloResult.DefenceState,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)) |
| | |
| | | } |
| | | i := protomsg.Image{} |
| | | err = proto.Unmarshal(bdata, &i) |
| | | resp1, err1 := util.DrawPolygonOnImage(msg1.Cid, i, msg1.RuleResult["yolo"].([]ruleserver.Result), weedfsUrl) |
| | | resp1, err1 := util.DrawPolygonOnImageForYolo(msg1.Cid, i, msg1.RuleResult["yolo"].([]structure.Result), weedfsUrl) |
| | | if err1 != nil { |
| | | logger.Error("缓存数据画框或上传图片服务器出错", err) |
| | | } else { |
| | | logger.Info("上传的图片信息:", resp1) |
| | | } |
| | | url = append(url, resp1["fileUrl"].(string)) |
| | | 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"].([]ruleserver.Result) { |
| | | 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 { |
| | |
| | | if len(alarmRules) > 0 { |
| | | isAlarm = 1 |
| | | //resp, err = util.PostFormBufferData(weedfsUrl, i, uuid.NewV4().String()) |
| | | resp, err = util.DrawPolygonOnImage(msg.Cid, i, msg.RuleResult["yolo"].([]ruleserver.Result), weedfsUrl) |
| | | resp, err = util.DrawPolygonOnImageForYolo(msg.Cid, i, msg.RuleResult["yolo"].([]structure.Result), weedfsUrl) |
| | | if err != nil { |
| | | logger.Error("画框或上传图片服务器出错", err) |
| | | return |
| | |
| | | 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() |
| | |
| | | 0, |
| | | 0, |
| | | 0, |
| | | targetInfos, |
| | | } |
| | | requstbody, err := json.Marshal(peraction) |
| | | |