| | |
| | | "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 |
| | | } |
| | |
| | | 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 |
| | |
| | | ) |
| | | |
| | | func GetFaceFeaFromSdk(fileBytes []byte,deadTime time.Duration) ([]*protomsg.ResultFaceDetect,error,*protomsg.Image){ |
| | | t1 := time.Now() |
| | | s := NewFaceSdkService(fileBytes, deadTime) |
| | | i, err := s.ReadFromUploadImg() |
| | | logger.Debug("ReadFromUploadImg用时:", time.Since(t1)) |
| | | t1 = time.Now() |
| | | if err !=nil{ |
| | | logger.Debug("readFromUploadImg err:",err) |
| | | return nil,err,i |
| | | } |
| | | bc, err := ImgCompress(i) |
| | | logger.Debug("ImgCompress用时:", time.Since(t1)) |
| | | t1 = time.Now() |
| | | if err !=nil { |
| | | logger.Debug("ImgCompress err:",err) |
| | | return nil,err,i |
| | | } |
| | | s.PushImgMsg(bc) |
| | | logger.Debug("PushImgMsg用时:", time.Since(t1)) |
| | | t1 = time.Now() |
| | | s.GetFaceFea() |
| | | logger.Debug("GetFaceFea用时:", time.Since(t1)) |
| | | if s.Result == nil{ |
| | | return nil,errors.New("no fea"),i |
| | | } else { |
| | |
| | | func NewFaceSdkService(fileBytes []byte, deadTime time.Duration) FaceSdkService{ |
| | | return FaceSdkService{ |
| | | File:fileBytes, |
| | | Id:uuid.NewV4().String(), |
| | | Id:time.Now().UnixNano(), |
| | | DeadTime:deadTime, |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | func (s *FaceSdkService) ReadFromUploadImg() (*protomsg.Image,error){ |
| | | //defer (*(s.File)).Close() |
| | | //imgB, err := ioutil.ReadAll(*(s.File)) |
| | | //if err !=nil{ |
| | | // logger.Debug("File.Read err:",err) |
| | | // return nil,err |
| | | //} |
| | | |
| | | picMat, err := gocv.IMDecode(s.File, gocv.IMReadColor) |
| | | if err !=nil { |
| | | logger.Debug("gocv.IMDecode err:",err) |
| | | return nil,err |
| | | } |
| | | logger.Debug("picMat.Data.len:", len(picMat.ToBytes())) |
| | | newMat := gocv.NewMat() |
| | | size := 1024 |
| | | if picMat.Rows() > size || picMat.Cols() > size { |
| | | fx := float64(size)/float64(picMat.Rows()) |
| | | fy := float64(size)/float64(picMat.Cols()) |
| | | ff := fx |
| | | if fx > fy{ |
| | | ff = fy |
| | | } |
| | | gocv.Resize(picMat,&newMat, image.Pt(0,0), ff, ff, gocv.InterpolationDefault) |
| | | picMat = newMat |
| | | } |
| | | |
| | | defer picMat.Close() |
| | | |
| | | if picMat.Empty() { |
| | | logger.Debug("file not exist") |
| | | return nil,errors.New("picMat is empty") |
| | |
| | | Height: height, |
| | | Timestamp: formatTimeStr, |
| | | Data: data, |
| | | Id: timeUnix, |
| | | Cid:s.Id, |
| | | Id: s.Id, |
| | | Cid: faceExtractWebCID, |
| | | },nil |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | 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) |
| | |
| | | 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() { |
| | |
| | | } |
| | | 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 |
| | | |