| | |
| | | |
| | | import ( |
| | | "basic.com/pubsub/protomsg.git" |
| | | "basic.com/valib/godraw.git" |
| | | "basic.com/valib/logger.git" |
| | | "bytes" |
| | | "github.com/gogo/protobuf/proto" |
| | | "github.com/pkg/errors" |
| | | "gocv.io/x/gocv" |
| | | "image" |
| | | "time" |
| | | "basic.com/valib/logger.git" |
| | | ) |
| | | |
| | | type FaceSdkService struct { |
| | |
| | | } |
| | | |
| | | func (s *FaceSdkService) readFromUploadImg() (*protomsg.Image,error){ |
| | | picMat, err := gocv.IMDecode(s.File, gocv.IMReadColor) |
| | | if err !=nil { |
| | | logger.Debug("gocv.IMDecode err:",err) |
| | | return nil,err |
| | | bt := bytes.NewBuffer(s.File) |
| | | img, _, err := image.Decode(bt) |
| | | if err != nil { |
| | | 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 |
| | | } |
| | | bgr := godraw.Image2BGR(img) |
| | | |
| | | defer picMat.Close() |
| | | |
| | | if picMat.Empty() { |
| | | logger.Debug("file not exist") |
| | | return nil,errors.New("picMat is empty") |
| | | } |
| | | height := int32(picMat.Rows()) |
| | | width := int32(picMat.Cols()) |
| | | data := picMat.ToBytes() |
| | | timeUnix := time.Now().Unix() |
| | | formatTimeStr := time.Unix(timeUnix, 0).Format("2006-01-02 15:04:05") |
| | | |
| | | return &protomsg.Image{ |
| | | Width: width, |
| | | Height: height, |
| | | Width: int32(img.Bounds().Dx()), |
| | | Height: int32(img.Bounds().Dy()), |
| | | Timestamp: formatTimeStr, |
| | | Data: data, |
| | | Data: bgr, |
| | | Id: s.Id, |
| | | Cid: faceExtractWebCID, |
| | | },nil |