| | |
| | | "strconv" |
| | | ) |
| | | // 人脸算法 |
| | | func Entrance(rule *protomsg.Rule, am *structure.AreaMap) structure.LittleRuleResult { |
| | | func Entrance(rule *protomsg.Rule, am *structure.AreaMap,lable *structure.Others,args *structure.SdkDatas,message *protomsg.SdkMessage) structure.LittleRuleResult { |
| | | logger.Debug("---------走了人脸检测算法",rule.Id,rule.SdkArgAlias,rule.Operator,rule.SdkArgValue,am.AreaId) |
| | | return filterRule(rule,am) |
| | | } |
| | |
| | | "strconv" |
| | | ) |
| | | // 人脸算法 |
| | | func Entrance(rule *protomsg.Rule, am *structure.AreaMap) structure.LittleRuleResult { |
| | | func Entrance(rule *protomsg.Rule, am *structure.AreaMap,lable *structure.Others,args *structure.SdkDatas,message *protomsg.SdkMessage) structure.LittleRuleResult { |
| | | logger.Debug("---------走了人脸比对算法",rule.Id,rule.SdkArgAlias,rule.Operator,rule.SdkArgValue,am.AreaId) |
| | | return filterRule1(rule,am) |
| | | } |
| | |
| | | ) |
| | | |
| | | //入侵算法 |
| | | func Entrance(rule *protomsg.Rule, am *structure.AreaMap) structure.LittleRuleResult { |
| | | func Entrance(rule *protomsg.Rule, am *structure.AreaMap,lable *structure.Others,args *structure.SdkDatas,message *protomsg.SdkMessage) structure.LittleRuleResult { |
| | | if rule.PolygonId == am.AreaId { // 首先这条规则得是这个算法的规则,其次规则所对应的区域id要跟区域数据的id对的上 |
| | | return filterRule(rule, am) |
| | | } else { |
| | |
| | | "time" |
| | | ) |
| | | |
| | | func Entrance (args *structure.SdkDatas,groupRule protomsg.GroupRule) (bool,string,string){ |
| | | func Entrance (args *structure.SdkDatas,groupRule protomsg.GroupRule,lable *structure.Others,message *protomsg.SdkMessage) (bool,string,string){ |
| | | resultSplice := []*structure.LittleRuleResult{} |
| | | sdkNames := "" |
| | | polygonId := "" |
| | |
| | | if ipcId == sdkData.IpcId { |
| | | //logger.Info("当前走的规则是--:", sdkName, "---","") |
| | | for _, areaMap := range sdkData.AreaMapList { |
| | | ruleResult := CallSo(sdk.Id, groupRule.Rules[j], areaMap) |
| | | ruleResult := CallSo(sdk.Id, groupRule.Rules[j], areaMap,lable,args,message) |
| | | if ruleResult.Result != "" { |
| | | logger.Info("条件规则结果:", ruleResult.Result) |
| | | // 如果结果为真,把这条规则中的区域置为有效 |
| | |
| | | if ipcId == sdkData.IpcId { |
| | | //logger.Info("当前走的规则是--:", sdkName, "---","") |
| | | for _, areaMap := range sdkData.AreaMapList { |
| | | ruleResult := CallSo(sdk.Id, groupRule.Rules[j], areaMap) |
| | | ruleResult := CallSo(sdk.Id, groupRule.Rules[j], areaMap,lable,args,message) |
| | | if ruleResult.Result != "" { |
| | | logger.Info("人脸比对规则结果:", ruleResult.Result) |
| | | // 如果结果为真,把这条规则中的区域置为有效 |
| | |
| | | } |
| | | // 个体静止 |
| | | for j := 0; j < len(groupRule.Rules); j++ { |
| | | if groupRule.Rules[j].SdkId == "812b674b-2375-4589-919a-5c1c3278a972" && groupRule.Rules[j].SdkArgAlias != "time_rule"{ |
| | | if groupRule.Rules[j].SdkId == "个体静止" && groupRule.Rules[j].SdkArgAlias != "time_rule"{ |
| | | for _, sdkData := range args.Sdkdata { |
| | | // 根据规则的sdkId查出其对应的ipcId,用ipcId去找该比对的数据 |
| | | sdk, err := cache.GetSdkById(groupRule.Rules[j].SdkId) |
| | |
| | | if ipcId == sdkData.IpcId { |
| | | //logger.Info("当前走的规则是--:", sdkName, "---","") |
| | | for _, areaMap := range sdkData.AreaMapList { |
| | | ruleResult := CallSo(sdk.Id, groupRule.Rules[j], areaMap) |
| | | ruleResult := CallSo(sdk.Id, groupRule.Rules[j], areaMap,lable,args,message) |
| | | if ruleResult.Result != "" { |
| | | logger.Info("人脸比对规则结果:", ruleResult.Result) |
| | | logger.Info("个体静止结果:", ruleResult.Result) |
| | | // 如果结果为真,把这条规则中的区域置为有效 |
| | | if strings.Contains(ruleResult.Result, "true") { |
| | | areaMap.IsEffective = true |
| | |
| | | sdkName := sdk.SdkName |
| | | if ipcId == sdkData.IpcId { |
| | | for _, areaMap := range sdkData.AreaMapList { |
| | | ruleResult := CallSo(sdk.Id, groupRule.Rules[j], areaMap) |
| | | ruleResult := CallSo(sdk.Id, groupRule.Rules[j], areaMap,lable,args,message) |
| | | if ruleResult.Result != "" { |
| | | if strings.Contains(ruleResult.Result, "true") { |
| | | areaMap.IsEffective = true |
| | |
| | | return structure.LittleRuleResult{} |
| | | } |
| | | |
| | | func CallSo(sdkId string,rule *protomsg.Rule, am *structure.AreaMap) structure.LittleRuleResult{ |
| | | func CallSo(sdkId string,rule *protomsg.Rule, am *structure.AreaMap,lable *structure.Others,args *structure.SdkDatas,message *protomsg.SdkMessage) structure.LittleRuleResult{ |
| | | // 根据sdkId查出其对应的sdk的soName,调用相应so的Entrance方法 |
| | | var soName = "" |
| | | if sdkId == "812b674b-2375-4589-919a-5c1c3278a97e" { |
| | |
| | | soName = "personUnsual.so" |
| | | } else if sdkId == "812b674b-2375-4589-919a-5c1c3278a972" { |
| | | soName = "faceCompare.so" |
| | | } else if sdkId == "个体静止" { |
| | | soName = "static.so" |
| | | } |
| | | //soInfo,errr := cache.GetSoInfoById(sdkId) |
| | | //if errr != nil { |
| | |
| | | if err1 != nil { |
| | | panic("没有找到入口函数") |
| | | } |
| | | ruleResult := f.(func(rule *protomsg.Rule, am *structure.AreaMap)structure.LittleRuleResult)(rule,am) |
| | | ruleResult := f.(func(rule *protomsg.Rule, am *structure.AreaMap,lable *structure.Others, args *structure.SdkDatas,message *protomsg.SdkMessage)structure.LittleRuleResult)(rule,am,lable,args,message) |
| | | return ruleResult |
| | | } |
| | | |
| | |
| | | ) |
| | | |
| | | // 人员异常算法 |
| | | func Entrance(rule *protomsg.Rule, am *structure.AreaMap) structure.LittleRuleResult { |
| | | func Entrance(rule *protomsg.Rule, am *structure.AreaMap,lable *structure.Others,args *structure.SdkDatas,message *protomsg.SdkMessage) structure.LittleRuleResult { |
| | | if rule.PolygonId == am.AreaId { // 首先这条规则得是这个算法的规则,其次规则所对应的区域id要跟区域数据的id对的上 |
| | | logger.Debug("---------走了人员异常算法",rule.Id,rule.SdkArgAlias,rule.Operator,rule.SdkArgValue,am.AreaId) |
| | | if rule.SdkArgAlias == "score" || rule.SdkArgAlias == "proportion" || rule.SdkArgAlias == "size" { // 判断的是相似值,占比,尺寸等过滤条件,如果再有,还可以再加 |
| | | if rule.SdkArgAlias == "score" || rule.SdkArgAlias == "proportion" || rule.SdkArgAlias == "size" || rule.SdkArgAlias == "" { // 判断的是相似值,占比,尺寸等过滤条件,如果再有,还可以再加 |
| | | return filterRule(rule, am) |
| | | } else if rule.SdkArgAlias == "objCount" { |
| | | return transferParameters(rule, am) |
| | |
| | | // 过滤规则先筛选出符合条件的目标数量 |
| | | func filterRule(rule *protomsg.Rule, am *structure.AreaMap) structure.LittleRuleResult { |
| | | // 处理的都是yolo数据 |
| | | if rule.PolygonId == am.AreaId { // 首先这条规则得是这个算法的规则,其次规则所对应的区域id要跟区域数据的id对的上 |
| | | if rule.SdkArgAlias == "score" || rule.SdkArgAlias == "proportion" || rule.SdkArgAlias == "size" { // 判断的是相似值,占比,尺寸等过滤条件,如果再有,还可以再加 |
| | | var args []*structure.Arg |
| | | if rule.RuleWithPre == "&&" { |
| | | args = am.FilterData |
| | | if rule.SdkArgAlias == "score" || rule.SdkArgAlias == "proportion" || rule.SdkArgAlias == "size" { // 判断的是相似值,占比,尺寸等过滤条件,如果再有,还可以再加 |
| | | var args []*structure.Arg |
| | | if rule.RuleWithPre == "&&" { |
| | | args = am.FilterData |
| | | } else { |
| | | args = am.Args |
| | | } |
| | | // 先清空过滤后的数据,再往里塞本次过滤后的数据 |
| | | am.FilterData = am.FilterData[0:0] |
| | | //logger.Debug("看看args:::::", args) |
| | | for _, arg := range args { |
| | | var formula string |
| | | if rule.SdkArgAlias == "score" { |
| | | formula = strconv.FormatFloat(arg.Score, 'f', -1, 64) + " " + rule.Operator + " " + rule.SdkArgValue // 得到字符串公式 |
| | | logger.Info("当前相似度小公式:", formula) |
| | | } else if rule.SdkArgAlias == "proportion" { |
| | | formula = strconv.FormatFloat(arg.Proportion, 'f', -1, 64) + " " + rule.Operator + " " + rule.SdkArgValue // 得到字符串公式 |
| | | logger.Info("当前占比小公式:", formula) |
| | | } else { |
| | | args = am.Args |
| | | formula = strconv.FormatFloat(arg.Size, 'f', -1, 64) + " " + rule.Operator + " " + rule.SdkArgValue // 得到字符串公式 |
| | | logger.Info("当前尺寸小公式:", formula) |
| | | } |
| | | // 先清空过滤后的数据,再往里塞本次过滤后的数据 |
| | | am.FilterData = am.FilterData[0:0] |
| | | //logger.Debug("看看args:::::", args) |
| | | for _, arg := range args { |
| | | var formula string |
| | | if rule.SdkArgAlias == "score" { |
| | | formula = strconv.FormatFloat(arg.Score, 'f', -1, 64) + " " + rule.Operator + " " + rule.SdkArgValue // 得到字符串公式 |
| | | logger.Info("当前相似度小公式:", formula) |
| | | } else if rule.SdkArgAlias == "proportion" { |
| | | formula = strconv.FormatFloat(arg.Proportion, 'f', -1, 64) + " " + rule.Operator + " " + rule.SdkArgValue // 得到字符串公式 |
| | | logger.Info("当前占比小公式:", formula) |
| | | } else { |
| | | formula = strconv.FormatFloat(arg.Size, 'f', -1, 64) + " " + rule.Operator + " " + rule.SdkArgValue // 得到字符串公式 |
| | | logger.Info("当前尺寸小公式:", formula) |
| | | } |
| | | expression, _ := govaluate.NewEvaluableExpression(formula) // 得到数学公式 |
| | | result, _ := expression.Evaluate(nil) // 得到数学公式的结果 |
| | | if result.(bool) { |
| | | am.FilterData = append(am.FilterData, arg) // 得到符合条件的过滤数据 |
| | | } |
| | | expression, _ := govaluate.NewEvaluableExpression(formula) // 得到数学公式 |
| | | result, _ := expression.Evaluate(nil) // 得到数学公式的结果 |
| | | if result.(bool) { |
| | | am.FilterData = append(am.FilterData, arg) // 得到符合条件的过滤数据 |
| | | } |
| | | am.TargetNum = len(am.FilterData) // 把符合条件的目标数量更新到targetNum字段 |
| | | if am.TargetNum > 0 { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "true", rule.Sort} |
| | | } else { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "false", rule.Sort} |
| | | } |
| | | } |
| | | am.TargetNum = len(am.FilterData) // 把符合条件的目标数量更新到targetNum字段 |
| | | if am.TargetNum > 0 { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "true", rule.Sort} |
| | | } else { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "false", rule.Sort} |
| | | } |
| | | |
| | | } else if rule.SdkArgAlias == "" { |
| | | if am.TargetNum > 0 { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "true", rule.Sort} |
| | | } else { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "false", rule.Sort} |
| | | } |
| | | } else if rule.SdkArgAlias == "" { |
| | | if am.TargetNum > 0 { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "true", rule.Sort} |
| | | } else { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "false", rule.Sort} |
| | | } |
| | | } |
| | | return structure.LittleRuleResult{} |
| | |
| | | "ruleprocess/ruleserver" |
| | | "ruleprocess/structure" |
| | | "strconv" |
| | | "sync" |
| | | ) |
| | | |
| | | //个体静止算法 |
| | | var StaticMap = make(map[string]CameraArea) |
| | | |
| | | type CameraArea struct { |
| | | targets []Obj |
| | | duration int |
| | | cache structure.ResultMsg |
| | | } |
| | | type Obj struct { |
| | | id string |
| | | location []structure.Rect |
| | | staticStatus int |
| | | } |
| | | |
| | | func Entrance(rule *protomsg.Rule, am *structure.AreaMap) structure.LittleRuleResult { |
| | | for _, obj := range am.FilterData { |
| | | var flag bool = true |
| | | for k, _ := range ruleserver.TimeEleList { |
| | | if k == rule.Id+""+strconv.Itoa(int(obj.Id)) { |
| | | flag = false // 有就置为false |
| | | logger.Info("有这个定时器,不再创建了:") |
| | | } |
| | | func Entrance(rule *protomsg.Rule, am *structure.AreaMap,lable *structure.Others,args *structure.SdkDatas,message *protomsg.SdkMessage) structure.LittleRuleResult { |
| | | if rule.PolygonId == am.AreaId { // 首先这条规则得是这个算法的规则,其次规则所对应的区域id要跟区域数据的id对的上 |
| | | logger.Debug("---------走了人员异常算法", rule.Id, rule.SdkArgAlias, rule.Operator, rule.SdkArgValue, am.AreaId) |
| | | if rule.SdkArgAlias == "score" || rule.SdkArgAlias == "proportion" || rule.SdkArgAlias == "size" || rule.SdkArgAlias == "" { // 判断的是相似值,占比,尺寸等过滤条件,如果再有,还可以再加 |
| | | return filterRule(rule, am) |
| | | } else if rule.SdkArgAlias == "duration" { |
| | | return CompareAndSave(rule,am,lable,args,message) |
| | | } else { |
| | | return structure.LittleRuleResult{} |
| | | } |
| | | if flag { |
| | | timeEle := ruleserver.TimeElement{N: 10, InitN: 10, AlarmFlag: false, BufferFlag: 5} // 扔进去一个定时器元素 |
| | | ruleserver.TimeEleList[rule.Id+""+strconv.Itoa(int(obj.Id))] = &timeEle // 定时器元素以小规则id和目标id为键 |
| | | } |
| | | } else { |
| | | return structure.LittleRuleResult{} |
| | | } |
| | | return structure.LittleRuleResult{} |
| | | } |
| | | |
| | | // 过滤规则先筛选出符合条件的目标数量 |
| | | func filterRule(rule *protomsg.Rule, am *structure.AreaMap) structure.LittleRuleResult { |
| | | // 处理的都是yolo数据 |
| | | var args []*structure.Arg |
| | | if rule.RuleWithPre == "&&" { |
| | | args = am.FilterData |
| | | } else { |
| | | args = am.Args |
| | | } |
| | | // 先清空过滤后的数据,再往里塞本次过滤后的数据 |
| | | am.FilterData = am.FilterData[0:0] |
| | | //logger.Debug("看看args:::::", args) |
| | | for _, arg := range args { |
| | | var formula string |
| | | if rule.SdkArgAlias == "score" { |
| | | formula = strconv.FormatFloat(arg.Score, 'f', -1, 64) + " " + rule.Operator + " " + rule.SdkArgValue // 得到字符串公式 |
| | | logger.Info("当前相似度小公式:", formula) |
| | | } else if rule.SdkArgAlias == "proportion" { |
| | | formula = strconv.FormatFloat(arg.Proportion, 'f', -1, 64) + " " + rule.Operator + " " + rule.SdkArgValue // 得到字符串公式 |
| | | logger.Info("当前占比小公式:", formula) |
| | | if rule.SdkArgAlias == "score" || rule.SdkArgAlias == "proportion" || rule.SdkArgAlias == "size" { // 判断的是相似值,占比,尺寸等过滤条件,如果再有,还可以再加 |
| | | var args []*structure.Arg |
| | | if rule.RuleWithPre == "&&" { |
| | | args = am.FilterData |
| | | } else { |
| | | formula = strconv.FormatFloat(arg.Size, 'f', -1, 64) + " " + rule.Operator + " " + rule.SdkArgValue // 得到字符串公式 |
| | | logger.Info("当前尺寸小公式:", formula) |
| | | args = am.Args |
| | | } |
| | | expression, _ := govaluate.NewEvaluableExpression(formula) // 得到数学公式 |
| | | result, _ := expression.Evaluate(nil) // 得到数学公式的结果 |
| | | if result.(bool) { |
| | | am.FilterData = append(am.FilterData, arg) // 得到符合条件的过滤数据 |
| | | // 先清空过滤后的数据,再往里塞本次过滤后的数据 |
| | | am.FilterData = am.FilterData[0:0] |
| | | //logger.Debug("看看args:::::", args) |
| | | for _, arg := range args { |
| | | var formula string |
| | | if rule.SdkArgAlias == "score" { |
| | | formula = strconv.FormatFloat(arg.Score, 'f', -1, 64) + " " + rule.Operator + " " + rule.SdkArgValue // 得到字符串公式 |
| | | logger.Info("当前相似度小公式:", formula) |
| | | } else if rule.SdkArgAlias == "proportion" { |
| | | formula = strconv.FormatFloat(arg.Proportion, 'f', -1, 64) + " " + rule.Operator + " " + rule.SdkArgValue // 得到字符串公式 |
| | | logger.Info("当前占比小公式:", formula) |
| | | } else { |
| | | formula = strconv.FormatFloat(arg.Size, 'f', -1, 64) + " " + rule.Operator + " " + rule.SdkArgValue // 得到字符串公式 |
| | | logger.Info("当前尺寸小公式:", formula) |
| | | } |
| | | expression, _ := govaluate.NewEvaluableExpression(formula) // 得到数学公式 |
| | | result, _ := expression.Evaluate(nil) // 得到数学公式的结果 |
| | | if result.(bool) { |
| | | am.FilterData = append(am.FilterData, arg) // 得到符合条件的过滤数据 |
| | | } |
| | | } |
| | | am.TargetNum = len(am.FilterData) // 把符合条件的目标数量更新到targetNum字段 |
| | | if am.TargetNum > 0 { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "true", rule.Sort} |
| | | } else { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "false", rule.Sort} |
| | | } |
| | | |
| | | } else if rule.SdkArgAlias == "" { |
| | | if am.TargetNum > 0 { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "true", rule.Sort} |
| | | } else { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "false", rule.Sort} |
| | | } |
| | | } |
| | | am.TargetNum = len(am.FilterData) // 把符合条件的目标数量更新到targetNum字段 |
| | | if am.TargetNum > 0 { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "true", rule.Sort} |
| | | } else { |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "false", rule.Sort} |
| | | } |
| | | return structure.LittleRuleResult{} |
| | | } |
| | | |
| | | // 判断两个矩形的重合度,把面积更大的做分母 |
| | | // 判断两个矩形的重合度,把面积更大的做分母,即取小值 |
| | | func PgsInterPercent(pgpts []structure.Point, box structure.Rect, widthScale float64, heightScale float64) (percent float64) { |
| | | |
| | | areapts, areaBox := ruleserver.GetLocation(box, 10) |
| | |
| | | } |
| | | return (perInterPg * 100) |
| | | } |
| | | // 判断一个区域内有没有静止的目标 |
| | | func CompareAndSave(rule *protomsg.Rule, am *structure.AreaMap,lable *structure.Others,args *structure.SdkDatas,message *protomsg.SdkMessage) structure.LittleRuleResult { |
| | | initN := 60 |
| | | if rule.SdkId == "个体静止" && rule.SdkArgAlias == "duration" { |
| | | if init,err := strconv.Atoi(rule.SdkArgValue); err != nil { |
| | | logger.Debug("个体静止算法读取持续时间失败",err) |
| | | } else { |
| | | initN = init |
| | | } |
| | | } |
| | | if len(structure.StaticMap[am.AreaId].Targets) == 0 { // 即第一帧数据(也不一定),还没有缓存 |
| | | objs := []*structure.Obj{} |
| | | for _, tar := range am.FilterData { |
| | | obj := &structure.Obj{Id: tar.Id, Location: tar.Location, N: initN,InitN:initN} |
| | | objs = append(objs, obj) |
| | | } |
| | | structure.StaticMap[am.AreaId] = &structure.CameraArea{objs} |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + "false", rule.Sort} |
| | | } else { |
| | | flag := "false" |
| | | for _, tar := range structure.StaticMap[am.AreaId].Targets { |
| | | singleResult := SingleStatic(tar,am,lable,90) |
| | | if singleResult { |
| | | flag = "true" |
| | | } |
| | | } |
| | | // 更新数据,把新来的数据写入缓存 |
| | | objs := []*structure.Obj{} |
| | | for _, tar := range am.FilterData { |
| | | flag1 := false |
| | | for _, OBJ := range structure.StaticMap[am.AreaId].Targets { |
| | | if tar.Id == OBJ.Id { |
| | | flag1 = true |
| | | } |
| | | } |
| | | if !flag1 { // 集合中没有的才插入 |
| | | obj := &structure.Obj{Id: tar.Id, Location: tar.Location, N: initN,InitN:initN,AlarmFlag: false, BufferFlag: 10, CacheSdkData: structure.ResultMsg{message, nil}} |
| | | objs = append(objs, obj) |
| | | } |
| | | } |
| | | structure.StaticMap[am.AreaId] = &structure.CameraArea{objs} |
| | | return structure.LittleRuleResult{am.SdkName, rule.RuleWithPre + "" + flag, rule.Sort} |
| | | } |
| | | } |
| | | |
| | | // rec变为[t]point 逆时针一圈的坐标 |
| | | func Rect2Point(rect structure.Rect) []structure.Point { |
| | | points := []structure.Point{} |
| | | point1 := structure.Point{rect.X, rect.Y} |
| | | points = append(points, point1) |
| | | point2 := structure.Point{rect.X, rect.Y + rect.Height} |
| | | points = append(points, point2) |
| | | point3 := structure.Point{rect.X + rect.Width, rect.Y + rect.Height} |
| | | points = append(points, point3) |
| | | point4 := structure.Point{rect.X + rect.Width, rect.Y} |
| | | points = append(points, point4) |
| | | return points |
| | | } |
| | | |
| | | // 判断一个目标是否静止了指定时间 |
| | | func SingleStatic(person *structure.Obj, am *structure.AreaMap,lable *structure.Others, argValue float64) bool{ |
| | | flag := false |
| | | for _, obj := range am.FilterData { |
| | | if person.Id == obj.Id { |
| | | coincidenceDegree := PgsInterPercent(Rect2Point(person.Location), obj.Location, 1, 1) |
| | | if coincidenceDegree >= argValue { |
| | | flag = true |
| | | } |
| | | } |
| | | } |
| | | if flag { // 有一个对象保持静止(id相等并且重合度高于阈值) |
| | | flagTime := TimerAlarm(lable,person,flag,am.AreaId) |
| | | if flagTime == "10" || flagTime == "11" { |
| | | return flag |
| | | } else { |
| | | return false |
| | | } |
| | | } else { |
| | | TimerAlarm(lable,person,flag,am.AreaId) |
| | | return flag |
| | | } |
| | | } |
| | | var rw sync.RWMutex |
| | | // 判断是否符合定时器条件 |
| | | func TimerAlarm(oth *structure.Others,person *structure.Obj, result bool,areaId string) (string) { |
| | | var flagTime string // |
| | | |
| | | rw.Lock() |
| | | |
| | | if result { // 结果为真 |
| | | for k, tar := range structure.StaticMap[areaId].Targets { |
| | | if tar.Id == person.Id { |
| | | if tar.N == 0 && tar.AlarmFlag { |
| | | logger.Debug("-------------------------符合持续时间规则但并不是首次,不报警") |
| | | flagTime = "11" |
| | | oth.TimeLabel = flagTime |
| | | } |
| | | if tar.N == 0 && !tar.AlarmFlag { // 这组规则的定时器要全部等于0 暂且认为一组规则只有一个定时器 |
| | | logger.Debug("———————————-------------首次符合持续时间规则并报警") |
| | | flagTime = "10" |
| | | tar.AlarmFlag = true |
| | | oth.CacheData = []structure.ResultMsg{} |
| | | oth.CacheData = append(oth.CacheData,tar.CacheSdkData) |
| | | oth.TimeLabel = flagTime |
| | | } |
| | | if tar.N != 0 { |
| | | flagTime = "00" |
| | | // 有定时器但不为0把已打的标签删除 |
| | | // args.RuleResult = nil |
| | | logger.Debug("------------------------结果为真但计数器不到0,不报警,此时的计数器", k, "的值为:", tar.N) |
| | | } |
| | | } |
| | | } |
| | | } else { // 结果为假 干掉这个计数器 |
| | | for index, tar := range structure.StaticMap[areaId].Targets { |
| | | if tar.Id == person.Id { |
| | | if tar.AlarmFlag { |
| | | if tar.BufferFlag == 0 { |
| | | logger.Debug("------------------------------杀死计数器,报警此帧状态改变的数据,此时的计数器的值为", tar.N) |
| | | flagTime = "12" |
| | | oth.TimeLabel = flagTime |
| | | structure.StaticMap[areaId].Targets = append(structure.StaticMap[areaId].Targets[:index],structure.StaticMap[areaId].Targets[index+1:]...) |
| | | } else { |
| | | if tar.BufferFlag > 0 { |
| | | logger.Debug("缓冲区减减") |
| | | tar.BufferFlag-- |
| | | } |
| | | } |
| | | } else { |
| | | logger.Debug("-----------结果为假且不到0,杀死定时器") |
| | | structure.StaticMap[areaId].Targets = append(structure.StaticMap[areaId].Targets[:index],structure.StaticMap[areaId].Targets[index+1:]...) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | rw.Unlock() |
| | | return flagTime |
| | | } |
| | | |
| | | |
New file |
| | |
| | | package main |
| | | |
| | | import ( |
| | | "fmt" |
| | | "ruleprocess/structure" |
| | | "testing" |
| | | ) |
| | | |
| | | func TestRect2Point(t *testing.T) { |
| | | rect := structure.Rect{100,100,200,200} |
| | | points := Rect2Point(rect) |
| | | fmt.Println(points) |
| | | } |
| | |
| | | |
| | | // 往es中插入人脸数据 |
| | | func InsertFace(msg structure.ResultMsg, linkId string) { |
| | | if msg.RuleResult["face"] != nil && len(msg.RuleResult["face"].([]structure.FaceResult)) > 0 { |
| | | if msg.RuleResult["face"] != nil && len(msg.RuleResult["face"].([]structure.TargetResult)) > 0 { |
| | | logger.Info("往ES插人脸数据") |
| | | faces := []*FaceAndRules{} |
| | | faces = PutFace(faces, msg) |
| | |
| | | // 上传大图 |
| | | if imgMaxUrl == "" { |
| | | bigPhotoUrl := make(map[string]interface{}) |
| | | bigPhotoUrl, err = util.DrawPolygonOnImageForFace(msg.Cid, i, msg.RuleResult["face"].([]structure.FaceResult), weedfsUrl) |
| | | bigPhotoUrl, err = util.DrawPolygonOnImageForFace(msg.Cid, i, msg.RuleResult["face"].([]structure.TargetResult), weedfsUrl) |
| | | logger.Debug("========大图路径:", bigPhotoUrl) |
| | | imgMaxUrl = bigPhotoUrl["fileUrl"].(string) |
| | | picTime = i.Timestamp |
| | |
| | | |
| | | // 归置人脸 |
| | | 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) { |
| | | if msg.RuleResult["face"] != nil && len(msg.RuleResult["face"].([]structure.TargetResult)) > 0 { |
| | | for _, faceResult := range msg.RuleResult["face"].([]structure.TargetResult) { |
| | | faces = hebingFace(faces, faceResult) |
| | | } |
| | | return faces |
| | |
| | | return nil |
| | | } |
| | | } |
| | | func hebingFace(faces []*FaceAndRules, faceResult structure.FaceResult) []*FaceAndRules { |
| | | func hebingFace(faces []*FaceAndRules, faceResult structure.TargetResult) []*FaceAndRules { |
| | | for _, arg := range faceResult.Args { |
| | | // 拿到每一张人脸 |
| | | //logger.Info("归置人脸时相似者的数量:", len(arg.Liker)) |
| | |
| | | CameraAddr string |
| | | TaskId string |
| | | Time string |
| | | Persons []structure.FaceResult |
| | | Persons []structure.TargetResult |
| | | DefenceLevel []int32 |
| | | } |
| | | // 把数据装配到label |
| | | func (label *Label)DataFormatToLabel(result structure.ResultMsg) { |
| | | logger.Debug("face结果:",result.RuleResult["face"] != nil && len(result.RuleResult["face"].([]structure.FaceResult)) > 0) |
| | | logger.Debug("face结果:",result.RuleResult["face"] != nil && len(result.RuleResult["face"].([]structure.TargetResult)) > 0) |
| | | logger.Debug("yolo结果:",result.RuleResult["yolo"] != nil && len(result.RuleResult["yolo"].([]structure.Result)) > 0) |
| | | if (result.RuleResult["face"] != nil && len(result.RuleResult["face"].([]structure.FaceResult)) > 0) || (result.RuleResult["yolo"] != nil && len(result.RuleResult["yolo"].([]structure.Result)) > 0) { // 得有人脸或者yolo规则才可以 |
| | | if (result.RuleResult["face"] != nil && len(result.RuleResult["face"].([]structure.TargetResult)) > 0) || (result.RuleResult["yolo"] != nil && len(result.RuleResult["yolo"].([]structure.Result)) > 0) { // 得有人脸或者yolo规则才可以 |
| | | |
| | | // 先判断一下数据带的规则标签是否有报警的可以推送的 |
| | | flag := false |
| | |
| | | flag = true |
| | | } |
| | | } |
| | | if flag || (result.RuleResult["face"] != nil && len(result.RuleResult["face"].([]structure.FaceResult)) > 0){ |
| | | if flag || (result.RuleResult["face"] != nil && len(result.RuleResult["face"].([]structure.TargetResult)) > 0){ |
| | | logger.Info("---------------标签过滤器赋值") |
| | | label.CameraId = result.Cid |
| | | camera, err := cache.GetCameraById(result.Cid) |
| | |
| | | label.CameraAddr = camera.Addr |
| | | label.TaskId = result.Tasklab.Taskid |
| | | label.Time = time.Now().Format("2006-01-02 15:04:05") |
| | | if result.RuleResult["face"] != nil && len(result.RuleResult["face"].([]structure.FaceResult)) > 0 { |
| | | for _,faceGroup := range result.RuleResult["face"].([]structure.FaceResult) { |
| | | if result.RuleResult["face"] != nil && len(result.RuleResult["face"].([]structure.TargetResult)) > 0 { |
| | | for _,faceGroup := range result.RuleResult["face"].([]structure.TargetResult) { |
| | | label.Persons = append(label.Persons,faceGroup) |
| | | label.DefenceLevel = append(label.DefenceLevel,faceGroup.AlarmLevel) |
| | | } |
| | |
| | | //logger.Info("--------------看看compareFlag的值和tableId和areaMap.args的长度:",compareFlag,tableIds,len(areaMap.args)) |
| | | for _, arg := range areaMap.Args { |
| | | arg.Liker = arg.Liker[0:0] |
| | | //logger.Info("清空之后看看之前打的人脸标签变了没:") |
| | | //if args.RuleResult["face"] != nil && len(args.RuleResult["face"].([]FaceResult)) > 0 { |
| | | // for _, faceResult := range args.RuleResult["face"].([]FaceResult) { |
| | | // for _,arg := range faceResult.Args { |
| | | // logger.Info("人员分值是:",arg.Score,"liker的数量为",arg.Liker) |
| | | // } |
| | | // } |
| | | //} |
| | | if compareFlag == 1 { |
| | | fillLiker(nil, threshold, arg) |
| | | } |
| | |
| | | if obj.Score >= threshold && float64(obj.Rects.Width*obj.Rects.Height) >= size && PgsInterPercent(areaPoints, obj.Rects, widthScale, heigthScale) >= intersectionper { |
| | | // 这步要备齐表达式里所需要的所有参数 |
| | | a.TargetNum++ |
| | | arg1 := structure.Arg{obj.Id,obj.Score, PgsInterPercent(areaPoints, obj.Rects, widthScale, heigthScale), float64(obj.Rects.Width * obj.Rects.Height), obj.IsYolo, obj.Rects, obj.Feature, obj.ThftRes, []*structure.BaseInfo{}} |
| | | arg1 := structure.Arg{obj.Id,obj.Score, PgsInterPercent(areaPoints, obj.Rects, widthScale, heigthScale), float64(obj.Rects.Width * obj.Rects.Height), obj.IsYolo, obj.Rects, obj.Feature, obj.ThftRes, []*structure.BaseInfo{},nil} |
| | | //logger.Println("放进去的arg:-------", arg1) |
| | | a.Args = append(a.Args, &arg1) |
| | | a.FilterData = append(a.FilterData, &arg1) |
| | |
| | | // 得到属于该摄像机的若干组任务的完整规则(跟每一条完整规则比较之后得出本张图像对于某个规则是否报警的结果。放进map,比如本帧图像的id,所碰撞成功的规则id) |
| | | args.RuleResult = make(map[string]interface{}) |
| | | args.RuleResult["yolo"] = []structure.Result{} |
| | | args.RuleResult["face"] = []structure.FaceResult{} |
| | | args.RuleResult["face"] = []structure.TargetResult{} |
| | | args.RuleResult["target"] = []structure.TargetResult{} |
| | | //logger.Warn("传进去之前是什么德行:",args.RuleResult["yolo"]) |
| | | if taskGroup != nil && len(taskGroup.GroupRules) > 0 { |
| | | // 先过独立,再过联动 |
| | |
| | | } |
| | | } |
| | | |
| | | func CallMiddleware(args *structure.SdkDatas,rule protomsg.GroupRule) (bool, string, string){ |
| | | func CallMiddleware(args *structure.SdkDatas,rule protomsg.GroupRule,lable *structure.Others,message *protomsg.SdkMessage) (bool, string, string){ |
| | | p,err := plugin.Open("./algorithm/middleware.so") |
| | | if err != nil { |
| | | panic(err) |
| | |
| | | if err1 != nil { |
| | | panic("没有找到中间件入口函数") |
| | | } |
| | | a,b,c := f.(func(args *structure.SdkDatas,rule protomsg.GroupRule)(bool, string, string))(args,rule) |
| | | a,b,c := f.(func(args *structure.SdkDatas,rule protomsg.GroupRule,label *structure.Others,message *protomsg.SdkMessage)(bool, string, string))(args,rule,lable,message) |
| | | return a,b,c |
| | | } |
| | | |
| | |
| | | sdkNames := "" |
| | | polygonId := "" |
| | | // 把一帧数据和一组规则发给算法部分,得出判断结果 |
| | | result,sdkNames,polygonId = CallMiddleware(args,*groupRule) |
| | | result,sdkNames,polygonId = CallMiddleware(args,*groupRule,&label,message) |
| | | |
| | | if result { |
| | | // 最后过持续时间等时间维度的条件 把时间规则位置调整到这个位置是为了缓存数据 |
| | |
| | | //logger.Info("-------------------yolo结果标签长度", len(args.RuleResult["yolo"].([]Result))) |
| | | } |
| | | if faceFlag { |
| | | args.RuleResult["face"] = append(args.RuleResult["face"].([]structure.FaceResult), structure.FaceResult{structure.Result{taskId, sdkNames, groupRule.GroupId, groupRule.DefenceState, groupRule.AlarmLevel, groupRule.GroupText, []structure.TargetInfo{}, polygonId, islink,label,}, faces}) |
| | | args.RuleResult["face"] = append(args.RuleResult["face"].([]structure.TargetResult), structure.TargetResult{structure.Result{taskId, sdkNames, groupRule.GroupId, groupRule.DefenceState, groupRule.AlarmLevel, groupRule.GroupText, []structure.TargetInfo{}, polygonId, islink,label,}, faces}) |
| | | //logger.Info("-------------------face结果标签", len(args.RuleResult["face"].([]FaceResult))) |
| | | labelTypes = append(labelTypes,1) |
| | | } |
| | |
| | | } |
| | | } |
| | | if k == "face" { |
| | | tempMap[k] = []structure.FaceResult{} |
| | | for _, res := range result.([]structure.FaceResult) { |
| | | tempMap[k] = append(tempMap[k].([]structure.FaceResult), res) |
| | | tempMap[k] = []structure.TargetResult{} |
| | | for _, res := range result.([]structure.TargetResult) { |
| | | tempMap[k] = append(tempMap[k].([]structure.TargetResult), res) |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | if k == "face" { |
| | | tempMap[k] = []structure.FaceResult{} |
| | | for _, res := range result.([]structure.FaceResult) { |
| | | tempMap[k] = append(tempMap[k].([]structure.FaceResult), res) |
| | | tempMap[k] = []structure.TargetResult{} |
| | | for _, res := range result.([]structure.TargetResult) { |
| | | tempMap[k] = append(tempMap[k].([]structure.TargetResult), res) |
| | | } |
| | | } |
| | | } |
| | |
| | | args.RuleResult["yolo"].([]structure.Result)[i].Others.LinkCache = label.LinkCache |
| | | } |
| | | } |
| | | for i := 0; i < len(args.RuleResult["face"].([]structure.FaceResult)); i++ { |
| | | if args.RuleResult["face"].([]structure.FaceResult)[i].RuleGroupId == groupRule.GroupId { // 把联动数据追加上 |
| | | args.RuleResult["face"].([]structure.FaceResult)[i].Others.LinkCache = label.LinkCache |
| | | for i := 0; i < len(args.RuleResult["face"].([]structure.TargetResult)); i++ { |
| | | if args.RuleResult["face"].([]structure.TargetResult)[i].RuleGroupId == groupRule.GroupId { // 把联动数据追加上 |
| | | args.RuleResult["face"].([]structure.TargetResult)[i].Others.LinkCache = label.LinkCache |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | if val == 1 { |
| | | if len(args.RuleResult["face"].([]structure.FaceResult)) >= 1 { |
| | | lens := len(args.RuleResult["face"].([]structure.FaceResult))-1 |
| | | args.RuleResult["face"] = args.RuleResult["face"].([]structure.FaceResult)[0:lens] |
| | | if len(args.RuleResult["face"].([]structure.TargetResult)) >= 1 { |
| | | lens := len(args.RuleResult["face"].([]structure.TargetResult))-1 |
| | | args.RuleResult["face"] = args.RuleResult["face"].([]structure.TargetResult)[0:lens] |
| | | } |
| | | } |
| | | } |
| | |
| | | logger.Info("定时器执行单元", time.Now().Unix()) |
| | | fmt.Println("定时器执行单元", time.Now().Unix()) |
| | | // 每秒钟计数器池子里所有的计数器元素都减一,减到0的是该报警的 |
| | | // 图片定时器系列 |
| | | rw.Lock() |
| | | for k, timeEle := range TimeEleList { |
| | | if timeEle.N > 0 { |
| | | timeEle.N = timeEle.N - 1 |
| | | logger.Error("-------------------------------------打印定时器计数元素当前值-----------------------------------------:", timeEle.N) |
| | | logger.Debug("-------------------------------------打印定时器计数元素当前值-----------------------------------------:", timeEle.N) |
| | | } |
| | | if timeEle.GroupId != "" && timeEle.N == 0 { |
| | | // 说明是联动任务的时间窗口 到点儿了该销毁了,再来了再创建 |
| | |
| | | } |
| | | } |
| | | rw.Unlock() |
| | | // 目标定时器系列 |
| | | for _,cameraArea := range structure.StaticMap { |
| | | for _,obj := range cameraArea.Targets { |
| | | if obj.N > 0 { |
| | | obj.N = obj.N - 1 |
| | | } |
| | | } |
| | | } |
| | | case stop := <-stopChan: |
| | | if stop { |
| | | logger.Info("定时器结束") |
New file |
| | |
| | | package structure |
| | | |
| | | //个体静止算法 |
| | | var StaticMap = make(map[string]*CameraArea) |
| | | |
| | | type CameraArea struct { |
| | | Targets []*Obj |
| | | } |
| | | type Obj struct { |
| | | Id uint64 |
| | | Location Rect |
| | | N int // 计数器当前值 |
| | | InitN int // 计数器初始值 |
| | | BufferFlag int // 缓冲容错位 连续n帧false才为false |
| | | AlarmFlag bool |
| | | CacheSdkData ResultMsg // 定时器的缓存数据 持续时间类的开启定时器时要缓存一帧 |
| | | } |
| | |
| | | Feature []byte |
| | | ThftRes protomsg.ThftResult |
| | | Liker []*BaseInfo |
| | | CacheData ResultMsg |
| | | } |
| | | |
| | | // 每个区域内的图片数据集合 |
| | |
| | | |
| | | type ResultMsg struct { |
| | | *protomsg.SdkMessage |
| | | RuleResult map[string]interface{} // 过完规则后打的标签 face: []FaceResult, yolo: []Result |
| | | RuleResult map[string]interface{} // 过完规则后打的标签 face: []FaceResult, yolo: []Result target |
| | | } |
| | | |
| | | // 过规则库打上的标签 |
| | |
| | | } |
| | | |
| | | // 过规则库打上的标签 |
| | | type FaceResult struct { |
| | | type TargetResult struct { |
| | | Result |
| | | Args []Arg |
| | | } |
| | |
| | | return |
| | | } |
| | | |
| | | func DrawPolygonOnImageForFace(cameraId string, img protomsg.Image, results []structure.FaceResult,url string) (maps map[string]interface{}, err0 error) { |
| | | func DrawPolygonOnImageForFace(cameraId string, img protomsg.Image, results []structure.TargetResult,url string) (maps map[string]interface{}, err0 error) { |
| | | |
| | | rook, _ := gocv.NewMatFromBytes(int(img.Height), int(img.Width), gocv.MatTypeCV8UC3, img.Data) |
| | | //rook := gocv.IMRead("/home/user/workspace/ruleprocess/util/105.jpg",gocv.IMReadColor) |