| | |
| | | "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) |
| | |
| | | } |
| | | 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") |
| | | |
| | |
| | | Width: width, |
| | | Height: height, |
| | | Timestamp: formatTimeStr, |
| | | Data: data, |
| | | Data: imgB, |
| | | Id: timeUnix, |
| | | Cid:s.Id, |
| | | },nil |
| | |
| | | } |
| | | } |
| | | |
| | | 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() { |
| | | Exit: |
| | | for { |
| | | select { |
| | | case <-time.Tick(time.Second*10): |
| | | ch <- nil |
| | | case <-time.Tick(time.Second*2): |
| | | fmt.Println("倒计时结束") |
| | | wg.Done() |
| | | return |
| | | break Exit |
| | | default: |
| | | if feas,ok := resultMap[s.Id];ok { |
| | | fmt.Println("faceFea got!!!") |
| | | ch <- feas |
| | | return |
| | | s.Result = feas |
| | | break Exit |
| | | } else { |
| | | fmt.Println("尚未拿到fea") |
| | | } |
| | | } |
| | | } |
| | | }() |
| | | fmt.Println("wa.Wait") |
| | | wg.Wait() |
| | | msg := <- ch |
| | | if msg != nil { |
| | | return msg,nil |
| | | } else { |
| | | return msg,errors.New("no fea") |
| | | } |
| | | fmt.Println("<-ch") |
| | | } |
| | | |
| | | func readTestImgFile() protomsg.Image{ |