| | |
| | | "github.com/knetic/govaluate" |
| | | "plugin" |
| | | "ruleprocess/cache" |
| | | "ruleprocess/logger" |
| | | logger "github.com/alecthomas/log4go" |
| | | "ruleprocess/structure" |
| | | "sort" |
| | | "strconv" |
| | |
| | | cameraPolygons := GetPolygons(args.CameraId) |
| | | // 把所有的sdk提取的数据都按所属摄像机的区域归置 |
| | | logger.Debug("当前摄像机id为:",message.Cid,"当前摄像机执行的任务是:",message.Tasklab.Taskname,"--任务id为:",message.Tasklab.Taskid) |
| | | for _, arg := range args.Sdkdata { |
| | | SdkDataFormat(args.CameraId, arg, cameraPolygons) |
| | | } |
| | | CallReadyData(args,cameraPolygons) |
| | | // 跑本摄像机的所有规则组 一组一组跑 |
| | | taskGroup := GetRuleGroup(args.CameraId, args.TaskId) // 本摄像机本任务下所有规则组 |
| | | //logger.Println("看下摄像机下的任务组:",taskRuleList) |
| | |
| | | args.RuleResult["yolo"] = []structure.Result{} |
| | | args.RuleResult["face"] = []structure.Result{} |
| | | args.RuleResult["target"] = []structure.Result{} |
| | | args.RuleResult["plate"] = []structure.Result{} |
| | | //logger.Warn("传进去之前是什么德行:",args.RuleResult["yolo"]) |
| | | if taskGroup != nil && len(taskGroup.GroupRules) > 0 { |
| | | // 先过独立,再过联动 |
| | |
| | | return a,b,c |
| | | } |
| | | |
| | | func CallReadyData(args *structure.SdkDatas,cameraPolygons []protomsg.CameraPolygon) { |
| | | p,err := plugin.Open("./algorithm/middleware.so") |
| | | if err != nil { |
| | | panic(err) |
| | | } |
| | | f,err1 := p.Lookup("ReadData") |
| | | if err1 != nil { |
| | | panic("没有找到中间件的数据准备函数") |
| | | } |
| | | f.(func(args *structure.SdkDatas,cameraPolygons []protomsg.CameraPolygon)())(args,cameraPolygons) |
| | | } |
| | | |
| | | func RunRule(args *structure.SdkDatas, groupRule *protomsg.GroupRule, taskId string, message *protomsg.SdkMessage, label structure.Others) (bool,[]int) { |
| | | defer func() { |
| | | if err := recover(); err != nil { |
| | |
| | | |
| | | if result { |
| | | // 最后过持续时间等时间维度的条件 把时间规则位置调整到这个位置是为了缓存数据 !!!!!ps: 对画面中单个目标做定时器的不用再过画面定时器 |
| | | cacheId := "" |
| | | for j := 0; j < len(groupRule.Rules); j++ { |
| | | for _, sdkData := range args.Sdkdata { |
| | | sdk, err := cache.GetSdkById(groupRule.Rules[j].SdkId) |
| | |
| | | if ipcId == sdkData.IpcId { |
| | | for _, areaMap := range sdkData.AreaMapList { |
| | | // 去开启一个定时器 |
| | | cacheid := duration(groupRule.Rules[j], groupRule.GroupId, areaMap, args, message) |
| | | if cacheid != "" { |
| | | cacheId = cacheid |
| | | } |
| | | duration(groupRule.Rules[j], groupRule.GroupId, areaMap, args, message,label) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | // 进行定时器的处理和判断 |
| | | timeFlag := TimerAlarm(&label, groupRule.GroupId, result) |
| | | if timeFlag == "01" || timeFlag == "10" || timeFlag == "11" || cacheId != ""{ // 没有定时器或者满足定时器条件 |
| | | if timeFlag == "01" || timeFlag == "10" || timeFlag == "11"{ // 没有定时器或者满足定时器条件 |
| | | // 打人脸标签和yolo标签 |
| | | // 最后成功报警才把符合条件的人脸数据塞进结果标签里 |
| | | // 配了人脸的算法才把人脸的数据甩出来打标签 |
| | |
| | | //for _,face := range faces { |
| | | // //logger.Debug("————————————————________________看看人脸的坐标:",face.Location) |
| | | //} |
| | | logger.Warn("___________________________________________________________________________终于走完万里长征") |
| | | //logger.Warn("___________________________________________________________________________终于走完万里长征") |
| | | // 把他们的位置数据也传下去 |
| | | yolos := []*structure.Arg{} |
| | | for _, sdkData := range args.Sdkdata { |
| | |
| | | } |
| | | } |
| | | } |
| | | logger.Debug("------locations的内容:", yolos) |
| | | //logger.Debug("------locations的内容:", yolos) |
| | | // 处理目标定时数据 |
| | | targets := []*structure.Arg{} |
| | | for _, sdkData := range args.Sdkdata { |
| | | if sdkData.IpcId == "02D54B61-0F16-C604-8567-FC4BE493C523" && sdkNames != "" { // 把yolo数据的各个目标的坐标输出方便后面画框 |
| | | if sdkData.IpcId == "02D54B61-0F16-C604-8567-FC4BE493C523" && sdkNames != "" { // 输出目标数据 |
| | | for _, areaMap := range sdkData.AreaMapList { |
| | | if areaMap.IsEffective { |
| | | targets = append(targets, putTargetsToResult(areaMap)...) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | // 车辆目标统计 |
| | | cars := []*structure.Arg{} |
| | | for _, sdkData := range args.Sdkdata { |
| | | //logger.Info("看看sdkNames",sdkNames) |
| | | if sdkData.IpcId == "91d923ef-6200-4549-ab1b-8e773e85d729" { // 把车牌数据的各个目标的坐标输出方便后面画框 |
| | | for _, areaMap := range sdkData.AreaMapList { |
| | | if areaMap.IsEffective { |
| | | cars = append(cars, putFaceToResult(areaMap, cars)...) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | // 跟踪目标统计 |
| | | personTrack := []*structure.Arg{} |
| | | for _, sdkData := range args.Sdkdata { |
| | | //logger.Info("看看sdkNames",sdkNames) |
| | | if sdkData.IpcId == "跟踪的ipcId" { // 把跟踪页面的各个目标的坐标输出 |
| | | for _, areaMap := range sdkData.AreaMapList { |
| | | if areaMap.IsEffective { |
| | | personTrack = append(personTrack, putFaceToResult(areaMap, personTrack)...) |
| | | } |
| | | } |
| | | } |
| | |
| | | } else { |
| | | islink = false |
| | | } |
| | | //logger.Info("触发的区域id:",polygonId) |
| | | var labelTypes []int // 0为yolo标签,1为face标签 2为两者标签 |
| | | if sdkNames != "" { |
| | | if sdkNames != "" && len(targets) == 0 { |
| | | args.RuleResult["yolo"] = append(args.RuleResult["yolo"].([]structure.Result), structure.Result{taskId, sdkNames, groupRule.GroupId, groupRule.DefenceState, groupRule.AlarmLevel, groupRule.GroupText, yolos, polygonId, islink,label}) |
| | | labelTypes = append(labelTypes,0) |
| | | //logger.Info("-------------------yolo结果标签长度", len(args.RuleResult["yolo"].([]Result))) |
| | | } |
| | | if faceFlag { |
| | | args.RuleResult["face"] = append(args.RuleResult["face"].([]structure.Result), structure.Result{taskId, sdkNames, groupRule.GroupId, groupRule.DefenceState, groupRule.AlarmLevel, groupRule.GroupText, faces, polygonId, islink,label}) |
| | | //logger.Info("-------------------face结果标签", len(args.RuleResult["face"].([]FaceResult))) |
| | | //logger.Info("-------------------face结果标签", len(args.RuleResult["face"].([]structure.Result))) |
| | | labelTypes = append(labelTypes,1) |
| | | } |
| | | if len(targets) > 0 { |
| | | args.RuleResult["target"] = append(args.RuleResult["target"].([]structure.Result), structure.Result{taskId, sdkNames, groupRule.GroupId, groupRule.DefenceState, groupRule.AlarmLevel, groupRule.GroupText, targets, polygonId, islink,label}) |
| | | logger.Info("-------------------目标持续结果标签", len(args.RuleResult["target"].([]structure.Result))) |
| | | //logger.Info("-------------------目标持续结果标签", len(args.RuleResult["target"].([]structure.Result))) |
| | | //labelTypes = append(labelTypes,2) |
| | | } |
| | | if len(cars) > 0 { |
| | | args.RuleResult["plate"] = append(args.RuleResult["plate"].([]structure.Result), structure.Result{taskId, sdkNames, groupRule.GroupId, groupRule.DefenceState, groupRule.AlarmLevel, groupRule.GroupText, cars, polygonId, islink,label}) |
| | | //logger.Info("-------------------车牌结果标签", len(args.RuleResult["plate"].([]structure.Result))) |
| | | //labelTypes = append(labelTypes,2) |
| | | } |
| | | if len(personTrack) > 0 { |
| | | args.RuleResult["track"] = append(args.RuleResult["track"].([]structure.Result), structure.Result{taskId, sdkNames, groupRule.GroupId, groupRule.DefenceState, groupRule.AlarmLevel, groupRule.GroupText, personTrack, polygonId, islink,label}) |
| | | //logger.Info("-------------------车牌结果标签", len(args.RuleResult["plate"].([]structure.Result))) |
| | | //labelTypes = append(labelTypes,2) |
| | | } |
| | | // 给持续时间的第一张赋予缓存数据(遍历复制) |
| | | if cacheId != "" { // 有这帧数据的缓存 |
| | | tempMap := make(map[string]interface{}) |
| | | for k, result := range args.RuleResult { |
| | | if k == "yolo" { |
| | | tempMap[k] = []structure.Result{} |
| | | for _, res := range result.([]structure.Result) { |
| | | tempMap[k] = append(tempMap[k].([]structure.Result), res) |
| | | } |
| | | } |
| | | if k == "face" { |
| | | tempMap[k] = []structure.Result{} |
| | | for _, res := range result.([]structure.Result) { |
| | | tempMap[k] = append(tempMap[k].([]structure.Result), res) |
| | | } |
| | | } |
| | | } |
| | | rw.Lock() |
| | | TimeEleList[cacheId].CacheSdkData.RuleResult = tempMap |
| | | rw.Unlock() |
| | | } |
| | | //if cacheId != "" { // 有这帧数据的缓存 |
| | | // tempMap := make(map[string]interface{}) |
| | | // for k, result := range args.RuleResult { |
| | | // if k == "yolo" { |
| | | // tempMap[k] = []structure.Result{} |
| | | // for _, res := range result.([]structure.Result) { |
| | | // tempMap[k] = append(tempMap[k].([]structure.Result), res) |
| | | // } |
| | | // } |
| | | // if k == "face" { |
| | | // tempMap[k] = []structure.Result{} |
| | | // for _, res := range result.([]structure.Result) { |
| | | // tempMap[k] = append(tempMap[k].([]structure.Result), res) |
| | | // } |
| | | // } |
| | | // } |
| | | // rw.Lock() |
| | | // TimeEleList[cacheId].CacheSdkData.RuleResult = tempMap |
| | | // rw.Unlock() |
| | | //} |
| | | |
| | | return true,labelTypes |
| | | } else { |
| | |
| | | // 结果为假时也要走,有杀死定时器的操作 |
| | | TimerAlarm(&label, groupRule.GroupId, result) |
| | | //fmt.Println(timeFlag) |
| | | logger.Info("不符合规则") |
| | | //logger.Info("不符合规则") |
| | | return false,[]int{} |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | } |
| | | //logger.Println("-----------------------------------------------听说你是空的?",faces) |
| | | return faces |
| | | } |
| | | |
| | |
| | | if len(am.FilterData) > 0 { |
| | | locations = am.FilterData |
| | | } |
| | | //logger.Println("-----------------------------------------------听说你是空的?",faces) |
| | | return locations |
| | | } |
| | | func putTargetsToResult(am *structure.AreaMap) []*structure.Arg { |
| | |
| | | if len(am.AlarmObj) > 0 { |
| | | locations = am.AlarmObj |
| | | } |
| | | //logger.Println("-----------------------------------------------听说你是空的?",faces) |
| | | return locations |
| | | } |
| | | // 联动任务的处理 |
| | |
| | | } |
| | | } |
| | | va.CacheData = structure.ResultMsg{message, tempMap} |
| | | logger.Info("这个摄像机--", args.CameraId, "--被赋予了result", va.Result) |
| | | //logger.Info("这个摄像机--", args.CameraId, "--被赋予了result", va.Result) |
| | | } |
| | | } |
| | | // 判断结果数组是否完满(即被赋值完毕)可得出报警结果 |
| | |
| | | for _, va := range timeEle.RuleResults { |
| | | completeFormula = completeFormula + va.RuleWithPre + "" + va.Result |
| | | } |
| | | logger.Info("---------------------------联动任务的公式", completeFormula) |
| | | //logger.Info("---------------------------联动任务的公式", completeFormula) |
| | | if completeFormula != "" { |
| | | expression, _ := govaluate.NewEvaluableExpression(completeFormula) |
| | | result, _ := expression.Evaluate(nil) // 得到数学公式的结果 |
| | | if result.(bool) { |
| | | logger.Info("___________________________________________________________________联动任务报警") |
| | | //logger.Info("___________________________________________________________________联动任务报警") |
| | | rw.RLock() |
| | | if TimeEleList[groupRule.GroupId] != nil { // 极偶尔有情况会等于nil,不知道为啥,做个判断以防宕机 |
| | | // 把数组里缓存的数据取出来一起报警 |
| | |
| | | } |
| | | } |
| | | } else { |
| | | logger.Warn("数组不圆满不打标签") |
| | | //logger.Warn("数组不圆满不打标签") |
| | | // 倒是把打的组规则标签给去掉了啊 |
| | | for _,val := range labelTypes { |
| | | if val == 0 { |
| | |
| | | for k, _ := range TimeEleList { |
| | | if k == groupRule.GroupId { |
| | | delete(TimeEleList, k) |
| | | logger.Debug("因为定时器的一帧数据结果为false,干掉定时器") |
| | | //logger.Debug("因为定时器的一帧数据结果为false,干掉定时器") |
| | | } |
| | | } |
| | | rw.Unlock() |
| | |
| | | } |
| | | |
| | | // 如果有持续时间条件维护开启一个定时器 |
| | | func duration(rule *protomsg.Rule, groupId string, am *structure.AreaMap, args *structure.SdkDatas, message *protomsg.SdkMessage) string{ |
| | | cacheId := "" |
| | | func duration(rule *protomsg.Rule, groupId string, am *structure.AreaMap, args *structure.SdkDatas, message *protomsg.SdkMessage, label structure.Others){ |
| | | // cacheId := "" |
| | | if rule.PolygonId == am.AreaId { // 首先规则所对应的区域id要跟区域数据的id对的上 配置的算法要对的上 |
| | | if rule.SdkId == "812b674b-2375-4589-919a-5c1c3278a977" { // 排除对画面中单个目标的算法,个体静止以及靠右行这种 |
| | | return "" |
| | | return |
| | | } |
| | | if rule.SdkArgAlias == "duration" { |
| | | //logger.Info("当前小规则是:---------", rule) |
| | |
| | | } |
| | | |
| | | if flag { |
| | | //m := make(map[string]interface{}) |
| | | //m["yolo"] = []structure.Result{} |
| | | //m["yolo"] = append(m["yolo"].([]structure.Result), structure.Result{args.TaskId, "", "", true, 0, "", am.FilterData, am.AreaJson, false,*lable}) |
| | | m := make(map[string]interface{}) |
| | | m["yolo"] = []structure.Result{} |
| | | m["yolo"] = append(m["yolo"].([]structure.Result), structure.Result{args.TaskId, "", "", true, 0, "", am.FilterData, am.AreaJson, false,label}) |
| | | timeLength, _ := strconv.Atoi(rule.SdkArgValue) |
| | | timeEle := TimeElement{N: timeLength, InitN: timeLength, AlarmFlag: false, BufferFlag: 10, CacheSdkData: structure.ResultMsg{message, nil}} // 扔进去一个定时器元素(并缓存当前画面帧数据) |
| | | timeEle := TimeElement{N: timeLength, InitN: timeLength, AlarmFlag: false, BufferFlag: 10, CacheSdkData: structure.ResultMsg{message, m}} // 扔进去一个定时器元素(并缓存当前画面帧数据) |
| | | //TimeEleList = make(map[string]timeElement) |
| | | TimeEleList[groupId+"+"+rule.Id] = &timeEle // 定时器元素以当前持续时间小规则id为键 |
| | | logger.Info("创建了计数器") |
| | | cacheId = groupId+"+"+rule.Id |
| | | //logger.Info("创建了计数器") |
| | | //cacheId = groupId+"+"+rule.Id |
| | | } |
| | | rw.Unlock() |
| | | } |
| | | } |
| | | return cacheId |
| | | return |
| | | } |
| | | |