From c16cab7461123245b06de496a92462a7f9e1f1d0 Mon Sep 17 00:00:00 2001 From: panlei <2799247126@qq.com> Date: 星期六, 02 十一月 2019 17:22:14 +0800 Subject: [PATCH] --- --- main.go | 220 +++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 175 insertions(+), 45 deletions(-) diff --git a/main.go b/main.go index 6b5506e..1969eb4 100644 --- a/main.go +++ b/main.go @@ -1,19 +1,71 @@ package main import ( - "fmt" - "ruleprocess/ruleserver" - "sync" - "analysis/work" + "basic.com/dbapi.git" "basic.com/pubsub/protomsg.git" "basic.com/valib/deliver.git" + "net/http" + _ "net/http/pprof" + "ruleprocess/insertdata" + "ruleprocess/labelFilter" + "ruleprocess/structure" + "ruleprocess/util" + "time" + + "basic.com/valib/logger.git" + //"bufio" + //"bytes" + "flag" + "fmt" "github.com/golang/protobuf/proto" + "github.com/spf13/viper" + //"gocv.io/x/gocv" + //"image" + //"image/color" + //"os" + "ruleprocess/cache" + "ruleprocess/ruleserver" + "sync" ) +var dbIp = flag.String("dbIp", "127.0.0.1", "dbserver ip") +var dbPort = flag.Int("dbPort", 8001, "default dbPort=8001") +var surveyPort = flag.Int("surveyPort", 40007, "survey port") //蹇冭烦 +var pubPort = flag.Int("pubPort", 50007, "pubsub port") //鏁版嵁鏇存柊 +var initchan = make(chan bool) +var env = flag.String("env","pro","env set") + +func init() { + flag.Parse() + // 鏃ュ織鍒濆鍖� + + insertdata.Init(*env) + var logFile = "./logger/" + if viper.GetString("LogBasePath") != "" { + logFile = viper.GetString("LogBasePath") + } + logFile = logFile + "ruleprocess.log" + fmt.Println("鏃ュ織鍦板潃锛�",logFile) + logger.Config(logFile, logger.DebugLevel) + logger.SetSaveDays(7) + logger.Info("鏃ュ織鍒濆鍖栨垚鍔燂紒") +} func main() { + //fmt.Println("缂撳瓨鍒濆鍖栧畬鎴�",<- initchan)//dbserver鍒濆鍖栧畬姣� + go func() { + http.ListenAndServe("0.0.0.0:8899",nil) + }() + flag.Parse() wg := sync.WaitGroup{} - wg.Add(1) + wg.Add(3) + + dbapi.Init(*dbIp, *dbPort) + go cache.Init(initchan, *dbIp, *surveyPort, *pubPort) + logger.Info("cache init completed!!!", <-initchan) //dbserver鍒濆鍖栧畬姣� + ruleserver.Init() + labelFilter.Init() go ruleserver.TimeTicker() + go ruleserver.StartServer() nReciever("ipc:///tmp/sdk-2-rules-process.ipc", deliver.PushPull, 1) wg.Wait() } @@ -26,34 +78,60 @@ var msg []byte var err error - + //msgChan := make(chan []byte,100) for { select { // case <-ctx.Done(): // return default: msg, err = c.Recv() + //msgChan <- msg if err != nil { + logger.Info("recv error : ", err) fmt.Println("recv error : ", err) continue } else { - arg := ruleserver.ArgsFromSdk{} - m := paramFormat(msg,&arg) - fmt.Println("瑙f瀽鍑烘潵鐨勬暟鎹細",arg) - ruleserver.MainJudge(&arg) - // 鎶奱rg閲岀殑鎵撶殑鏍囩鎷垮嚭鏉ョ粰m鍐嶅皝瑁呬竴灞� - resultMag := ruleserver.ResultMsg{SdkMessage: m, RuleResult: arg.RuleResult} - fmt.Println("鎵撳畬鏍囩鍚庣殑缁撴灉锛�",resultMag) + //runtime.GOMAXPROCS(runtime.NumCPU()) + //logger.Debug("浣跨敤鐨刢pu涓暟锛�",runtime.NumCPU()) + //go func(msg []byte) { + logger.Debug("褰撳墠鏃堕棿鎴筹細", time.Now().Unix()) + arg := structure.SdkDatas{} + //paramFormat(msg, &arg) + start := time.Now() + m := paramFormat(msg, &arg) + // 杩涜瑙勫垯澶勭悊鍒ゆ柇(鎵撲笂瑙勫垯鐨勬爣绛�) + ruleserver.Judge(&arg, &m) // 鎶妔dkMessage浼犺繘鍘伙紝鏂逛究缂撳瓨鏁版嵁鏃舵嫾鍑轰竴涓猺esultMag + // 鎶奱rg閲岀殑鎵撶殑鏍囩鎷垮嚭鏉ョ粰m鍐嶅皝瑁呬竴灞� + resultMsg := structure.ResultMsg{SdkMessage: &m, RuleResult: arg.RuleResult} + logger.Debug("瑙勫垯鍒ゆ柇瀹屾墍鐢ㄦ椂闂达細", time.Since(start)) + // 灏嗘墦瀹屾爣绛剧殑鏁版嵁鎻掑叆鍒癊S + insertdata.InsertToEs(resultMsg) + logger.Debug("鎻掑叆瀹孍s鎵�鐢ㄦ椂闂达細", time.Since(start)) + //浜嬩欢鎺ㄩ�� + labelFilter.PushSomthing(resultMsg) + //}(msg) } } } } -// 灏嗗閮ㄤ紶杩涙潵鐨剆dk鏁版嵁鍖呰В鎴� ArgsFromSdk -func paramFormat(msg []byte,arg *ruleserver.ArgsFromSdk) protomsg.SdkMessage { + +// 灏嗗閮ㄤ紶杩涙潵鐨剅ect锛坱op,bottom,left,right锛夎浆鍖栦负鑷繁鍐呴儴鐨剅ect(left top width height) +func rectFormat(rcobj *protomsg.Rect) structure.Rect { + //logger.Info("++++++++++++++++++++++++++++++鏀跺埌鐨剏olo鐨勫尯鍩熷潗鏍囷細",rcobj) + rect := structure.Rect{} + rect.X = float64(rcobj.Left) + rect.Y = float64(rcobj.Top) + rect.Width = float64(rcobj.Right - rcobj.Left) + rect.Height = float64(rcobj.Bottom - rcobj.Top) + return rect +} + +// 灏嗗閮ㄤ紶杩涙潵鐨剆dk鏁版嵁鍖呰В鎴� SdkDatas +func paramFormat(msg []byte, args *structure.SdkDatas) protomsg.SdkMessage { defer func() { - if err := recover();err != nil{ - fmt.Println("瑙e寘杩囩▼鐨勯敊璇�",err.(string)) + if err := recover(); err != nil { + logger.Info("瑙e寘杩囩▼鐨勫紓甯告崟鑾�", err.(string)) } }() @@ -63,44 +141,96 @@ if err != nil { panic("瑙f瀽msg鏃跺嚭鐜伴敊璇�") } - arg.CameraId = m.Cid - arg.TaskId = m.Tasklab.Taskid - bdata,err := UnCompress(m.Data) + + // 鍏堣繘琛屼竴涓嬭拷韪� + ruleserver.FaceIsSame(&m) + args.CameraId = m.Cid + args.TaskId = m.Tasklab.Taskid + // 鎶婂浘鐗囩殑浜岃繘鍒惰В鍘嬬缉杩涜鐢绘鍦ㄥ帇缂╁洖鍘� + bdata, err := util.UnCompress(m.Data) if err != nil { panic("瑙e帇缂╁浘鐗囨椂鍑虹幇閿欒") } i := protomsg.Image{} err = proto.Unmarshal(bdata, &i) - arg.ImageWidth = int(i.Width) - arg.ImageHeight = int(i.Height) - // 鏆傛椂鍐欐锛宻dk杩樻病鏈夎繖淇╃畻娉� - arg.KeepRight = false - arg.IsStatic = false - fmt.Println("浠巑ongos涓嬁鍒扮殑鏁版嵁鍖呴暱搴︿负锛�", len(msg)) - for _, sdkinfo := range m.Tasklab.Sdkinfos { + logger.Info("鎺ュ埌鏁版嵁锛屾憚鍍忔満涓猴細", m.Cid, "鍥剧墖鐨刬d涓猴細", i.Id) + //logger.Info("----------------鐪嬬湅鏈夊嚑涓畻娉曪細",len(m.Tasklab.Sdkinfos)) + for _, sdkinfo := range m.Tasklab.Sdkinfos { // yolo绠楁硶 if sdkinfo.Sdktype == "Yolo" { - yoloParam := protomsg.ParamYoloObj{} - err = proto.Unmarshal(sdkinfo.Sdkdata, &yoloParam) - if err != nil { - fmt.Println("瑙f瀽sdk鏁版嵁鏃跺嚭鐜伴敊璇�", err) + arg := structure.SdkData{} + arg.TaskId = m.Tasklab.Taskid + arg.IpcId = sdkinfo.Ipcid + arg.IsYolo = true + //bdata, err := util.UnCompress(m.Data) + //if err != nil { + // panic("瑙e帇缂╁浘鐗囨椂鍑虹幇閿欒") + //} + //i := protomsg.Image{} + //err = proto.Unmarshal(bdata, &i) + arg.ImageWidth = int(i.Width) + arg.ImageHeight = int(i.Height) + // 鏆傛椂鍐欐锛宻dk杩樻病鏈夎繖淇╃畻娉� + arg.KeepRight = false + arg.IsStatic = false + logger.Info("-----杩借釜涔嬪悗sdkinfo.Sdkdata鐨勯暱搴︿负锛�----", len(sdkinfo.Sdkdata)) + if len(sdkinfo.Sdkdata) > 1 { + // 澶т簬1鎵嶆湁鏁版嵁 + yoloParam := protomsg.ParamYoloObj{} + err = proto.Unmarshal(sdkinfo.Sdkdata, &yoloParam) + if err != nil { + logger.Info("瑙f瀽YOLO sdk鏁版嵁鏃跺嚭鐜伴敊璇�", err) + continue + } + var yoloNum int = 0 + for _, info := range yoloParam.Infos { + if info.Typ == 0 { + //logger.Debug("-------------yolo鐨勫潗鏍囨湁鍑犱釜",info.RcObj) + photoMap := structure.PhotoMap{Rects: rectFormat(info.RcObj), Score: float64(info.Prob) * 100, IsYolo: true,Id:info.ObjID} + arg.Photo = append(arg.Photo, photoMap) + yoloNum++ + } + } + logger.Info("--------------杩借釜涔嬪悗yolo鐨勪釜鏁帮細", yoloNum) + args.Sdkdata = append(args.Sdkdata, &arg) + } else { continue - } - for _, info := range yoloParam.Infos { - photoMap := ruleserver.PhotoMap{Rects:rectFormat(info.RcObj, i.Width, i.Height), Score: float64(info.Prob)} - arg.Photo = append(arg.Photo, photoMap) } } + if sdkinfo.Sdktype == "FaceDetect" { // 浜鸿劯妫�娴� + arg := structure.SdkData{} + arg.TaskId = m.Tasklab.Taskid + arg.IpcId = sdkinfo.Ipcid + arg.IsYolo = false + //bdata, err := util.UnCompress(m.Data) + //if err != nil { + // panic("瑙e帇缂╁浘鐗囨椂鍑虹幇閿欒") + //} + //i := protomsg.Image{} + //err = proto.Unmarshal(bdata, &i) + arg.ImageWidth = int(i.Width) + arg.ImageHeight = int(i.Height) + // 鏆傛椂鍐欐锛宻dk杩樻病鏈夎繖淇╃畻娉� + arg.KeepRight = false + arg.IsStatic = false + if len(sdkinfo.Sdkdata) > 1 { + faceParam := protomsg.ParamFacePos{} + err = proto.Unmarshal(sdkinfo.Sdkdata, &faceParam) + if err != nil { + logger.Info("瑙f瀽FACE sdk鏁版嵁鏃跺嚭鐜伴敊璇�", err) + continue + } + logger.Info("--------------杩借釜涔嬪悗浜鸿劯鐨勪釜鏁帮細", len(faceParam.Faces)) + for _, info := range faceParam.Faces { + //logger.Info("_______________________________________________绗竴娆$湅鐩镐技鍊硷細",info.Pos.FAngle.Confidence*100) + photoMap := structure.PhotoMap{Id: info.Pos.FaceID, Rects: rectFormat(info.Pos.RcFace), Score: float64(info.Pos.FAngle.Confidence * 100), IsYolo: false, ThftRes: *(info.Result), Feature: info.Feats} + arg.Photo = append(arg.Photo, photoMap) + } + args.Sdkdata = append(args.Sdkdata, &arg) + } else { + continue + } + } } return m -} - -// 灏嗗閮ㄤ紶杩涙潵鐨剅ect锛坱op,bottom,left,right锛夎浆鍖栦负鑷繁鍐呴儴鐨剅ect(left top width height) -func rectFormat(rcobj *protomsg.Rect, width int32, height int32) ruleserver.Rect { - rect := ruleserver.Rect{} - rect.X = float64(rcobj.Left) - rect.Y = float64(rcobj.Top) - rect.Width = float64(width - rcobj.Left - rcobj.Right) - rect.Height = float64(height - rcobj.Top - rcobj.Bottom) - return rect } -- Gitblit v1.8.0