From f88a44f58396fb36a979cfe5640a36d6ac2d7289 Mon Sep 17 00:00:00 2001 From: liuxiaolong <736321739@qq.com> Date: 星期四, 25 七月 2019 14:50:56 +0800 Subject: [PATCH] add discovery --- controllers/fileController.go | 135 +++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 129 insertions(+), 6 deletions(-) diff --git a/controllers/fileController.go b/controllers/fileController.go index a5bc403..fd631e1 100644 --- a/controllers/fileController.go +++ b/controllers/fileController.go @@ -3,11 +3,14 @@ import ( "basic.com/pubsub/protomsg.git" "basic.com/fileServer/WeedFSClient.git" + "basic.com/valib/deliver.git" "bytes" "encoding/base64" + "encoding/json" "errors" "fmt" "github.com/gin-gonic/gin" + "github.com/gogo/protobuf/proto" "github.com/satori/go.uuid" "image" "image/jpeg" @@ -25,6 +28,7 @@ "webserver/extend/util" "webserver/models" "webserver/service" + esApi "basic.com/pubsub/esutil.git" ) type FileController struct { @@ -91,6 +95,127 @@ } } else { util.ResponseFormat(c, code.Success, fileNameOnly) + } +} + + +type FaceExtract struct { + Url string `json:"url"` + FaceBytes []byte `json:"faceBytes"` +} +var faceExtractedMap = make(map[string]FaceExtract,0) + +// @Summary 浜鸿劯鎻愬彇 +// @Description 浜鸿劯鎻愬彇 +// @Produce json +// @Tags 浠ュ浘鎼滃浘 +// @Param file formData file true "浜哄憳鍥剧墖" +// @Success 200 {string} json "{"code":200, msg:"", data:"", success:true}" +// @Failure 500 {string} json "{"code":500, msg:"", data:"", success:false}" +// @Router /data/api-v/dbperson/faceExtract [POST] +func (controller FileController) FaceExtract(c *gin.Context) { + file, _, err := c.Request.FormFile("file") //image杩欎釜鏄痷plaodify鍙傛暟瀹氫箟涓殑 'fileObjName':'image' + if err != nil { + util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎") + return + } + var weedfsUri = "http://"+config.WeedFs.Ip+":"+strconv.Itoa(config.WeedFs.UploadPort)+"/submit" + resultMap :=make(map[string]interface{},0) + //灏嗕笂浼犵殑鍥剧墖浜や汉鑴告娴嬪拰浜鸿劯鎻愬彇锛岃幏寰楃壒寰� + fileBytes, _ := ioutil.ReadAll(file) + faceArr, err, pI := service.GetFaceFeaFromSdk(fileBytes, time.Second*60) + if err ==nil && len(faceArr) >0 { + //1.鎻愬彇姣忎竴寮犱汉鑴稿皬鍥� + 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)) + weedFilePath, e := WeedFSClient.UploadFile(weedfsUri, "FaceUrl", cutFaceImgData) + if e == nil{ + faceExtractedMap[weedFilePath] = FaceExtract{ + Url:weedFilePath, + FaceBytes:r.Feats, + } + urlArr = append(urlArr, weedFilePath) + } + } + //2.澶у浘鐢绘,鏍囪瘑浜鸿劯浣嶇疆 + originFilePath, _ := WeedFSClient.UploadFile(weedfsUri, "FaceUrl", fileBytes) + resultMap["uploadImage"] = originFilePath + resultMap["smImage"] = urlArr + util.ResponseFormat(c,code.Success, resultMap) + } else { + util.ResponseFormat(c,code.ComError,"鏈彁鍙栧埌浜鸿劯") + } +} + +// @Summary 浠ュ浘鎼滃浘 +// @Description 浠ュ浘鎼滃浘 +// @Produce json +// @Tags 浠ュ浘鎼滃浘 +// @Param url formData string true "url" +// @Param tableId formData string true "搴曞簱id" +// @Param compThreshold formData string true "姣斿闃堝��" +// @Success 200 {string} json "{"code":200, msg:"", data:"", success:true}" +// @Failure 500 {string} json "{"code":500, msg:"", data:"", success:false}" +// @Router /data/api-v/dbperson/searchByPhoto [POST] +func (controller FileController) SearchByPhoto(c *gin.Context) { + photoUrl := c.Request.FormValue("url") + tableId := c.Request.FormValue("tableId")//搴曞簱id + compThresholdStr := c.Request.FormValue("compThreshold")//姣斿闃堝�� + compThreshold, err := strconv.ParseFloat(compThresholdStr,32) + if photoUrl == "" || err != nil { + util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎") + return + } + if face,ok := faceExtractedMap[photoUrl];!ok{ + util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎") + return + } else { + arg := protomsg.CompareArgs{ + FaceFeature:face.FaceBytes, + CompareThreshold:float32(compThreshold), + } + if tableId !=""{ + arg.TableIds = []string{ tableId } + } + b, err := proto.Marshal(&arg) + if err !=nil{ + util.ResponseFormat(c, code.ComError, "璇锋眰marshal澶辫触") + return + } + compServerList := config.CompServerInfo.Url + fmt.Println("compServerList:", compServerList) + resultList :=make([]protomsg.Dbperson,0) + for _,str :=range compServerList{ + reqUrl := "tcp://"+str + reqClient := deliver.NewClient(deliver.ReqRep, reqUrl) + err = reqClient.Send(b) + if err !=nil{ + continue + } + resultB, err := reqClient.Recv() + if err !=nil{ + continue + } + m :=make(map[string]float32,0) + err = json.Unmarshal(resultB, &m) + if err !=nil{ + continue + } + personIds := make([]string,len(m)) + for k,_ :=range m{ + personIds = append(personIds,k) + } + esServerIp := config.EsInfo.Masterip + esServerPort := config.EsInfo.Httpport + index := config.EsInfo.EsIndex.Dbtablepersons.IndexName + dbpeople, e := esApi.Dbpersoninfosbyid(personIds, index, esServerIp, esServerPort) + if e ==nil{ + resultList = append(resultList,dbpeople...) + } + } + util.ResponseFormat(c,code.Success,resultList) } } @@ -249,12 +374,10 @@ field, _, err1 := uploadFileReturnAddr(file, filename, tableId) //extNames = append(extNames, field) - if err1 != nil { - if field == "" { - failList = append(failList, filename+"涓婁紶澶辫触,"+err1.Error()) - } else { - successList = append(successList, filename+"鍔犲叆搴曞簱澶辫触,"+err1.Error()) - } + if err1 != nil || field == "" { + failList = append(failList, filename) + } else { + successList = append(successList, filename) } } addResult["successList"] = successList -- Gitblit v1.8.0