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

---
 controllers/dbtableperson.go |  179 +++++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 131 insertions(+), 48 deletions(-)

diff --git a/controllers/dbtableperson.go b/controllers/dbtableperson.go
index 3278f61..966de9d 100644
--- a/controllers/dbtableperson.go
+++ b/controllers/dbtableperson.go
@@ -2,9 +2,15 @@
 
 import (
 	"basic.com/dbapi.git"
+	"basic.com/fileServer/WeedFSClient.git"
+	"basic.com/pubsub/protomsg.git"
+	"encoding/base64"
 	"encoding/json"
+	"io/ioutil"
 	"strconv"
+	"time"
 	"webserver/extend/logger"
+	"webserver/service"
 
 	"github.com/gin-gonic/gin"
 	"github.com/satori/go.uuid"
@@ -24,9 +30,8 @@
 // @Produce json
 // @Tags dbperson 搴曞簱浜哄憳
 // @Param obj body models.Dbtablepersons true "搴曞簱浜哄憳鏁版嵁"
-// @Success 200 {object} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}"
-// @Failure 500 {string} json "{"code":500,  msg:"杩斿洖閿欒淇℃伅", success:false}"
-// @Failure 400 {object} json code.RequestParamError
+// @Success 200 {string} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}"
+// @Failure 500 {string} json "{"code":500, msg:"杩斿洖閿欒淇℃伅", success:false}"
 // @Router /data/api-v/dbperson/addDbPerson [PUT]
 func (dbc DbPersonController) AddDbPerson(c *gin.Context) {
 	dbperson := new(models.Dbtablepersons)
@@ -99,6 +104,76 @@
 	}
 }
 
+// @Summary 鏇存柊搴曞簱浜鸿劯鐓х墖
+// @Description 鏇存柊搴曞簱浜鸿劯鐓х墖
+// @Accept  json
+// @Produce json
+// @Tags 搴曞簱浜哄憳
+// @Param id formData string true "浜哄憳id"
+// @Param file formData file true "浜鸿劯鍥剧墖"
+// @Success 200 {string} json "{"code":200, msg:"", success:true}"
+// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
+// @Router /data/api-v/dbperson/updateFace [POST]
+func (dbc DbPersonController) UpdateFace(c *gin.Context) {
+	file, header, err := c.Request.FormFile("file")
+	id := c.Request.FormValue("id")
+	if err != nil || id == "" {
+		util.ResponseFormat(c,code.RequestParamError,"鍙傛暟鏈夎")
+		return
+	}
+	//鏂囦欢鐨勫悕绉�
+	filename := header.Filename
+	defer file.Close()
+	// weedfs 涓婁紶
+	fileBytes, err := ioutil.ReadAll(file)
+	if err !=nil {
+		util.ResponseFormat(c,code.ComError,"鍥剧墖璇诲彇澶辫触")
+		return
+	}
+
+	//灏嗕笂浼犵殑鍥剧墖浜や汉鑴告娴嬪拰浜鸿劯鎻愬彇锛岃幏寰楃壒寰�
+	var faceBase64=""
+	faceArr, err, pI := service.GetFaceFeaFromSdk(fileBytes, time.Second*5)
+	if faceArr ==nil {
+		util.ResponseFormat(c,code.ComError,"鏈埌鎻愬彇浜鸿劯")
+		return
+	}
+	var rcFace *protomsg.Rect
+	if err ==nil && len(faceArr) >0 {
+		if len(faceArr) >1 {
+			util.ResponseFormat(c,code.ComError,"浜鸿劯澶т簬涓�寮狅紝璇锋崲涓�寮犱汉鑴稿浘鐗�")
+			return
+		}
+		for _,r := range faceArr {
+			//鎷垮埌浜鸿劯鐨勫潗鏍�
+			rcFace = r.Pos.RcFace
+
+			faceBase64 = base64.StdEncoding.EncodeToString(r.Feats)//鑾峰彇鎻愬彇鍒扮殑绗竴寮犱汉鑴哥壒寰�
+			break
+		}
+	}
+	var weedfsUri = "http://"+config.WeedFs.Ip+":"+strconv.Itoa(config.WeedFs.UploadPort)+"/submit"
+	//鏍规嵁浜鸿劯鍧愭爣鎵e嚭浜鸿劯灏忓浘
+	t1 := time.Now()
+	cutFaceImgData := util.SubImg(*pI, int(rcFace.Left), int(rcFace.Top), int(rcFace.Right), int(rcFace.Bottom))
+	logger.Debug("SubImg鐢ㄦ椂锛�", time.Since(t1))
+	t1 = time.Now()
+	weedFilePath, e := WeedFSClient.UploadFile(weedfsUri, filename, cutFaceImgData)
+	logger.Debug("涓婁紶鍒皐eedfs鐢ㄦ椂:", time.Since(t1))
+	t1 = time.Now()
+	if e != nil {
+		util.ResponseFormat(c,code.ComError,"浜鸿劯涓婁紶澶辫触")
+		return
+	}
+	var dbpApi dbapi.DbPersonApi
+	b,d := dbpApi.UpdateFace(id,faceBase64,weedFilePath)
+	if b {
+		util.ResponseFormat(c,code.UpdateSuccess,d)
+	} else {
+		util.ResponseFormat(c,code.UpdateFail,"鏇存柊浜鸿劯澶辫触")
+	}
+}
+
 func UpdateDbPersonsOfDbTable(id string) (message string) {
 	url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
 		"/" + config.EsInfo.EsIndex.Dbtablepersons.IndexName + "/_update_by_query?refresh"
@@ -164,25 +239,30 @@
 	}
 }
 
+type DelMultiPerson []string
+
 // @Summary 鍒犻櫎搴曞簱浜哄憳
 // @Description 鍒犻櫎搴撲汉鍛�
 // @Accept  json
 // @Produce json
 // @Tags dbperson 搴曞簱浜哄憳
-// @Param uuids body []string true "搴曞簱浜哄憳ids "
+// @Param uuids body controllers.DelMultiPerson true "搴曞簱浜哄憳ids "
 // @Success 200 {string} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}"
 // @Failure 500 {string} json "{"code":500,  msg:"杩斿洖閿欒淇℃伅", success:false}"
 // @Router /data/api-v/dbperson/deleteMoreDbPerson [POST]
 func (dbc DbPersonController) DeleteMoreDbPerson(c *gin.Context) {
-	uuids := make([]string, 0, 5)
+	var uuids DelMultiPerson
 	err := c.BindJSON(&uuids)
 	if err !=nil || len(uuids)==0{
 		util.ResponseFormat(c,code.RequestParamError,"鍙傛暟鏈夎")
 		return
 	}
+	logger.Debug("DeleteMoreDbPerson len(uuids):",len(uuids))
 	var pApi dbapi.DbPersonApi
-	paramBody := util.Struct2Map(uuids)
-	b, _ := pApi.DeleteMoreDbPerson(paramBody)
+	m := map[string]interface{}{
+		"ids": uuids,
+	}
+	b, _ := pApi.DeleteMoreDbPerson(m)
 	if b {
 		util.ResponseFormat(c, code.Success, "鍒犻櫎搴曞簱浜哄憳鎴愬姛")
 	} else {
@@ -195,41 +275,41 @@
 // @Accept  json
 // @Produce json
 // @Tags dbperson 搴曞簱浜哄憳
-// @Param reqMap body map false "{"tableId":"","orderName":"uuid","orderType":"desc","contentValue":"","page":1,"size":8}"
+// @Param reqMap body controllers.DbtSearch false "{"tableId":"","orderName":"id","orderType":"desc","contentValue":"","page":1,"size":8}"
 // @Success 200 {string} json "{"code":200, "msg":"鐩綍缁撴瀯鏁版嵁", "success":true,"data":{}}"
 // @Failure 500 {string} json "{code:500,  msg:"杩斿洖閿欒淇℃伅", success:false,data:{}}"
 // @Router /data/api-v/dbperson/queryDbPersonsByTbId [POST]
 func (dbc DbPersonController) QueryDbPersonsByTbId(c *gin.Context) {
-	reqBody := make(map[string]interface{}, 5)
-	c.BindJSON(&reqBody)
-	tableId := ""
-	if reqBody["tableId"] != nil {
-		tableId = reqBody["tableId"].(string)
+	//reqBody := make(map[string]interface{}, 5)
+	var reqBody DbtSearch
+	err := c.BindJSON(&reqBody)
+	if err !=nil || reqBody.Page <=0 || reqBody.Size <=0 {
+		util.ResponseFormat(c,code.RequestParamError,"鍙傛暟鏈夎")
+		return
+	}
+
+	if reqBody.TableId == "" {
+		util.ResponseFormat(c,code.RequestParamError,"鍙傛暟鏈夎锛屽簳搴搃d涓嶈兘涓虹┖")
+		return
 	}
 	orderName := "id"
-	if reqBody["orderName"] != nil {
-		orderName = reqBody["orderName"].(string)
+	if reqBody.OrderName != "" {
+		orderName = reqBody.OrderName
 	} // 鍒楀悕
 	orderType := "desc"
-	if reqBody["orderType"] != nil {
-		orderType = reqBody["orderType"].(string)
+	if reqBody.OrderType != "" {
+		orderType = reqBody.OrderType
 	} // 鍒楃被鍨�
 	contentValue := ""
-	if reqBody["contentValue"] != nil {
-		contentValue = reqBody["contentValue"].(string)
-	} //杈撳叆妗嗗唴瀹�
+
 	page := 1
-	if reqBody["page"] != nil {
-		page = int(reqBody["page"].(float64))
+	if reqBody.Page >1 {
+		page = reqBody.Page
 	} // 椤电爜
 	size := 8
-	if reqBody["size"] != nil {
-		size = int(reqBody["size"].(float64))
+	if reqBody.Size >8 {
+		size = reqBody.Size
 	} // 鏉℃暟
-
-	if tableId == "all" || tableId == "" {
-		// / 鎵�鏈変汉鍛�
-	}
 
 	if orderType == "desc" {
 		orderType = "desc"
@@ -238,7 +318,7 @@
 	}
 	var pApi dbapi.DbPersonApi
 	paramBody := map[string]interface{}{
-		"tableId":tableId,
+		"tableId": reqBody.TableId,
 		"orderName":orderName,
 		"orderType":orderType,
 		"contentValue":contentValue,
@@ -253,31 +333,41 @@
 	}
 }
 
+type DbtSearch struct {
+	TableId string `json:"tableId"`
+	OrderName string `json:"orderName"`
+	OrderType string `json:"orderType"`
+	ContentValue string `json:"contentValue"`
+	Page int `json:"page"`
+	Size int `json:"size"`
+}
+
+/*
 // @Summary 鏌ヨ搴曞簱浜哄憳鍒楄〃
 // @Description 鏌ヨ搴撲汉鍛樺垪琛�
 // @Accept  json
 // @Produce json
 // @Tags dbperson 搴曞簱浜哄憳
-// @Param reqMap body map false "{"tableId":"","orderName":"uuid","orderType":"desc","contentValue":"","page":1,"size":8}"
-// @Success 200 {string} json "{"code":200, "msg":"鐩綍缁撴瀯鏁版嵁", "success":true,"data":{}}"
-// @Failure 500 {string} json "{code:500,  msg:"杩斿洖閿欒淇℃伅", success:false,data:{}}"
+// @Param reqMap body controllers.DbtSearch false "{"tableId":"","orderName":"id","orderType":"desc","contentValue":"","page":1,"size":8}"
+// @Success 200 {string} json "{"code":200, "msg":"", "success":true,"data":{}}"
+// @Failure 500 {string} json "{code:500,  msg:"", success:false,data:{}}"
 // @Router /data/api-v/dbperson/queryDbPersonsByCampare [POST]
 func (dbc DbPersonController) QueryDbPersonsByCampare(c *gin.Context) {
 	url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
 		"/" + config.EsInfo.EsIndex.Dbtablepersons.IndexName + "/_search" // ?refresh=wait_for
-	reqBody := make(map[string]interface{}, 5)
+	var reqBody DbtSearch
 	c.BindJSON(&reqBody)
 	tableId := ""
-	if reqBody["tableId"] != nil {
-		tableId = reqBody["tableId"].(string)
+	if reqBody.TableId != "" {
+		tableId = reqBody.TableId
 	}
 	orderName := "_id"
-	if reqBody["orderName"] != nil {
-		orderName = reqBody["orderName"].(string)
+	if reqBody.OrderName != "" {
+		orderName = reqBody.OrderName
 	} // 鍒楀悕
 	orderType := "desc"
-	if reqBody["orderType"] != nil {
-		orderType = reqBody["orderType"].(string)
+	if reqBody.OrderType != "" {
+		orderType = reqBody.OrderType
 	} // 鍒楃被鍨�
 	faceUrl := ""
 	var threshold float32
@@ -319,14 +409,7 @@
 	data := esutil.GetEsDataReq(url, params, true)
 	featByte := make([]byte, 0, 1024)
 	if len(faceUrl) > 3 { //   linux
-		/*fileName := picUrl   // picIp 瀹氫箟鍦� fileController weedfs 鏂囦欢鏈嶅姟鍣� 璁块棶 璺緞 鍓嶇紑
-		detect := gorun.GetSimpleFaceDetect(fileName)
-		if len(detect) != 1 {
-			util.ResponseFormat(c,code.TooManyFeatureFindError,"鍥剧墖鐗瑰緛鍊艰繃澶�")
-			return
-		}else {
-			featByte = detect[0]["feature"].([]byte)
-		}*/ //   linux
+
 	}
 	to := page * size
 	datalist := sourceCompare(data["datalist"].([]interface{}), false, featByte, threshold)
@@ -341,4 +424,4 @@
 	data["total"] = len(datalist)
 	//c.JSON(200, data)
 	util.ResponseFormat(c, code.Success, data)
-}
+}*/

--
Gitblit v1.8.0