From bc86eeeefb27f73c28c44cd63624ef3c8ac2448c Mon Sep 17 00:00:00 2001 From: liuxiaolong <736321739@qq.com> Date: 星期六, 29 六月 2019 10:57:33 +0800 Subject: [PATCH] init map --- service/FaceSdkService.go | 53 ++++++++++++++++++++++++++++++++--------------------- 1 files changed, 32 insertions(+), 21 deletions(-) diff --git a/service/FaceSdkService.go b/service/FaceSdkService.go index 4749496..6008c1d 100644 --- a/service/FaceSdkService.go +++ b/service/FaceSdkService.go @@ -81,7 +81,7 @@ } height := int32(picMat.Rows()) width := int32(picMat.Cols()) - //data := picMat.ToBytes() + data := picMat.ToBytes() timeUnix := time.Now().Unix() formatTimeStr := time.Unix(timeUnix, 0).Format("2006-01-02 15:04:05") @@ -89,7 +89,7 @@ Width: width, Height: height, Timestamp: formatTimeStr, - Data: imgB, + Data: data, Id: timeUnix, Cid:s.Id, },nil @@ -119,29 +119,24 @@ func (s *FaceSdkService) GetFaceFea(){ var wg sync.WaitGroup wg.Add(1) + ticker := time.NewTicker(time.Second * 3) + go func(ticker *time.Ticker, s *FaceSdkService) { + defer ticker.Stop() + defer wg.Done() - go func() { - Exit: for { select { - case <-time.Tick(time.Second*2): - fmt.Println("鍊掕鏃剁粨鏉�") - wg.Done() - break Exit + case <-ticker.C: + return default: - if feas,ok := resultMap[s.Id];ok { - fmt.Println("faceFea got!!!") + if feas,ok := resultMap.Get(s.Id);ok { s.Result = feas - break Exit - } else { - fmt.Println("灏氭湭鎷垮埌fea") + return } } } - }() - fmt.Println("wa.Wait") + }(ticker, s) wg.Wait() - fmt.Println("<-ch") } func readTestImgFile() protomsg.Image{ @@ -181,14 +176,30 @@ imgPushChan <- is } -var resultMap map[string][]*protomsg.ResultFaceExtCom +type FeaResult struct { + FeaM map[string][]*protomsg.ResultFaceExtCom + Lock sync.Mutex +} + +func (f *FeaResult) Write(id string,faceResult []*protomsg.ResultFaceExtCom){ + f.Lock.Lock() + defer f.Lock.Unlock() + f.FeaM[id] = faceResult +} + +func (f *FeaResult) Get(id string) ([]*protomsg.ResultFaceExtCom,bool){ + f.Lock.Lock() + defer f.Lock.Unlock() + coms,ok := f.FeaM[id] + return coms,ok +} + +var resultMap = FeaResult{} func InitService() { fmt.Println("service init!") imgPushChan = make(chan []byte) - - resultMap = make(map[string][]*protomsg.ResultFaceExtCom,0) - + resultMap.FeaM = make(map[string][]*protomsg.ResultFaceExtCom,0) client_push = deliver.NewClient(deliver.PushPull, Url_Service_PUSH) client_pull = deliver.NewClient(deliver.PushPull, Url_Service_PULL) defer func() { @@ -251,7 +262,7 @@ } else { fmt.Println("鐩爣鏁帮細",len(pff.ExtComp)) if len(pff.ExtComp) >0 { - resultMap[rMsg.Cid] = pff.ExtComp + resultMap.Write(perId,pff.ExtComp) for _,fea :=range pff.ExtComp{ base64Fea := base64.StdEncoding.EncodeToString(fea.Feats) -- Gitblit v1.8.0