| | |
| | | "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 ( |
| | |
| | | |
| | | func NewFaceSdkService(file multipart.File) FaceSdkService{ |
| | | return FaceSdkService{ |
| | | File:file, |
| | | File:&file, |
| | | Id:uuid.NewV4().String(), |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | 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() |
| | |
| | | } |
| | | } |
| | | |
| | | 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{ |
| | |
| | | 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() { |
| | |
| | | } 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) |