| | |
| | | Enable int32 `json:"enable"` |
| | | } |
| | | |
| | | type CompareSortResult []CompareResult |
| | | |
| | | func (s CompareSortResult) Len() int{ |
| | | return len(s) |
| | | } |
| | | func (s CompareSortResult) Swap(i,j int){ |
| | | s[i],s[j] = s[j],s[i] |
| | | } |
| | | func (s CompareSortResult) Less(i,j int) bool{ |
| | | return s[i].CompareScore > s[j].CompareScore |
| | | type ScoreIndex struct { |
| | | CompareScore float32 |
| | | Index int |
| | | } |
| | | |
| | | //填充向前端返回的数据 |
| | | func FillDataToCompareResult(compResult *protomsg.SdkCompareResult) CompareSortResult { |
| | | //resultList :=make([]CompareResult,0) |
| | | var resultList = CompareSortResult{} |
| | | func FillDataToCompareResult(compResult *protomsg.SdkCompareResult) []CompareResult { |
| | | |
| | | dbPersonM := make(map[string]*protomsg.SdkCompareEach, 0) |
| | | captureM := make(map[string]*protomsg.SdkCompareEach, 0) |
| | | personIds :=make([]string,0) |
| | | var resultList = make([]CompareResult, len(compResult.CompareResult)) |
| | | dbPersonM := make(map[string]ScoreIndex, 0) |
| | | captureM := make(map[string]ScoreIndex, 0) |
| | | personIds := make([]string,0) |
| | | captureIds := make([]string,0) |
| | | for _,v :=range compResult.CompareResult{ |
| | | for idx,v :=range compResult.CompareResult{ |
| | | if v.Tableid == service.CaptureTable { |
| | | captureM[v.Id] = v |
| | | captureM[v.Id] = ScoreIndex{ |
| | | Index: idx, |
| | | CompareScore: v.CompareScore, |
| | | } |
| | | captureIds = append(captureIds,v.Id) |
| | | } else { |
| | | dbPersonM[v.Id] = v |
| | | dbPersonM[v.Id] = ScoreIndex{ |
| | | Index: idx, |
| | | CompareScore: v.CompareScore, |
| | | } |
| | | personIds = append(personIds,v.Id) |
| | | } |
| | | } |
| | | logger.Debug("comp len(personIds):", len(personIds)) |
| | | logger.Debug("comp len(captureIds):", len(captureIds)) |
| | | esServerIp := config.EsInfo.Masterip |
| | | esServerPort := config.EsInfo.Httpport |
| | | index := config.EsInfo.EsIndex.Dbtablepersons.IndexName |
| | | |
| | | var dbpersons []protomsg.Dbperson |
| | | if len(personIds) >0 { |
| | | dbpersons, _ = esApi.Dbpersoninfosbyid(personIds, index, esServerIp, esServerPort) |
| | | var dbpApi dbapi.DbPersonApi |
| | | dbpersons, _ = dbpApi.Dbpersoninfosbyid(personIds) |
| | | } |
| | | |
| | | logger.Debug("comp dbpersons.len:", len(dbpersons)) |
| | | if dbpersons !=nil { |
| | | if len(dbpersons) >0 { |
| | | var dtApi dbapi.DbTableApi |
| | | for _,p :=range dbpersons { |
| | | var dbP = DbPersonVo { |
| | | PersonId: p.Id, |
| | |
| | | TableId: p.TableId, |
| | | Enable: p.Enable, |
| | | } |
| | | dbTableInfos, _ := esApi.Dbtablefosbyid([]string{p.TableId}, config.EsInfo.EsIndex.DbTables.IndexName, esServerIp, esServerPort) |
| | | dbTableInfos, _ := dtApi.DbtablesById([]string{ p.TableId }) |
| | | if dbTableInfos !=nil{ |
| | | dbP.BwType = dbTableInfos[0].BwType |
| | | dbP.TableName = dbTableInfos[0].TableName |
| | | } |
| | | var cr = CompareResult{ |
| | | |
| | | resultList[dbPersonM[p.Id].Index] = CompareResult{ |
| | | BaseInfo:[]DbPersonVo{ dbP }, |
| | | } |
| | | resultList = append(resultList,cr) |
| | | } |
| | | } |
| | | if len(captureIds) >0 { |
| | |
| | | BaseInfo: bi, |
| | | SdkName: "人脸", |
| | | } |
| | | resultList = append(resultList,vpE) |
| | | resultList[captureM[vp.Id].Index] = vpE |
| | | } |
| | | } |
| | | sort.Sort(resultList) |
| | | return resultList |
| | | } |
| | | |
| | |
| | | func (controller FileController) SearchByPhoto(c *gin.Context) { |
| | | var searchBody models.EsSearch |
| | | err := c.BindJSON(&searchBody) |
| | | if err !=nil || searchBody.PicUrl == "" || len(searchBody.DataBases) == 0 { |
| | | if err !=nil{ |
| | | util.ResponseFormat(c, code.RequestParamError, "参数有误") |
| | | return |
| | | } |
| | | var faceB []byte |
| | | if searchBody.CaptureId != "" {//做查找此人 |
| | | searchPeople, err := esApi.Dbpersoninfosbyid([]string{searchBody.CaptureId}, config.EsInfo.EsIndex.VideoPersons.IndexName, config.EsInfo.Masterip, config.EsInfo.Httpport) |
| | | if searchBody.CaptureId != "" {//做查找此人,搜所有抓拍和底库 |
| | | searchBase64Fea, err := esApi.GetVideoPersonFaceFeatureById(searchBody.CaptureId, config.EsInfo.EsIndex.VideoPersons.IndexName, config.EsInfo.Masterip, config.EsInfo.Httpport) |
| | | if err !=nil { |
| | | util.ResponseFormat(c, code.ComError, "抓拍数据不存在,请检查") |
| | | return |
| | | } |
| | | if searchPeople !=nil && len(searchPeople) == 1{ |
| | | decodeF, err := base64.StdEncoding.DecodeString(searchPeople[0].FaceFeature) |
| | | if searchBase64Fea !=""{ |
| | | decodeF, err := base64.StdEncoding.DecodeString(searchBase64Fea) |
| | | if err !=nil { |
| | | util.ResponseFormat(c, code.ComError, "本条抓怕特征不是base64,请检查") |
| | | return |
| | | } |
| | | faceB = decodeF |
| | | } |
| | | } else {//做以图搜图 |
| | | if searchBody.PicUrl == "" || len(searchBody.DataBases) == 0 { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数有误") |
| | | return |
| | | } |
| | | if face,ok := faceExtractedMap[searchBody.PicUrl];!ok{ |
| | | util.ResponseFormat(c, code.RequestParamError, "请重新上传图片") |
| | | return |
| | | } else { |
| | | faceB = face.FaceBytes |
| | | } |
| | | } |
| | | //if searchBody.CompareNum !="" { |
| | | // //二次搜索,不需要再比对了 |
| | | // co := service.GetCompResultByNum(searchBody.CompareNum) |
| | | // if co != nil { |
| | | // //二次搜索和排序 |
| | | // twiceM := GetCompareDataTwice(co,&searchBody) |
| | | // util.ResponseFormat(c,code.Success,twiceM) |
| | | // } else { |
| | | // m :=make(map[string]interface{},0) |
| | | // m["compareNum"] = searchBody.CompareNum |
| | | // m["total"] = 0 |
| | | // m["totalList"] = []CompareResult{} |
| | | // util.ResponseFormat(c,code.CompareResultGone,m) |
| | | // } |
| | | //} |
| | | |
| | | var sysSetApi dbapi.SysSetApi |
| | | analyServerId := "" |
| | | flag, sysconf := sysSetApi.GetServerInfo() |
| | |
| | | util.ResponseFormat(c, code.ComError, "analyServerId为空,配置有误") |
| | | return |
| | | } |
| | | if searchBody.CaptureId == ""{ |
| | | if face,ok := faceExtractedMap[searchBody.PicUrl];!ok{ |
| | | util.ResponseFormat(c, code.RequestParamError, "请重新上传图片") |
| | | return |
| | | } else { |
| | | faceB = face.FaceBytes |
| | | } |
| | | } |
| | | |
| | | if faceB == nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "请重新上传图片") |
| | | return |
| | |
| | | CompareThreshold: searchBody.Threshold, |
| | | } |
| | | |
| | | var hasCompEsPerson = false |
| | | if searchBody.DataBases !=nil { |
| | | for idx,tableId :=range searchBody.DataBases { |
| | | if tableId == "esData" { |
| | | searchBody.DataBases = append(searchBody.DataBases[:idx], searchBody.DataBases[idx+1:]...) |
| | | searchBody.DataBases = append(searchBody.DataBases,service.CaptureTable) |
| | | hasCompEsPerson = true |
| | | break |
| | | } |
| | | } |
| | | arg.TableIds = searchBody.DataBases |
| | | } else { |
| | | arg.TableIds = []string{} |
| | | } |
| | | arg.Source = true // 标识来源是web |
| | | arg.AlarmLevel = searchBody.AlarmLevel |
| | |
| | | timeStart := time.Now() |
| | | |
| | | compareService := service.NewFaceCompareService(arg) |
| | | compareOnce := compareService.Compare() |
| | | totalData := compareOnce.CompareData |
| | | var totalData service.CompareList |
| | | if len(arg.TableIds) >0 {//有比对底库 |
| | | dbPersonTargets := compareService.CompareDbPersons() |
| | | if dbPersonTargets !=nil { |
| | | totalData = append(totalData,*dbPersonTargets...) |
| | | } |
| | | } |
| | | if hasCompEsPerson {//有比对Es抓拍 |
| | | esPersons := compareService.CompareVideoPersons() |
| | | if esPersons !=nil { |
| | | totalData = append(totalData, *esPersons...) |
| | | } |
| | | } |
| | | |
| | | logger.Debug("comp 比对结束,用时:",time.Since(timeStart)) |
| | | service.SetCompResultByNum(&service.CompareOnce{ |
| | | CompareNum: compareService.CompareNum, |
| | | CompareData: &totalData, |
| | | }) |
| | | |
| | | m := make(map[string]interface{},3) |
| | | if totalData != nil && totalData.Len() > 0{ |
| | | //按分值排序 |
| | | var scorePre []float32 |
| | | for _,f1 :=range *totalData { |
| | | scorePre = append(scorePre, f1.CompareScore) |
| | | } |
| | | //logger.Debug("comp 排序前分值数组:",scorePre) |
| | | sort.Sort(totalData) |
| | | var scoreAf []float32 |
| | | for _,f2 :=range *totalData { |
| | | scoreAf = append(scoreAf, f2.CompareScore) |
| | | } |
| | | //logger.Debug("comp 排序后分值数组:",scoreAf) |
| | | total := totalData.Len() |
| | | |
| | | m["compareNum"] = compareOnce.CompareNum |
| | | m["compareNum"] = compareService.CompareNum |
| | | m["total"] = total |
| | | var sCompResult protomsg.SdkCompareResult |
| | | if total <= searchBody.Size { |
| | | sCompResult.CompareResult = *totalData |
| | | sCompResult.CompareResult = totalData |
| | | } else { |
| | | sCompResult.CompareResult = (*totalData)[0:searchBody.Size] |
| | | sCompResult.CompareResult = totalData[0:searchBody.Size] |
| | | } |
| | | resultList := FillDataToCompareResult(&sCompResult) |
| | | m["totalList"] = resultList |
| | |
| | | |
| | | } else { |
| | | m["total"] = 0 |
| | | m["compareNum"] = searchBody.CompareNum |
| | | m["compareNum"] = compareService.CompareNum |
| | | m["totalList"] = []CompareResult{} |
| | | } |
| | | util.ResponseFormat(c,code.Success,m) |
| | |
| | | |
| | | func uploadFileReturnAddr(file multipart.File, filename string, tableId string) (string, map[string]interface{}, error) { |
| | | defer file.Close() |
| | | field := "" |
| | | // weedfs 上传 |
| | | fileBytes, err := ioutil.ReadAll(file) |
| | | if err !=nil { |
| | |
| | | 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() |
| | | fileInfo, e := esutil.PostFormData(weedfsUri, filename, "file", cutFaceImgData) |
| | | weedFilePath, e := WeedFSClient.UploadFile(weedfsUri, filename, cutFaceImgData) |
| | | logger.Debug("上传到weedfs用时:", time.Since(t1)) |
| | | t1 = time.Now() |
| | | if e != nil { |
| | | fmt.Println(e.Error()) |
| | | return "", nil, e |
| | | } else { |
| | | field = fileInfo[picUrlField].(string) // 文件路径 |
| | | } |
| | | if strings.Contains(field,"/"){ |
| | | idx := strings.Index(field, "/") |
| | | field = field[idx+1:] |
| | | } |
| | | |
| | | ext := path.Ext(filename) |
| | | fileNameOnly := strings.TrimSuffix(filename, ext) |
| | | |
| | | dbperson := new(models.Dbtablepersons) |
| | | dbperson.PersonPicUrl = field // 图片路经 |
| | | dbperson.PersonPicUrl = weedFilePath // 图片路经 |
| | | dbperson.TableId = tableId // |
| | | dbperson.PersonName = fileNameOnly // 图片名 |
| | | dbperson.Enable = 1 //默认有效 |
| | | // 演示base64编码 |
| | | |
| | | dbperson.FaceFeature = faceBase64 // 特征值base64 码 |
| | | |
| | | result := addDbPerson(dbperson) |
| | |
| | | addResult["successList"] = successList |
| | | addResult["failList"] = failList |
| | | addResult["fields"] = extNames |
| | | //addResult["detail"] = details |
| | | //fields := fdfsclient.UploadFileByBuffer100(bytess, extNames) // fastdfs 上传 |
| | | |
| | | if len(successList)>0 { |
| | | util.ResponseFormat(c, code.DbPersonUploadSuccess, addResult) |
| | | } else { |
| | | util.ResponseFormat(c, code.ServiceInsideError, err.Error()) |
| | | util.ResponseFormat(c, code.DbPersonUploadFail, addResult) |
| | | } |
| | | } |
| | | |