From 725d54ef5f16ede9c39cb003ed9d474edf02c0e5 Mon Sep 17 00:00:00 2001
From: liuxiaolong <736321739@qq.com>
Date: 星期五, 30 八月 2019 13:58:25 +0800
Subject: [PATCH] add facedetect grpc

---
 service/FaceSdkService.go |   71 ++++++++++++++++++++++-------------
 1 files changed, 44 insertions(+), 27 deletions(-)

diff --git a/service/FaceSdkService.go b/service/FaceSdkService.go
index 354bd00..9547060 100644
--- a/service/FaceSdkService.go
+++ b/service/FaceSdkService.go
@@ -8,6 +8,7 @@
 	"github.com/pkg/errors"
 	"github.com/satori/go.uuid"
 	"gocv.io/x/gocv"
+	"image"
 	"sync"
 	"time"
 	"webserver/extend/logger"
@@ -33,30 +34,45 @@
 )
 
 func GetFaceFeaFromSdk(fileBytes []byte,deadTime time.Duration) ([]*protomsg.ResultFaceDetect,error,*protomsg.Image){
-	tStart :=time.Now()
+	t1 := time.Now()
 	s := NewFaceSdkService(fileBytes, deadTime)
 	i, err := s.ReadFromUploadImg()
-	logger.Debug("ReadFromUploadImg鐢ㄦ椂锛�", time.Since(tStart))
+	logger.Debug("ReadFromUploadImg鐢ㄦ椂:", time.Since(t1))
+	t1 = time.Now()
 	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))
+	imgBytes, err := proto.Marshal(i)
 	if err !=nil {
-		logger.Debug("ImgCompress err:",err)
+		logger.Debug("i marshal 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
+	detectResults, err := DoFDetectWithRpc(&protomsg.FaceDetectRequest{ReqParam: imgBytes}, deadTime)
+	if err !=nil {
+		logger.Debug("DoFDetectWithRpc err:",err)
+		return nil,err,i
 	} else {
-		return s.Result,nil,i
+		return detectResults,nil,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 {
+	//	return s.Result,nil,i
+	//}
 }
 
 func NewFaceSdkService(fileBytes []byte, deadTime time.Duration) FaceSdkService{
@@ -88,19 +104,27 @@
 }
 
 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")
@@ -122,18 +146,13 @@
 }
 
 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
@@ -143,7 +162,6 @@
 			return nil,errors.New("compressed len is 0")
 		}
 		bc = bc[:n]
-		logger.Debug("bc鐢ㄦ椂锛�", time.Since(t1))
 		return bc,nil
 	}
 }
@@ -273,7 +291,6 @@
 		}
 		rMsg := protomsg.SdkMessage{}
 		if err := proto.Unmarshal(resultBytes, &rMsg); err == nil {
-			logger.Debug("received MSG:", rMsg.Cid)
 			i := protomsg.Image{}
 			bdata, err := util.UnCompress(rMsg.Data)
 			if err !=nil {

--
Gitblit v1.8.0