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 | 125 ++++++++++++++++++++++++----------------- 1 files changed, 72 insertions(+), 53 deletions(-) diff --git a/service/FaceSdkService.go b/service/FaceSdkService.go index 5f2888f..6008c1d 100644 --- a/service/FaceSdkService.go +++ b/service/FaceSdkService.go @@ -10,29 +10,31 @@ "github.com/pkg/errors" "github.com/satori/go.uuid" "gocv.io/x/gocv" + "io/ioutil" "mime/multipart" "sync" "time" ) type FaceSdkService struct { - File multipart.File + File *multipart.File Id string + Result []*protomsg.ResultFaceExtCom } const ( - Ipc_Push_Ext = "_2.ipc" - Ipc_Pull_Ext = "_1.ipc" - Ipc_Url_Pre = "ipc:///tmp///" + Ipc_Push_Ext = "_2.ipc" + Ipc_Pull_Ext = "_1.ipc" + Ipc_Url_Pre = "ipc:///tmp///" Virtual_FaceTaskId = "92496BDF-2BFA-98F2-62E8-96DD9866ABD2" - Virtual_FaceSdkId = "virtual-faceextract-sdk-pull" - Url_Service_PUSH = Ipc_Url_Pre + Virtual_FaceSdkId + Ipc_Push_Ext - Url_Service_PULL = Ipc_Url_Pre + Virtual_FaceSdkId + Ipc_Pull_Ext + Virtual_FaceSdkId = "virtual-faceextract-sdk-pull" + Url_Service_PUSH = Ipc_Url_Pre + Virtual_FaceSdkId + Ipc_Push_Ext + Url_Service_PULL = Ipc_Url_Pre + Virtual_FaceSdkId + Ipc_Pull_Ext ) func NewFaceSdkService(file multipart.File) FaceSdkService{ return FaceSdkService{ - File:file, + File:&file, Id:uuid.NewV4().String(), } } @@ -58,13 +60,18 @@ } func (s *FaceSdkService) ReadFromUploadImg() (*protomsg.Image,error){ - defer s.File.Close() - imgB := make([]byte,0) - if _, err := s.File.Read(imgB);err !=nil{ + defer (*(s.File)).Close() + imgB, err := ioutil.ReadAll(*(s.File)) + if err !=nil{ + fmt.Println("File.Read err:",err) return nil,err } + + fmt.Println("imgB.len:",len(imgB)) + picMat, err := gocv.IMDecode(imgB, gocv.IMReadColor) if err !=nil { + fmt.Println("gocv.IMDecode err:",err) return nil,err } defer picMat.Close() @@ -89,18 +96,18 @@ } func ImgCompress(i *protomsg.Image) ([]byte,error){ - if b, err := proto.Marshal(i);err !=nil{ + if b, err := proto.Marshal(i); err != nil { fmt.Println("protoImage marshal err") return nil,err } else { - bc := make([]byte,len(b)) + bc := make([]byte, len(b)) ht := make([]int, 64<<10) - n,err := lz4.CompressBlock(b, bc, ht) - if err !=nil { + n, err := lz4.CompressBlock(b, bc, ht) + if err != nil { fmt.Println(err) return nil,err } - if n >= len(b){ + if n >= len(b) { fmt.Println("image is not compressible") return nil,errors.New("compressed len is 0") } @@ -109,33 +116,27 @@ } } -func (s *FaceSdkService) GetFaceFea() (feas []*protomsg.ResultFaceExtCom,err error){ +func (s *FaceSdkService) GetFaceFea(){ var wg sync.WaitGroup wg.Add(1) - ch := make(chan []*protomsg.ResultFaceExtCom,0) - go func() { + ticker := time.NewTicker(time.Second * 3) + go func(ticker *time.Ticker, s *FaceSdkService) { + defer ticker.Stop() + defer wg.Done() + for { select { - case <-time.Tick(time.Second*10): - ch <- nil - wg.Done() + case <-ticker.C: return default: - if feas,ok := resultMap[s.Id];ok { - fmt.Println("faceFea got!!!") - ch <- feas + if feas,ok := resultMap.Get(s.Id);ok { + s.Result = feas return } } } - }() + }(ticker, s) wg.Wait() - msg := <- ch - if msg != nil { - return msg,nil - } else { - return msg,errors.New("no fea") - } } func readTestImgFile() protomsg.Image{ @@ -160,13 +161,13 @@ //gocv.IMWrite("xxx.jpg", wrMat) i = protomsg.Image{ - Width: width, - Height: height, + Width: width, + Height: height, Timestamp: formatTimeStr, - Data: data, - Id: timeUnix, + Data: data, + Id: timeUnix, } - i.Cid = uuid.NewV4().String()//鏁版嵁鍞竴id + i.Cid = uuid.NewV4().String() //鏁版嵁鍞竴id fmt.Println("gocv read img completed") return i } @@ -175,13 +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 InitService(){ +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() { @@ -193,14 +211,14 @@ go thRecv() } -func thSend(){ +func thSend() { for { select { - case d := <- imgPushChan: + case d := <-imgPushChan: fmt.Println("imgPushChan in") err := client_push.Send(d) - if err !=nil { - fmt.Println("img Send err:",err) + if err != nil { + fmt.Println("img Send err:", err) } default: //fmt.Println("no img in") @@ -208,19 +226,20 @@ } } -func thRecv(){ +func thRecv() { for { resultBytes, err := client_pull.Recv() - if err !=nil{ - fmt.Println("pull err:",err) + if err != nil { + //fmt.Println("pull err:", err) continue } rMsg := protomsg.SdkMessage{} - if err := proto.Unmarshal(resultBytes, &rMsg);err ==nil{ - fmt.Println("received MSG:",rMsg.Cid) + if err := proto.Unmarshal(resultBytes, &rMsg); err == nil { + fmt.Println("received MSG:", rMsg.Cid) perId := rMsg.Cid //鏁版嵁id - if rMsg.Tasklab !=nil && rMsg.Tasklab.Taskid == Virtual_FaceTaskId { + if rMsg.Tasklab != nil && rMsg.Tasklab.Taskid == Virtual_FaceTaskId { sdkInfos := rMsg.Tasklab.Sdkinfos + fmt.Println("Len(sdkInfos)=",len(sdkInfos)) for _,swt :=range sdkInfos{ if swt.Sdktype =="FaceDetect"{ @@ -243,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) @@ -257,8 +276,8 @@ } } } else { - fmt.Println("recv msg Err:",err) + fmt.Println("recv msg Err:", err) } } -} \ No newline at end of file +} -- Gitblit v1.8.0