| | |
| | | package ruleserver |
| | | |
| | | import ( |
| | | bigCache "basic.com/pubsub/cache.git" |
| | | "basic.com/pubsub/cache.git/esutil" |
| | | "basic.com/dbapi.git" |
| | | "basic.com/pubsub/protomsg.git" |
| | | "encoding/json" |
| | | "errors" |
| | | "fmt" |
| | | "github.com/go-yaml/yaml" |
| | | "io/ioutil" |
| | | "github.com/golang/protobuf/proto" |
| | | "math" |
| | | "nanomsg.org/go-mangos" |
| | | "nanomsg.org/go-mangos/protocol/req" |
| | | "nanomsg.org/go-mangos/transport/tcp" |
| | | "net" |
| | | "ruleprocess/cache" |
| | | "ruleprocess/logger" |
| | | "basic.com/valib/logger.git" |
| | | "ruleprocess/structure" |
| | | "strconv" |
| | | "time" |
| | | ) |
| | | |
| | | var serverIp, serverPort, dbTablePersons string |
| | | |
| | | var sock mangos.Socket |
| | | type conf struct { |
| | | PhotoUrl string `yaml:"photoUrl"` |
| | | VideoPersons string `yaml:"videoPersons"` |
| | |
| | | } |
| | | |
| | | func Init() { |
| | | data, err := ioutil.ReadFile("./config/conf.yml") |
| | | if err != nil { |
| | | fmt.Println("读取配置文件出错--", err) |
| | | logger.Error("读取配置文件出错--", err) |
| | | } |
| | | c := conf{} |
| | | //把yaml形式的字符串解析成struct类型 |
| | | yaml.Unmarshal(data, &c) |
| | | logger.Debug("配置文件的值为:", c) |
| | | serverIp = c.ServerIp |
| | | serverPort = c.ServerPort |
| | | dbTablePersons = c.DbTablePersons |
| | | localConfig, err1 := cache.GetServerInfo() |
| | | if err1 != nil { |
| | | logger.Error("查询本机信息失败!") |
| | | } |
| | | logger.Debug("本机信息和server信息:", localConfig, serverIp, serverPort) |
| | | bigCache.Init(dbTablePersons, serverIp, serverPort, localConfig.ServerId) |
| | | //logger.Debug("本机信息和server信息:", localConfig, serverIp, serverPort) |
| | | //bigCache.Init(dbTablePersons, serverIp, serverPort, localConfig.ServerId) |
| | | go Push1() |
| | | } |
| | | |
| | | var sender chan *protomsg.CompareArgs = make(chan *protomsg.CompareArgs) |
| | | var receiver chan []byte = make(chan []byte) |
| | | type BaseInfo struct { |
| | | TableId string `json:"tableId"` |
| | | TableName string `json:"tableName"` |
| | |
| | | taskGroup = task |
| | | } |
| | | } |
| | | return taskGroup |
| | | logger.Debug("当前数据帧要匹配的规则组:-------------------------","摄像机id:",cameraId,"任务id",taskId) |
| | | if taskGroup == nil { |
| | | return nil |
| | | } else { |
| | | for _,ruleGroup := range taskGroup.GroupRules{ |
| | | logger.Info(ruleGroup.GroupText) |
| | | } |
| | | return taskGroup |
| | | } |
| | | } |
| | | |
| | | // 根据摄像机id拿到摄像机所有区域 |
| | | func GetPolygons(cameraId string) []protomsg.CameraPolygon { |
| | | var cameraPolygons []protomsg.CameraPolygon |
| | | cameraPolygons = cache.GetPolygonsByCameraId(cameraId) |
| | | //logger.Debug("------=======查看下全部区域:",cameraPolygons) |
| | | logger.Debug("------=======查看下全部区域:",cameraPolygons) |
| | | return cameraPolygons |
| | | } |
| | | |
| | | // 保留四位小数 |
| | | func Retain(f float32) (float64) { |
| | | s := strconv.FormatFloat(float64(f), 'f', 4, 64) |
| | | v, err := strconv.ParseFloat(s, 64) |
| | | if err != nil { |
| | | logger.Error("保留四位小数转换错误") |
| | | } |
| | | // float32与float64互转会有位数保留不准的问题 |
| | | return v |
| | | value1, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", float64(f)), 64) |
| | | return value1 |
| | | } |
| | | |
| | | func Decimal(value float32) float64 { |
| | | value1, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", float64(value)), 64) |
| | | logger.Info("初步保留两位成str:",value1) |
| | | n10 := math.Pow10(4) |
| | | value2 := math.Trunc((value1+0.5/n10)*n10) / n10 |
| | | logger.Info("初步保留两位成str::::",value2) |
| | | return value2 |
| | | } |
| | | // 取出某个时间规则的第几天的规则段集合 |
| | | func GetTimeById(id string, index int) []TimeRange { |
| | | _, cameraTimeRule := cache.GetTimeRuleById(id) |
| | |
| | | } |
| | | |
| | | // 给目标填充liker |
| | | func (arg *Arg) fillLiker(tableId []string, compareThreshold float32) { |
| | | bytes := bigCache.GetComparePersonBaseInfo(tableId, arg.Feature, compareThreshold) |
| | | var m map[string]float32 |
| | | err1 := json.Unmarshal(bytes, &m) |
| | | func fillLiker(tableId []string, compareThreshold float32,arg *structure.Arg) { |
| | | //bytes := bigCache.GetComparePersonBaseInfo(tableId, arg.Feature, compareThreshold) |
| | | comArg := &protomsg.CompareArgs{ |
| | | TableIds:tableId, |
| | | FaceFeature:arg.Feature, |
| | | CompareThreshold:compareThreshold, |
| | | Source:false, |
| | | } |
| | | |
| | | bytes := getCompareMsg(comArg) |
| | | //bytes := []byte{} |
| | | var scResult protomsg.SdkCompareResult |
| | | err1 := proto.Unmarshal(bytes, &scResult) |
| | | if err1 != nil { |
| | | logger.Error("getBaseInfo解压错误", err1) |
| | | return |
| | | } |
| | | //logger.Info("----------------------------------------map是", m) |
| | | ids := []string{} |
| | | for key, _ := range m { |
| | | ids = append(ids, key) |
| | | m := make(map[string]*protomsg.SdkCompareEach) |
| | | for _,key := range scResult.CompareResult { |
| | | ids = append(ids, key.Id) |
| | | m[key.Id] = key |
| | | } |
| | | logger.Info("------------------------------------------------------------------------------------------------------------------------") |
| | | //logger.Info("=====================人员id的集合为:",ids) |
| | | if len(ids) > 0 { |
| | | baseinfos, err1 := esutil.Dbpersoninfosbyid(ids, dbTablePersons, serverIp, serverPort) |
| | | var dbApi dbapi.DbPersonApi |
| | | baseinfos, err1 := dbApi.Dbpersoninfosbyid(ids) |
| | | //baseinfos, err1 := esutil.Dbpersoninfosbyid(ids, dbTablePersons, serverIp, serverPort) |
| | | if err1 != nil { |
| | | logger.Error("查询底库人员信息出错", err1) |
| | | } |
| | | var dtapi dbapi.DbTableApi |
| | | for _, baseinfo := range baseinfos { |
| | | // 根据tableId查询底库信息给liker赋值 |
| | | //logger.Info("---------看看每个底库人员的信息:",baseinfo.Id,baseinfo.PersonName) |
| | | var tableIds []string |
| | | tableIds := []string{} |
| | | tableIds = append(tableIds, baseinfo.TableId) // 虽然是传入数组返回数组的接口,但我按单个的使用了 |
| | | table, err := esutil.Dbtablefosbyid(tableIds, "dbtables", serverIp, serverPort) |
| | | table, err := dtapi.DbtablesById(tableIds) |
| | | //table, err := esutil.Dbtablefosbyid(tableIds, "dbtables", serverIp, serverPort) |
| | | if err != nil || len(table) == 0 { |
| | | logger.Error("根据id查询底库信息出错!", err, "--返回值长度为:", len(table)) |
| | | } |
| | | logger.Debug("看看这个base的对比值是多少:",Retain(m[baseinfo.Id]) * 100) |
| | | base := BaseInfo{TableId: baseinfo.TableId, TableName: table[0].TableName, BwType: table[0].BwType, CompareScore: Retain(m[baseinfo.Id]) * 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" { // 配了人脸比对 |
| | |
| | | if groupRule.Rules[j].SdkArgAlias == "compareBase" && groupRule.Rules[j].SdkArgValue == "" { // 配的参数是比对全部底库 |
| | | compareFlag = 1 |
| | | } |
| | | if groupRule.Rules[j].SdkArgAlias == "threshold" { |
| | | if groupRule.Rules[j].SdkArgAlias == "cmpThreshold" { |
| | | v2, err := strconv.ParseFloat(groupRule.Rules[j].SdkArgValue, 32) |
| | | if err != nil { |
| | | logger.Error("string转float32失败!") |
| | |
| | | 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) { |
| | | // for _,arg := range faceResult.Args { |
| | | // logger.Info("人员分值是:",arg.Score,"liker的数量为",arg.Liker) |
| | | // } |
| | | // } |
| | | //} |
| | | if compareFlag == 1 { |
| | | arg.fillLiker(nil, threshold) |
| | | fillLiker(nil, threshold, arg) |
| | | } |
| | | if compareFlag == 2 { |
| | | arg.fillLiker(tableIds, threshold) |
| | | fillLiker(tableIds, threshold, arg) |
| | | } |
| | | //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 |
| | | threshold := 0.0 // 相似度 |
| | | a.TargetNum = 0 |
| | | threshold := 80.0 // 相似度 |
| | | intersectionper := 0.2 // 占比 |
| | | size := 0.0 // 尺寸 |
| | | |
| | | areaPoints := Json2points(a.areaJson) |
| | | areaPoints := Json2points(a.AreaJson) |
| | | logger.Info("看看图片的width和height:",arg.ImageWidth,arg.ImageHeight) |
| | | 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.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) |
| | | } |
| | | } |
| | | a.time = time.Unix(time.Now().Unix(), 0).String()[11:16] |
| | | a.keepRight = arg.KeepRight |
| | | a.isStatic = arg.IsStatic |
| | | logger.Info("区域是:",areaPoints,"区域内目标数量为:",a.TargetNum,"---",len(a.FilterData)) |
| | | for _,tar := range a.FilterData { |
| | | logger.Info("具体目标的坐标为",tar.Id,tar.Location) |
| | | } |
| | | 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) |
| | | } |
| | | } |
| | | |
| | | func Push(data *protomsg.CompareArgs,sock mangos.Socket) []byte{ |
| | | //var sock mangos.Socket |
| | | var err error |
| | | var msg []byte |
| | | |
| | | //if sock, err = req.NewSocket(); err != nil { |
| | | // logger.Error("创建请求socket失败: %s", err.Error()) |
| | | //} |
| | | errSize := sock.SetOption(mangos.OptionMaxRecvSize,5*1024*1024) |
| | | if errSize != nil { |
| | | logger.Error("Failed set MaxRecvSize: %v", err) |
| | | return nil |
| | | } |
| | | serverIP, _ := GetLocalIP() |
| | | if err = sock.Dial("tcp://"+serverIP+":4010"); err != nil { |
| | | logger.Error("请求socket拨号失败: %s", err.Error()) |
| | | } |
| | | //sock.AddTransport(ipc.NewTransport()) |
| | | sock.AddTransport(tcp.NewTransport()) |
| | | |
| | | logger.Info("序列化数据") |
| | | bytes,err1 := proto.Marshal(data) |
| | | logger.Info("数据长度为:",len(bytes)) |
| | | if err1 != nil { |
| | | logger.Info("序列化失败:",err1) |
| | | } |
| | | logger.Debug("推送数据") |
| | | //bytes := []byte("ndfasojdfaidsos") |
| | | if err = sock.Send(bytes); err != nil { |
| | | logger.Error("推送socket发送数据失败: %s", err.Error()) |
| | | //os.Exit(1) |
| | | } |
| | | if msg, err = sock.Recv(); err != nil { |
| | | logger.Error("接收响应失败: %s", err.Error()) |
| | | //os.Exit(1) |
| | | } |
| | | logger.Debug("数据推送成功!收到响应,数据长度为:",len(msg)) |
| | | return msg |
| | | } |
| | | |
| | | |
| | | func Push1(){ |
| | | //var sock mangos.Socket |
| | | var err error |
| | | var msg []byte |
| | | |
| | | if sock, err = req.NewSocket(); err != nil { |
| | | logger.Error("创建请求socket失败: %s", err.Error()) |
| | | } |
| | | errSize := sock.SetOption(mangos.OptionMaxRecvSize,5*1024*1024) |
| | | if errSize != nil { |
| | | logger.Error("Failed set MaxRecvSize: %v", err) |
| | | } |
| | | //sock.AddTransport(ipc.NewTransport()) |
| | | sock.AddTransport(tcp.NewTransport()) |
| | | serverIP, _ := GetLocalIP() |
| | | if err = sock.Dial("tcp://"+serverIP+":4010"); err != nil { |
| | | logger.Error("请求socket拨号失败: %s", err.Error()) |
| | | } |
| | | logger.Info("序列化数据") |
| | | |
| | | for { |
| | | select { |
| | | // case <-ctx.Done(): |
| | | // return |
| | | case data := <- sender: |
| | | logger.Info("比对进程入参:",data.TableIds,data.CompareThreshold) |
| | | bytes,err1 := proto.Marshal(data) |
| | | logger.Info("数据长度为:",len(bytes)) |
| | | if err1 != nil { |
| | | logger.Info("序列化失败:",err1) |
| | | } |
| | | logger.Debug("推送数据") |
| | | //bytes := []byte("ndfasojdfaidsos") |
| | | if err = sock.Send(bytes); err != nil { |
| | | logger.Error("推送socket发送数据失败: %s", err.Error()) |
| | | //os.Exit(1) |
| | | } |
| | | if msg, err = sock.Recv(); err != nil { |
| | | logger.Error("接收响应失败: %s", err.Error()) |
| | | //os.Exit(1) |
| | | } |
| | | logger.Debug("数据推送成功!收到响应,数据长度为:",len(msg)) |
| | | receiver <- msg |
| | | default: |
| | | |
| | | } |
| | | } |
| | | } |
| | | func getCompareMsg(data *protomsg.CompareArgs) []byte{ |
| | | sender <- data |
| | | return <-receiver |
| | | } |
| | | // 获取本机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 |
| | | } |