sunty
2020-08-20 9303b69ea569bcb5e581147543a3fd58e90d0d25
controllers/fileController.go
@@ -5,6 +5,7 @@
   "basic.com/fileServer/WeedFSClient.git"
   esApi "basic.com/pubsub/esutil.git"
   "basic.com/pubsub/protomsg.git"
   "basic.com/valib/godraw.git"
   "basic.com/valib/logger.git"
   "bytes"
   "encoding/base64"
@@ -12,7 +13,6 @@
   "errors"
   "fmt"
   "github.com/gin-gonic/gin"
   "gocv.io/x/gocv"
   "image"
   "image/color"
   "image/jpeg"
@@ -71,7 +71,7 @@
   filename := header.Filename
   var result = make(map[string]string)
   if picType == "1" {
      fileNameOnly, _, err, faceFeature := uploadFileReturnAddr(file, filename, "", false)
      _, _, err, faceFeature, faceUrlPath := uploadFileReturnAddr(file, filename, "", false)
      if err != nil {
         if err.Error() == "NotFeatureFindError" {
            util.ResponseFormat(c, code.NotFeatureFindError, filename)
@@ -80,7 +80,7 @@
         }
         return
      }
      result["picUrl"] = fileNameOnly
      result["picUrl"] = faceUrlPath
      result["faceFeature"] = faceFeature
   } else {
      localConf, err2 := cache.GetServerInfo()
@@ -148,7 +148,11 @@
      urlArr := make([]string,0)
      for _,r := range faceArr {
         rcFace := r.Pos.RcFace
         cutFaceImgData := util.SubImg(*pI, int(rcFace.Left), int(rcFace.Top), int(rcFace.Right), int(rcFace.Bottom))
         cutFaceImgData,cutErr := util.SubCutImg(pI, rcFace, 20)
         if cutErr != nil {
            logger.Debug("util.SubCutImg err:", cutErr)
            continue
         }
         weedFilePath, e := WeedFSClient.UploadFile(weedfsUri, "FaceUrl", cutFaceImgData)
         if e == nil{
            faceExtractedMap[weedFilePath] = FaceExtract{
@@ -171,19 +175,19 @@
}
func drawPolygonOnImg(i *protomsg.Image,faceArr *[]*protomsg.ResultFaceDetect) (*[]byte,error) {
   rook, _ := gocv.NewMatFromBytes(int(i.Height), int(i.Width), gocv.MatTypeCV8UC3, i.Data)
   defer rook.Close()
   //yellow := color.RGBA{255, 255, 0, 0}
   img,err := godraw.ToImage(i.Data, int(i.Width), int(i.Height))
   if err != nil {
      return nil,err
   }
   red := color.RGBA{255, 0, 0, 0}
   for _,faceResult := range *faceArr {
      left := int(faceResult.Pos.RcFace.Left)
      top := int(faceResult.Pos.RcFace.Top)
      right := int(faceResult.Pos.RcFace.Right)
      bottom := int(faceResult.Pos.RcFace.Bottom)
      gocv.Rectangle(&rook, image.Rect(left,top,right,bottom), red, 1)
      util.DrawRect(img.(*image.RGBA),int(faceResult.Pos.RcFace.Left),int(faceResult.Pos.RcFace.Top),int(faceResult.Pos.RcFace.Right),
         int(faceResult.Pos.RcFace.Bottom), red)
   }
   fData,err := gocv.IMEncode(".jpg", rook)
   return &fData,err
   quantity := 100
   jpgData, err := godraw.ImageToJpeg(img, &quantity)
   return &jpgData,err
}
type CompareResult struct {
@@ -413,7 +417,7 @@
   faceArr, err, pI := service.GetFaceFeaFromSdk(fileBytes, time.Second*60)
   if err ==nil && len(faceArr) ==1 {
      rcFace := faceArr[0].Pos.RcFace
      cutFaceImgData := util.SubImg(*pI, int(rcFace.Left), int(rcFace.Top), int(rcFace.Right), int(rcFace.Bottom))
      cutFaceImgData,_ := util.SubCutImg(pI, rcFace, 20)
      weedFilePath, e := WeedFSClient.UploadFile(weedfsUri, "FaceUrl", cutFaceImgData)
      if e == nil{
         faceExtractedMap[weedFilePath] = FaceExtract{
@@ -492,6 +496,9 @@
               return
            }
            faceB = decodeF
         } else {
            util.ResponseFormat(c, code.ComError, "本条人脸抓拍特征为空,请检查")
            return
         }
      }
@@ -761,7 +768,7 @@
         break
      }
      //根据人脸坐标扣出人脸小图
      cutFaceImgData := util.SubImg(*pI, int(rcFace.Left), int(rcFace.Top), int(rcFace.Right), int(rcFace.Bottom))
      cutFaceImgData,_ := util.SubCutImg(pI, rcFace, 20)
      weedFilePath, e := WeedFSClient.UploadFile(weedfsUri, "testCutFace", cutFaceImgData)
      if e !=nil{
         util.ResponseFormat(c,code.ComError,"文件上传失败")
@@ -779,24 +786,24 @@
log.Fatalln(err)
}*/
func uploadFileReturnAddr(file multipart.File, filename string, tableId string, add2Db bool) (string, map[string]interface{}, error, string) {
func uploadFileReturnAddr(file multipart.File, filename string, tableId string, add2Db bool) (string, map[string]interface{}, error, string, string) {
   defer file.Close()
   // weedfs 上传
   fileBytes, err := ioutil.ReadAll(file)
   if err !=nil {
      return "",nil,err,""
      return "",nil,err,"",""
   }
   //将上传的图片交人脸检测和人脸提取,获得特征
   var faceBase64=""
   faceArr, err, pI := service.GetFaceFeaFromSdk(fileBytes, time.Second*5)
   if faceArr ==nil {
      return "",nil,errors.New("NotFeatureFindError"),""
      return "",nil,errors.New("NotFeatureFindError"),"",""
   }
   var rcFace *protomsg.Rect
   if err ==nil && len(faceArr) >0 {
      if len(faceArr) >1 {
         return "",nil, errors.New("TooManyFaces"),""
         return "",nil, errors.New("TooManyFaces"),"",""
      }
      for _,r := range faceArr {
         //拿到人脸的坐标
@@ -809,12 +816,16 @@
   localConf, err2 := cache.GetServerInfo()
   if err2 !=nil || localConf.WebPicIp == "" {
      logger.Debug("localConfig is wrong!!!")
      return "",nil,err2,""
      return "",nil,err2,"",""
   }
   var weedfsUri = "http://"+localConf.WebPicIp+":"+strconv.Itoa(int(localConf.WebPicPort))+"/submit?collection=persistent"
   //根据人脸坐标扣出人脸小图
   t1 := time.Now()
   cutFaceImgData := util.SubImg(*pI, int(rcFace.Left), int(rcFace.Top), int(rcFace.Right), int(rcFace.Bottom))
   cutFaceImgData,cutErr := util.SubCutImg(pI, rcFace, 20)
   if cutErr != nil {
      logger.Debug("util.SubCutImg err:", cutErr)
      return "", nil, cutErr,"",""
   }
   logger.Debug("SubImg用时:", time.Since(t1))
   t1 = time.Now()
   weedFilePath, e := WeedFSClient.UploadFile(weedfsUri, filename, cutFaceImgData)
@@ -822,7 +833,7 @@
   t1 = time.Now()
   if e != nil {
      logger.Debug("WeedFSClient.UploadFile err:", e)
      return "", nil, e,""
      return "", nil, e,"",""
   }
   ext := path.Ext(filename)
@@ -840,8 +851,8 @@
      result = addDbPerson(dbperson)
   }
   logger.Debug("addDbPerson用时:", time.Since(t1))
   return fileNameOnly, result, nil,faceBase64
   logger.Debug("addDbPerson用时:", time.Since(t1), "result:", result)
   return fileNameOnly, result, nil,faceBase64, weedFilePath
}
@@ -898,7 +909,7 @@
            lock.Unlock()
            return
         }
         field, _, err1,_ := uploadFileReturnAddr(file, filename, tableId, true)
         field, _, err1,_,_ := uploadFileReturnAddr(file, filename, tableId, true)
         lock.Lock()
         if err1 != nil || field == "" {
            if err1 !=nil && err1.Error() == "NotFeatureFindError" {