From 92c1e11f87617b0e67f6918423a428248e2015e0 Mon Sep 17 00:00:00 2001 From: liuxiaolong <736321739@qq.com> Date: 星期二, 06 八月 2019 11:26:44 +0800 Subject: [PATCH] from picMat to newMat use resize --- service/FaceSdkService.go | 51 +++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 41 insertions(+), 10 deletions(-) diff --git a/service/FaceSdkService.go b/service/FaceSdkService.go index 8cbb403..5cfa375 100644 --- a/service/FaceSdkService.go +++ b/service/FaceSdkService.go @@ -8,14 +8,16 @@ "github.com/pkg/errors" "github.com/satori/go.uuid" "gocv.io/x/gocv" + "image" "sync" "time" "webserver/extend/logger" + "webserver/extend/util" ) type FaceSdkService struct { File []byte - Id string + Id int64 Result []*protomsg.ResultFaceDetect DeadTime time.Duration } @@ -24,6 +26,7 @@ Ipc_Push_Ext = "_2.ipc" Ipc_Pull_Ext = "_1.ipc" Ipc_Url_Pre = "ipc:///tmp///" + faceExtractWebCID = "virtual-face-extract-web-camera-id" Virtual_FaceTaskId = "92496BDF-2BFA-98F2-62E8-96DD9866ABD2" Virtual_FaceSdkId = "virtual-faceextract-sdk-pull" Url_Service_PUSH = Ipc_Url_Pre + Virtual_FaceSdkId + Ipc_Push_Ext @@ -31,19 +34,25 @@ ) func GetFaceFeaFromSdk(fileBytes []byte,deadTime time.Duration) ([]*protomsg.ResultFaceDetect,error,*protomsg.Image){ + tStart :=time.Now() s := NewFaceSdkService(fileBytes, deadTime) i, err := s.ReadFromUploadImg() + logger.Debug("ReadFromUploadImg鐢ㄦ椂锛�", time.Since(tStart)) if err !=nil{ logger.Debug("readFromUploadImg err:",err) return nil,err,i } + tStart = time.Now() bc, err := ImgCompress(i) + logger.Debug("ImgCompress鐢ㄦ椂锛�", time.Since(tStart)) if err !=nil { logger.Debug("ImgCompress err:",err) return nil,err,i } s.PushImgMsg(bc) + t1 := time.Now() s.GetFaceFea() + logger.Debug("GetFaceFea鐢ㄦ椂:", time.Since(t1)) if s.Result == nil{ return nil,errors.New("no fea"),i } else { @@ -54,7 +63,7 @@ func NewFaceSdkService(fileBytes []byte, deadTime time.Duration) FaceSdkService{ return FaceSdkService{ File:fileBytes, - Id:uuid.NewV4().String(), + Id:time.Now().UnixNano(), DeadTime:deadTime, } } @@ -92,6 +101,12 @@ logger.Debug("gocv.IMDecode err:",err) return nil,err } + newMat := gocv.NewMat() + if picMat.Rows() > 1000 || picMat.Cols() > 1000 { + gocv.Resize(picMat,&newMat, image.Pt(0,0), 0.5, 0.5, gocv.InterpolationDefault) + picMat = newMat + } + defer picMat.Close() if picMat.Empty() { logger.Debug("file not exist") @@ -108,19 +123,24 @@ Height: height, Timestamp: formatTimeStr, Data: data, - Id: timeUnix, - Cid:s.Id, + Id: s.Id, + Cid: faceExtractWebCID, },nil } func ImgCompress(i *protomsg.Image) ([]byte,error){ + t1 := time.Now() if b, err := proto.Marshal(i); err != nil { logger.Debug("protoImage marshal err") return nil,err } else { + logger.Debug("proto.Marshal鐢ㄦ椂锛�", time.Since(t1)) + t1 = time.Now() bc := make([]byte, len(b)) ht := make([]int, 64<<10) n, err := lz4.CompressBlock(b, bc, ht) + logger.Debug("lz4.CompressBlock鐢ㄦ椂锛�", time.Since(t1)) + t1 = time.Now() if err != nil { logger.Debug(err) return nil,err @@ -130,6 +150,7 @@ return nil,errors.New("compressed len is 0") } bc = bc[:n] + logger.Debug("bc鐢ㄦ椂锛�", time.Since(t1)) return bc,nil } } @@ -195,24 +216,24 @@ } type FeaResult struct { - FaceM map[string][]*protomsg.ResultFaceDetect + FaceM map[int64][]*protomsg.ResultFaceDetect Lock sync.Mutex } -func (f *FeaResult) Write(id string,faceDetectResult []*protomsg.ResultFaceDetect){ +func (f *FeaResult) Write(id int64,faceDetectResult []*protomsg.ResultFaceDetect){ f.Lock.Lock() defer f.Lock.Unlock() f.FaceM[id] = faceDetectResult } -func (f *FeaResult) Get(id string) ([]*protomsg.ResultFaceDetect,bool){ +func (f *FeaResult) Get(id int64) ([]*protomsg.ResultFaceDetect,bool){ f.Lock.Lock() defer f.Lock.Unlock() coms,ok := f.FaceM[id] return coms,ok } -func (f *FeaResult) Delete(id string){ +func (f *FeaResult) Delete(id int64){ f.Lock.Lock() defer f.Lock.Unlock() delete(f.FaceM,id) @@ -223,7 +244,7 @@ func InitService() { logger.Debug("service init!") imgPushChan = make(chan []byte) - resultMap.FaceM = make(map[string][]*protomsg.ResultFaceDetect,0) + resultMap.FaceM = make(map[int64][]*protomsg.ResultFaceDetect,0) client_push = deliver.NewClient(deliver.PushPull, Url_Service_PUSH) client_pull = deliver.NewClient(deliver.PushPull, Url_Service_PULL) defer func() { @@ -260,7 +281,17 @@ rMsg := protomsg.SdkMessage{} if err := proto.Unmarshal(resultBytes, &rMsg); err == nil { logger.Debug("received MSG:", rMsg.Cid) - perId := rMsg.Cid //鏁版嵁id + i := protomsg.Image{} + bdata, err := util.UnCompress(rMsg.Data) + if err !=nil { + logger.Debug("uncompress err:",err) + continue + } + err = proto.Unmarshal(bdata, &i) + if err !=nil { + continue + } + perId := i.Id //鏁版嵁id if rMsg.Tasklab != nil && rMsg.Tasklab.Taskid == Virtual_FaceTaskId { sdkInfos := rMsg.Tasklab.Sdkinfos -- Gitblit v1.8.0