| | |
| | | package ruleserver |
| | | |
| | | import ( |
| | | bigCache "basic.com/pubsub/cache.git" |
| | | "basic.com/pubsub/cache.git/esutil" |
| | | "basic.com/pubsub/protomsg.git" |
| | | "encoding/json" |
| | | "errors" |
| | | "fmt" |
| | | "github.com/go-yaml/yaml" |
| | | "github.com/golang/protobuf/proto" |
| | | "io/ioutil" |
| | | "math" |
| | | "nanomsg.org/go-mangos" |
| | | "nanomsg.org/go-mangos/protocol/req" |
| | | "nanomsg.org/go-mangos/transport/tcp" |
| | | "os" |
| | | "net" |
| | | "ruleprocess/cache" |
| | | "ruleprocess/logger" |
| | | "github.com/golang/protobuf/proto" |
| | | "ruleprocess/structure" |
| | | "strconv" |
| | | "time" |
| | | ) |
| | |
| | | logger.Error("查询本机信息失败!") |
| | | } |
| | | logger.Debug("本机信息和server信息:", localConfig, serverIp, serverPort) |
| | | bigCache.Init(dbTablePersons, serverIp, serverPort, localConfig.ServerId) |
| | | //bigCache.Init(dbTablePersons, serverIp, serverPort, localConfig.ServerId) |
| | | sock, err = req.NewSocket(); |
| | | if err != nil { |
| | | logger.Error("创建请求socket失败: %s", err.Error()) |
| | | } |
| | | } |
| | | |
| | | type BaseInfo struct { |
| | | TableId string `json:"tableId"` |
| | | TableName string `json:"tableName"` |
| | | BwType string `json:"bwType"` |
| | | CompareScore float64 `json:"compareScore"` |
| | | PersonId string `json:"personId"` |
| | | PersonName string `json:"personName"` |
| | | PersonPicUrl string `json:"personPicUrl"` |
| | | PhoneNum string `json:"phoneNum"` |
| | | Sex string `json:"sex"` |
| | | IdCard string `json:"idCard"` |
| | | MonitorLevel string `json:"monitorLevel"` |
| | | Content string `json:"content"` |
| | | } |
| | | |
| | | // 以摄像机id查出跟其相关的所有任务下的所有规则组 |
| | | func GetRuleGroup(cameraId string, taskId string) *protomsg.TaskGroupArgs { |
| | |
| | | taskGroup = task |
| | | } |
| | | } |
| | | logger.Debug("当前数据帧要匹配的规则组:-------------------------") |
| | | logger.Debug("当前数据帧要匹配的规则组:-------------------------","摄像机id:",cameraId,"任务id",taskId) |
| | | for _,ruleGroup := range taskGroup.GroupRules{ |
| | | logger.Info(ruleGroup.GroupText) |
| | | } |
| | |
| | | return value2 |
| | | } |
| | | // 取出某个时间规则的第几天的规则段集合 |
| | | func GetTimeById(id string, index int) []TimeRange { |
| | | func GetTimeById(id string, index int) []structure.TimeRange { |
| | | _, cameraTimeRule := cache.GetTimeRuleById(id) |
| | | var timeRangeList []day |
| | | var timeRangeList []structure.Day |
| | | err := json.Unmarshal([]byte(cameraTimeRule.TimeRule), &timeRangeList) |
| | | if err != nil { |
| | | logger.Error("取时间规则时反序列化错误!") |
| | |
| | | return 0 |
| | | } |
| | | |
| | | type TimeRange struct { |
| | | Start string `json:"start"` |
| | | End string `json:"end"` |
| | | } |
| | | type day struct { |
| | | Day int `json:"day"` // 标示当前星期几 |
| | | TimeRange []TimeRange `json:"time_range"` // 当天的几个时间段 |
| | | } |
| | | |
| | | |
| | | // 将字符串格式的坐标序列化为Point格式 |
| | | func Json2points(areaPoints string) []Point { |
| | | var pts []Point |
| | | func Json2points(areaPoints string) []structure.Point { |
| | | var pts []structure.Point |
| | | if areaPoints == "[]" || areaPoints == "" { |
| | | logger.Error("=====================此区域为全部区域") |
| | | pts = append(pts, Point{0, 0}) |
| | | pts = append(pts, Point{0, 540}) |
| | | pts = append(pts, Point{960, 540}) |
| | | pts = append(pts, Point{960, 0}) |
| | | pts = append(pts, structure.Point{0, 0}) |
| | | pts = append(pts, structure.Point{0, 540}) |
| | | pts = append(pts, structure.Point{960, 540}) |
| | | pts = append(pts, structure.Point{960, 0}) |
| | | } else { |
| | | err := json.Unmarshal([]byte(areaPoints), &pts) |
| | | if err != nil { |
| | |
| | | } |
| | | |
| | | // 给目标填充liker |
| | | func (arg *Arg) fillLiker(tableId []string, compareThreshold float32) { |
| | | func FillLiker(arg *structure.Arg,tableId []string, compareThreshold float32) { |
| | | //bytes := bigCache.GetComparePersonBaseInfo(tableId, arg.Feature, compareThreshold) |
| | | comArg := &protomsg.CompareArgs{ |
| | | TableIds:tableId, |
| | |
| | | CompareThreshold:compareThreshold, |
| | | Source:false, |
| | | } |
| | | bytes := Push("tcp://192.168.1.182:40010",comArg,sock) |
| | | serverIp, _ := GetLocalIP() |
| | | bytes := Push("tcp://"+serverIp+":40010",comArg,sock) |
| | | var scResult protomsg.SdkCompareResult |
| | | err1 := proto.Unmarshal(bytes, &scResult) |
| | | if err1 != nil { |
| | |
| | | if err != nil || len(table) == 0 { |
| | | logger.Error("根据id查询底库信息出错!", err, "--返回值长度为:", len(table)) |
| | | } |
| | | logger.Debug("看看这个base的对比值是多少:", Decimal(m[baseinfo.Id].CompareScore * 100)) |
| | | base := BaseInfo{TableId: baseinfo.TableId, TableName: table[0].TableName, BwType: table[0].BwType, CompareScore: Decimal(m[baseinfo.Id].CompareScore * 100), PersonId: baseinfo.Id, PersonName: baseinfo.PersonName, PersonPicUrl: baseinfo.PersonPicUrl, PhoneNum: baseinfo.PhoneNum, Sex: baseinfo.Sex, IdCard: baseinfo.IdCard, MonitorLevel: baseinfo.MonitorLevel, Content: baseinfo.Reserved} |
| | | logger.Debug("看看这个base的对比值是多少:", Decimal(m[baseinfo.Id].CompareScore)) |
| | | base := structure.BaseInfo{TableId: baseinfo.TableId, TableName: table[0].TableName, BwType: table[0].BwType, CompareScore: Decimal(m[baseinfo.Id].CompareScore), PersonId: baseinfo.Id, PersonName: baseinfo.PersonName, PersonPicUrl: baseinfo.PersonPicUrl, PhoneNum: baseinfo.PhoneNum, Sex: baseinfo.Sex, IdCard: baseinfo.IdCard, MonitorLevel: baseinfo.MonitorLevel, Content: baseinfo.Reserved} |
| | | //os.Exit(1) |
| | | arg.Liker = append(arg.Liker, &base) |
| | | } |
| | |
| | | } |
| | | |
| | | // 人脸比对 |
| | | func Compare(args *SdkDatas, groupRule *protomsg.GroupRule) { |
| | | func Compare(args *structure.SdkDatas, groupRule *protomsg.GroupRule) { |
| | | compareFlag := 0 |
| | | var tableIds []string |
| | | var threshold float32 = 0.5 // 默认阈值为0.5 |
| | | var threshold float32 = 50 // 默认阈值为50 |
| | | // 看看是否有只配人脸比对算法但没有配对比库的规则,如果有,则比对对象为全部底库 |
| | | for j := 0; j < len(groupRule.Rules); j++ { |
| | | if groupRule.Rules[j].SdkId == "812b674b-2375-4589-919a-5c1c3278a972" { // 配了人脸比对 |
| | |
| | | logger.Info("============================================进行人脸对比") |
| | | for _, areaMap := range sdkData.AreaMapList { |
| | | // 拿区域中每个人脸特征值去对比,填充其liker |
| | | if groupRule.Rules[j].PolygonId == areaMap.areaId { |
| | | if groupRule.Rules[j].PolygonId == areaMap.AreaId { |
| | | //logger.Info("--------------看看compareFlag的值和tableId和areaMap.args的长度:",compareFlag,tableIds,len(areaMap.args)) |
| | | for _, arg := range 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) { |
| | | if args.RuleResult["face"] != nil && len(args.RuleResult["face"].([]structure.FaceResult)) > 0 { |
| | | for _, faceResult := range args.RuleResult["face"].([]structure.FaceResult) { |
| | | for _,arg := range faceResult.Args { |
| | | logger.Info("人员分值是:",arg.Score,"liker的数量为",arg.Liker) |
| | | } |
| | | } |
| | | } |
| | | if compareFlag == 1 { |
| | | arg.fillLiker(nil, threshold) |
| | | FillLiker(arg,nil, threshold) |
| | | } |
| | | if compareFlag == 2 { |
| | | arg.fillLiker(tableIds, threshold) |
| | | FillLiker(arg,tableIds, threshold) |
| | | } |
| | | //logger.Info("-------------------成功给liker赋值,长度为:", len(arg.Liker)) |
| | | } |
| | | areaMap.filterData = areaMap.args |
| | | areaMap.FilterData = areaMap.Args |
| | | //logger.Info("=======第一次看args:",(areaMap.filterData)) |
| | | } |
| | | //logger.Info("-------------------------------人脸对比之后的目标数量",len(areaMap.args)) |
| | |
| | | } |
| | | |
| | | // 计算区域内的目标数量以及将相似度、占比、尺寸等打包 |
| | | func (a *AreaMap) CountAreaObjs(arg *SdkData) { |
| | | func CountAreaObjs(a *structure.AreaMap,arg *structure.SdkData) { |
| | | |
| | | a.targetNum = 0 |
| | | a.TargetNum = 0 |
| | | threshold := 0.0 // 相似度 |
| | | intersectionper := 0.2 // 占比 |
| | | size := 0.0 // 尺寸 |
| | | |
| | | areaPoints := Json2points(a.areaJson) |
| | | areaPoints := Json2points(a.AreaJson) |
| | | widthScale := float64(arg.ImageWidth / 960) |
| | | heigthScale := float64(arg.ImageHeight / 540) |
| | | for _, obj := range arg.Photo { |
| | | //logger.Info("------------------看看sdkData:", arg.SdkName, "的Photo数据----------------", obj, "----顺便看看占比-----:", PgsInterPercent(areaPoints, obj.Rects, widthScale, heigthScale)) |
| | | if threshold <= obj.Score && size <= float64(obj.Rects.Width*obj.Rects.Height) && intersectionper <= PgsInterPercent(areaPoints, obj.Rects, widthScale, heigthScale) { |
| | | // 这步要备齐表达式里所需要的所有参数 |
| | | a.targetNum++ |
| | | arg1 := 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, []*BaseInfo{}} |
| | | 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{}} |
| | | //logger.Println("放进去的arg:-------", arg1) |
| | | a.args = append(a.args, &arg1) |
| | | a.filterData = append(a.filterData, &arg1) |
| | | a.Args = append(a.Args, &arg1) |
| | | a.FilterData = append(a.FilterData, &arg1) |
| | | } |
| | | } |
| | | //logger.Info("区域是:",areaPoints,"区域内目标数量为:",a.targetNum,"---",len(a.filterData)) |
| | | a.time = time.Unix(time.Now().Unix(), 0).String()[11:16] |
| | | a.keepRight = arg.KeepRight |
| | | a.isStatic = arg.IsStatic |
| | | logger.Info("区域是:",areaPoints,"装配完数据区域内目标数量为:",a.TargetNum) |
| | | a.Time = time.Unix(time.Now().Unix(), 0).String()[11:16] |
| | | a.KeepRight = arg.KeepRight |
| | | a.IsStatic = arg.IsStatic |
| | | //logger.Println("--------------------看看区域数据:",*a) |
| | | } |
| | | |
| | | // 把sdk从数据帧上提取的按照区域分类归置 |
| | | func SdkDataFormat(cameraId string, arg *SdkData, cameraPolygons []protomsg.CameraPolygon) { |
| | | func SdkDataFormat(cameraId string, arg *structure.SdkData, cameraPolygons []protomsg.CameraPolygon) { |
| | | logger.Info("==================================本sdkData中解出来的目标数据=======================================") |
| | | for _, photo := range arg.Photo { |
| | | logger.Info("--------解析出来的数据---", cameraId, arg.IpcId, photo.Rects, photo.Score) |
| | | } |
| | | for _, polygon := range cameraPolygons { |
| | | //logger.Println("++++++在这儿看一下区域啊:", polygon.Polygon) |
| | | areaMap := AreaMap{cameraId: cameraId, areaId: polygon.Id, areaJson: polygon.Polygon, triggerLine: polygon.TriggerLine, directionLine: polygon.DirectionLine} |
| | | areaMap := structure.AreaMap{CameraId: cameraId, AreaId: polygon.Id, AreaJson: polygon.Polygon, TriggerLine: polygon.TriggerLine, DirectionLine: polygon.DirectionLine} |
| | | // 为每个摄像机区域填充数据 |
| | | areaMap.CountAreaObjs(arg) |
| | | CountAreaObjs(&areaMap,arg) |
| | | arg.AreaMapList = append(arg.AreaMapList, &areaMap) |
| | | } |
| | | } |
| | |
| | | //bytes := []byte("ndfasojdfaidsos") |
| | | if err = sock.Send(bytes); err != nil { |
| | | logger.Error("推送socket发送数据失败: %s", err.Error()) |
| | | os.Exit(1) |
| | | //os.Exit(1) |
| | | } |
| | | if msg, err = sock.Recv(); err != nil { |
| | | logger.Error("接收响应失败: %s", err.Error()) |
| | | os.Exit(1) |
| | | //os.Exit(1) |
| | | } |
| | | logger.Debug("数据推送成功!收到响应,数据长度为:",len(msg)) |
| | | return msg |
| | | } |
| | | |
| | | // 获取本机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 |
| | | } |