| | |
| | | package ruleserver |
| | | |
| | | import ( |
| | | "basic.com/pubsub/cache.git/esutil" |
| | | bigCache "basic.com/pubsub/cache.git" |
| | | "basic.com/pubsub/cache.git/esutil" |
| | | "basic.com/pubsub/protomsg.git" |
| | | "encoding/json" |
| | | "fmt" |
| | | "github.com/go-yaml/yaml" |
| | | "io/ioutil" |
| | | "math" |
| | | "nanomsg.org/go-mangos" |
| | | "nanomsg.org/go-mangos/protocol/req" |
| | | "nanomsg.org/go-mangos/transport/tcp" |
| | | "os" |
| | | "ruleprocess/cache" |
| | | "ruleprocess/logger" |
| | | "github.com/golang/protobuf/proto" |
| | | "strconv" |
| | | "time" |
| | | ) |
| | | var serverIp,serverPort,dbTablePersons string |
| | | |
| | | var serverIp, serverPort, dbTablePersons string |
| | | var sock mangos.Socket |
| | | type conf struct { |
| | | PhotoUrl string `yaml:"photoUrl"` |
| | | VideoPersons string `yaml:"videoPersons"` |
| | |
| | | ServerPort string `yaml:"serverPort"` |
| | | DbTablePersons string `yaml:"dbTablePersons"` |
| | | } |
| | | |
| | | func Init() { |
| | | data, err := ioutil.ReadFile("./config/conf.yml") |
| | | if err != nil { |
| | |
| | | c := conf{} |
| | | //把yaml形式的字符串解析成struct类型 |
| | | yaml.Unmarshal(data, &c) |
| | | logger.Debug("配置文件的值为:",c) |
| | | logger.Debug("配置文件的值为:", c) |
| | | serverIp = c.ServerIp |
| | | serverPort = c.ServerPort |
| | | dbTablePersons = c.DbTablePersons |
| | |
| | | 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) |
| | | 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"` |
| | | 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"` |
| | | 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 { |
| | | func GetRuleGroup(cameraId string, taskId string) *protomsg.TaskGroupArgs { |
| | | all := cache.GetCameraTaskRulesByCameraId(cameraId) |
| | | //logger.Info("==================所有的任务的规则组:",all) |
| | | var taskGroup *protomsg.TaskGroupArgs |
| | | for _,task := range all { |
| | | var taskGroup *protomsg.TaskGroupArgs |
| | | for _, task := range all { |
| | | if task.TaskId == taskId { |
| | | taskGroup = task |
| | | } |
| | | } |
| | | logger.Debug("当前数据帧要匹配的规则组:-------------------------") |
| | | 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 |
| | | func Retain(f float32) (float64) { |
| | | 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) |
| | |
| | | } |
| | | return nil |
| | | } |
| | | |
| | | |
| | | // 根据传入的字符串得到其在一周内的索引 周一到周日分别对应1到7 |
| | | func getIndexOfWeek(weekday string) int { |
| | |
| | | // 将字符串格式的坐标序列化为Point格式 |
| | | func Json2points(areaPoints string) []Point { |
| | | var pts []Point |
| | | if areaPoints == "[]" || areaPoints == ""{ |
| | | if areaPoints == "[]" || areaPoints == "" { |
| | | logger.Error("=====================此区域为全部区域") |
| | | pts = append(pts, Point{0, 0}) |
| | | pts = append(pts, Point{0, 540}) |
| | |
| | | } |
| | | |
| | | // 给目标填充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 (arg *Arg) fillLiker(tableId []string, compareThreshold float32) { |
| | | //bytes := bigCache.GetComparePersonBaseInfo(tableId, arg.Feature, compareThreshold) |
| | | comArg := &protomsg.CompareArgs{ |
| | | TableIds:tableId, |
| | | FaceFeature:arg.Feature, |
| | | CompareThreshold:compareThreshold, |
| | | Source:false, |
| | | } |
| | | bytes := Push("tcp://192.168.1.182:40010",comArg,sock) |
| | | var scResult protomsg.SdkCompareResult |
| | | err1 := proto.Unmarshal(bytes, &scResult) |
| | | if err1 != nil { |
| | | logger.Error("getBaseInfo解压错误", err1) |
| | | } |
| | | //logger.Info("----------------------------------------map是", m) |
| | | ids := []string{} |
| | | for key,_ := range m { |
| | | ids = append(ids,key) |
| | | for _,key := range scResult.CompareResult { |
| | | ids = append(ids, key.Id) |
| | | } |
| | | logger.Info("------------------------------------------------------------------------------------------------------------------------") |
| | | logger.Info("=====================人员id的集合为:",ids) |
| | | //logger.Info("=====================人员id的集合为:",ids) |
| | | if len(ids) > 0 { |
| | | baseinfos, err1 := esutil.Dbpersoninfosbyid(ids,dbTablePersons,serverIp,serverPort) |
| | | baseinfos, err1 := esutil.Dbpersoninfosbyid(ids, dbTablePersons, serverIp, serverPort) |
| | | if err1 != nil { |
| | | logger.Error("查询底库人员信息出错", err1) |
| | | } |
| | | for _,baseinfo := range baseinfos { |
| | | for _, baseinfo := range baseinfos { |
| | | // 根据tableId查询底库信息给liker赋值 |
| | | logger.Info("---------看看每个底库人员的信息:",baseinfo.Id,baseinfo.PersonName) |
| | | var tableIds []string |
| | | tableIds = append(tableIds,baseinfo.TableId) // 虽然是传入数组返回数组的接口,但我按单个的使用了 |
| | | table,err := esutil.Dbtablefosbyid(tableIds,"dbtables",serverIp,serverPort) |
| | | if err != nil || len(table) == 0{ |
| | | logger.Error("根据id查询底库信息出错!",err,"--返回值长度为:",len(table)) |
| | | //logger.Info("---------看看每个底库人员的信息:",baseinfo.Id,baseinfo.PersonName) |
| | | tableIds := []string{} |
| | | tableIds = append(tableIds, baseinfo.TableId) // 虽然是传入数组返回数组的接口,但我按单个的使用了 |
| | | table, err := esutil.Dbtablefosbyid(tableIds, "dbtables", serverIp, serverPort) |
| | | if err != nil || len(table) == 0 { |
| | | logger.Error("根据id查询底库信息出错!", err, "--返回值长度为:", len(table)) |
| | | } |
| | | base := BaseInfo{TableId:baseinfo.TableId,TableName:table[0].TableName,BwType:table[0].BwType,CompareScore:Retain(m[baseinfo.Id]),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的对比值是多少:",base) |
| | | logger.Debug("看看这个base的对比值是多少:", Decimal(m[baseinfo.Id] * 100)) |
| | | base := BaseInfo{TableId: baseinfo.TableId, TableName: table[0].TableName, BwType: table[0].BwType, CompareScore: Decimal(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} |
| | | //os.Exit(1) |
| | | arg.Liker = append(arg.Liker, &base) |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | // 人脸比对 |
| | | func Compare(args *SdkDatas, groupRule *protomsg.GroupRule) { |
| | | compareFlag := 0 |
| | |
| | | if groupRule.Rules[j].SdkId == "812b674b-2375-4589-919a-5c1c3278a972" { // 配了人脸比对 |
| | | if groupRule.Rules[j].SdkArgAlias == "compareBase" && groupRule.Rules[j].SdkArgValue != "" { // 配了比对底库的参数但不是配的全部底库 |
| | | compareFlag = 2 |
| | | tableIds = append(tableIds,groupRule.Rules[j].SdkArgValue)// 最后会多一个tableId,切出来的数组取len-1就可以 |
| | | tableIds = append(tableIds, groupRule.Rules[j].SdkArgValue) // 最后会多一个tableId,切出来的数组取len-1就可以 |
| | | } |
| | | if groupRule.Rules[j].SdkArgAlias == "compareBase" && groupRule.Rules[j].SdkArgValue == "" { // 配的参数是比对全部底库 |
| | | compareFlag = 1 |
| | |
| | | logger.Info("没有配置对比底库参数") |
| | | return |
| | | } |
| | | logger.Debug("=====配置的阈值为:",threshold) |
| | | logger.Debug("=====配置的阈值为:", threshold) |
| | | for j := 0; j < len(groupRule.Rules); j++ { |
| | | if groupRule.Rules[j].SdkId == "812b674b-2375-4589-919a-5c1c3278a972" { |
| | | for _, sdkData := range args.Sdkdata { |
| | |
| | | for _, areaMap := range sdkData.AreaMapList { |
| | | // 拿区域中每个人脸特征值去对比,填充其liker |
| | | if groupRule.Rules[j].PolygonId == areaMap.areaId { |
| | | logger.Info("--------------看看compareFlag的值和tableId和areaMap.args的长度:",compareFlag,tableIds,len(areaMap.args)) |
| | | //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 { |
| | | arg.fillLiker(nil,threshold) |
| | | arg.fillLiker(nil, threshold) |
| | | } |
| | | if compareFlag == 2 { |
| | | arg.fillLiker(tableIds,threshold) |
| | | arg.fillLiker(tableIds, threshold) |
| | | } |
| | | logger.Info("-------------------成功给liker赋值,长度为:", len(arg.Liker)) |
| | | //logger.Info("-------------------成功给liker赋值,长度为:", len(arg.Liker)) |
| | | } |
| | | areaMap.filterData = areaMap.args |
| | | //logger.Info("=======第一次看args:",(areaMap.filterData)) |
| | |
| | | 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{}} |
| | | 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{}} |
| | | //logger.Println("放进去的arg:-------", 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 |
| | |
| | | areaMap.CountAreaObjs(arg) |
| | | arg.AreaMapList = append(arg.AreaMapList, &areaMap) |
| | | } |
| | | } |
| | | } |
| | | |
| | | func Push(url string,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 |
| | | } |
| | | //sock.AddTransport(ipc.NewTransport()) |
| | | sock.AddTransport(tcp.NewTransport()) |
| | | if err = sock.Dial(url); err != nil { |
| | | logger.Error("请求socket拨号失败: %s", err.Error()) |
| | | } |
| | | 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 |
| | | } |