From e9e0c1f3abf1bbf83ebdb933d318caa2a45c15b0 Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期六, 30 三月 2024 17:14:11 +0800
Subject: [PATCH] 调整代码结构, 精简比对过程
---
Makefile | 20 -
db/personStatus.go | 27 +
util/util.go | 35 +
/dev/null | 80 -----
db/base.go | 18 +
compare/compare.go | 103 ++++++
config/config.go | 53 --
cache/shardmap/shardmap.go | 61 +---
compare/faceSdk.go | 2
config/compare.yaml | 13
db/person.go | 87 +++++
cache/cache.go | 212 ++++++++++++++
main.go | 112 +++----
db/db.go | 39 ++
14 files changed, 609 insertions(+), 253 deletions(-)
diff --git a/Makefile b/Makefile
index 1e38d14..36c1672 100644
--- a/Makefile
+++ b/Makefile
@@ -1,26 +1,12 @@
BUILD_TIME := $(shell date "+%F %T")
COMMIT_SHA1 := $(shell git rev-parse HEAD)
-APP_NAME := sdkCompare
-BUILD_VERSION := 1.0.0
+APP_NAME := faceCompare
+BUILD_VERSION := 2.0.0
PLATFORM := x86_64
export LD_LIBRARY_PATH=/opt/vasystem/libs/FaceDetect
-ifeq ("$(PLATFORM)", "aarch64")
- $(info CUR_PLATFORM:$(PLATFORM))
- export CGO_ENABLED=1
- export GOOS=linux
- export GOARCH=arm64
- export CC=/opt/l4t-gcc/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc
- export CXX=/opt/l4t-gcc/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc
- export CGO_CFLAGS=--sysroot=/opt/l4t-gcc/sysroot-glibc-linaro-2.25-2018.05-aarch64-linux-gnu
- export CGO_CXXFLAGS=--sysroot=/opt/l4t-gcc/sysroot-glibc-linaro-2.25-2018.05-aarch64-linux-gnu
- export CGO_LDFLAGS=--sysroot=/opt/l4t-gcc/sysroot-glibc-linaro-2.25-2018.05-aarch64-linux-gnu -lstdc++
-else
- CUR_OS :=$(shell uname -m)
- $(info CUR_PLATFORM:$(CUR_OS))
-endif
all:
- go build -a -ldflags "-X 'basic.com/valib/version.git.BuildVersion=${BUILD_VERSION}' -X 'basic.com/valib/version.git.BuildTime=${BUILD_TIME}' -X 'basic.com/valib/version.git.CommitSha1=${COMMIT_SHA1}' -X 'basic.com/valib/version.git.AppName=${APP_NAME}'" -o ${APP_NAME}
+ go build -v -ldflags "-X 'basic.com/valib/version.git.BuildVersion=${BUILD_VERSION}' -X 'basic.com/valib/version.git.BuildTime=${BUILD_TIME}' -X 'basic.com/valib/version.git.CommitSha1=${COMMIT_SHA1}' -X 'basic.com/valib/version.git.AppName=${APP_NAME}'" -o ${APP_NAME}
clean:
rm -f ${APP_NAME}
.PHONY:
diff --git a/cache/cache.go b/cache/cache.go
new file mode 100644
index 0000000..76e8634
--- /dev/null
+++ b/cache/cache.go
@@ -0,0 +1,212 @@
+package cache
+
+import (
+ "flag"
+ "sync"
+ "time"
+
+ "sdkCompare/cache/shardmap"
+ "sdkCompare/db"
+
+ "basic.com/pubsub/protomsg.git"
+ "basic.com/valib/logger.git"
+)
+
+var querynum = flag.Int("querynum", 3000, "the query number from database")
+var threadnum = flag.Int("threadnum", 32, "the number of thread to deal data.")
+
+var (
+ Unfiled = "unfiled"
+ PRE_DBTABLE = "dbTable_"
+)
+
+type CmapItem struct {
+ sync.Mutex
+ Area map[string]*shardmap.ShardMap
+}
+
+var CacheMap *CmapItem
+var doOnce sync.Once
+
+func ReInitDbTablePersonsCache() {
+ CacheMap.Lock()
+ defer CacheMap.Unlock()
+ if CacheMap == nil {
+ CacheMap = &CmapItem{
+ Area: make(map[string]*shardmap.ShardMap),
+ }
+ }
+ for tableId, _ := range CacheMap.Area {
+ delete(CacheMap.Area, tableId)
+ }
+
+ initDbTablePersonsCache()
+}
+
+func InitDbTablePersons() {
+ doOnce.Do(func() {
+ flag.Parse()
+
+ CacheMap = &CmapItem{
+ Area: make(map[string]*shardmap.ShardMap),
+ }
+
+ // 鍒濆鍖栨湭鍒嗙被, 娌℃湁灏忓尯id鐨勬。妗�
+ CacheMap.Area[Unfiled] = shardmap.New(uint8(*threadnum))
+ })
+
+ initDbTablePersonsCache()
+}
+
+func initDbTablePersonsCache() {
+ // 缂撳瓨搴曞簱涓殑鍏ㄩ儴浜哄憳淇℃伅
+ var dbpApi db.DbPersons
+ total, e := dbpApi.GetPersonTotal("")
+
+ var psApi db.PersonStatus
+ accessAreas, _ := psApi.GetPersonAccessedAreas()
+
+ logger.Debugf("鎵�鏈夊簳搴撳叡鏈�%d鏉¤褰�", total)
+ if e == nil && total > 0 {
+ queryEachNum := *querynum
+ qn := int(total) / *threadnum
+ if *querynum < qn {
+ queryEachNum = qn
+ }
+ queryT := int(total) / queryEachNum
+ if int(total)%queryEachNum > 0 {
+ queryT++
+ }
+ temptime := time.Now()
+ var wg sync.WaitGroup
+
+ for i := 0; i < queryT; i++ {
+ j := i * queryEachNum
+ wg.Add(1)
+ go func(qs int) {
+ defer wg.Done()
+ dbPersons, err := dbpApi.GetPersonsCompareCacheBase(j, queryEachNum)
+ if err != nil {
+ logger.Error(err)
+ return
+ }
+ logger.Debugf("鑾峰彇%d鏉′汉鍛樹俊鎭�", len(dbPersons))
+ CacheMap.Lock()
+
+ areaId := ""
+ for _, value := range dbPersons {
+ areaId = value.AreaId
+ // 娌℃湁灏忓尯id鐨勪汉鍛�
+ if areaId == "" {
+ CacheMap.Area[Unfiled].Set(value.Id, value)
+ CacheMap.Area[Unfiled].Settime()
+ continue
+ }
+
+ for _, areaId := range accessAreas[value.Id] {
+ if _, ok := CacheMap.Area[areaId]; !ok {
+ CacheMap.Area[areaId] = shardmap.New(uint8(*threadnum))
+ }
+
+ CacheMap.Area[areaId].Set(value.Id, value)
+ CacheMap.Area[areaId].Settime()
+ }
+ }
+
+ CacheMap.Unlock()
+
+ }(j)
+ }
+ wg.Wait()
+ logger.Debug("搴曞簱浜哄憳缂撳瓨瀹屾垚鐢ㄦ椂:", time.Since(temptime))
+ }
+}
+
+// UpdateDbPersonsCacheById 鏇存柊缂撳瓨涓殑鍏ㄩ儴浜哄憳淇℃伅
+func UpdateDbPersonsCacheById(id string) {
+ var dbpApi db.DbPersons
+ info, err := dbpApi.GetPersonsCompareCacheById(id)
+ if err != nil {
+ logger.Error(err)
+ return
+ }
+ if info.Tableid != "" {
+ CacheMap.Lock()
+ defer CacheMap.Unlock()
+ if _, ok := CacheMap.Area[info.Tableid]; !ok {
+ CacheMap.Area[info.Tableid] = shardmap.New(uint8(*threadnum))
+ }
+ CacheMap.Area[info.Tableid].Set(info.Id, info)
+ CacheMap.Area[info.Tableid].Settime()
+ }
+}
+
+func RealTimeAddPersonInfoToCache(tableId string, id string, faceFeature string, enable int32, carNo string) {
+ CacheMap.Lock()
+ defer CacheMap.Unlock()
+ if _, ok := CacheMap.Area[tableId]; !ok {
+ CacheMap.Area[tableId] = shardmap.New(uint8(*threadnum))
+ }
+ var ei = protomsg.Esinfo{
+ Id: id,
+ Tableid: tableId,
+ FaceFeature: faceFeature,
+ Enable: enable,
+ CarNo: carNo,
+ }
+ CacheMap.Area[tableId].Set(id, &ei)
+ logger.Debug("id:", id, ",tableId:", ",len(faceFeature):", len(faceFeature), ",tableId:", tableId, ",enable:", enable)
+}
+
+func RealTimeDelPersonFromCache(tableId string, id string) {
+ logger.Debug("DelPersonFromCache,tableId:", tableId, ",id:", id)
+ CacheMap.Lock()
+ defer CacheMap.Unlock()
+ if _, ok := CacheMap.Area[tableId]; ok {
+ CacheMap.Area[tableId].Del(id)
+ logger.Debug("DelPerson ok success")
+ } else {
+ logger.Error("tableId:", tableId, " not exist")
+ }
+}
+
+func RealTimeDelTable(tableId string) {
+ logger.Debug("RealTimeDelTable tableId:", tableId)
+ CacheMap.Lock()
+ defer CacheMap.Unlock()
+
+ if dtM, ok := CacheMap.Area[PRE_DBTABLE]; ok {
+ dtM.Del(tableId)
+ }
+ if _, ok := CacheMap.Area[tableId]; ok {
+ delete(CacheMap.Area, tableId)
+ }
+}
+
+// 浣垮簳搴撶敓鏁�,灏嗗簳搴撲腑鐨勬墍鏈夌敓鏁堢姸鎬佺殑浜虹壒寰佹坊鍔犲埌缂撳瓨
+func RealTimeUpdateTable(tableId string, enable int32) {
+ logger.Debug("RealTimeUpdateTable tableId:", tableId, ",enable:", enable)
+ CacheMap.Lock()
+ defer CacheMap.Unlock()
+
+ if _, ok := CacheMap.Area[PRE_DBTABLE]; !ok {
+ CacheMap.Area[PRE_DBTABLE] = shardmap.New(uint8(*threadnum))
+ }
+ CacheMap.Area[PRE_DBTABLE].Set(tableId, enable == 1)
+}
+
+func UpdateCache(changeMsg *protomsg.EsPersonCacheChange) {
+ if changeMsg.Type == protomsg.EsCacheChanged_T_DbTable {
+ if changeMsg.Action == protomsg.DbAction_Insert || changeMsg.Action == protomsg.DbAction_Update {
+ RealTimeUpdateTable(changeMsg.TableId[0], changeMsg.Enable)
+ } else if changeMsg.Action == protomsg.DbAction_Delete {
+ RealTimeDelTable(changeMsg.TableId[0])
+ }
+ } else if changeMsg.Type == protomsg.EsCacheChanged_T_DbTablePerson {
+ if changeMsg.Action == protomsg.DbAction_Insert || changeMsg.Action == protomsg.DbAction_Update {
+ RealTimeAddPersonInfoToCache(changeMsg.TableId[0], changeMsg.PersonId, changeMsg.Feature, changeMsg.Enable, changeMsg.CarNo)
+ } else if changeMsg.Action == protomsg.DbAction_Delete {
+ RealTimeDelPersonFromCache(changeMsg.TableId[0], changeMsg.PersonId)
+ }
+ }
+}
diff --git a/cache/compare.go b/cache/compare.go
deleted file mode 100644
index 4d45b87..0000000
--- a/cache/compare.go
+++ /dev/null
@@ -1,360 +0,0 @@
-package cache
-
-import (
- "encoding/base64"
- "flag"
- "fmt"
- "sdkCompare/face"
- "strconv"
- "strings"
- "sync"
- "time"
-
- "basic.com/pubsub/protomsg.git"
- "basic.com/valib/logger.git"
- "github.com/golang/protobuf/proto"
- "sdkCompare/cache/shardmap"
-)
-
-var querynum = flag.Int("querynum", 3000, "the query number from database")
-var threadnum = flag.Int("threadnum", 32, "the number of thread to deal data.")
-
-var (
- thresholdLimit = float32(50)
- captureTable = "capturetable"
- PRE_DBTABLE = "dbTable_"
- PRE_CAPTURE_SERVER = "captureServer_"
-)
-
-type CmapItem struct {
- sync.Mutex
- Cam map[string]*shardmap.ShardMap
-}
-
-var Cmap *CmapItem
-var doOnce sync.Once
-
-func ReInitDbTablePersonsCache() {
- Cmap.Lock()
- defer Cmap.Unlock()
- if Cmap == nil {
- Cmap = &CmapItem{
- Cam: make(map[string]*shardmap.ShardMap),
- }
- }
- for tableId, _ := range Cmap.Cam {
- delete(Cmap.Cam, tableId)
- }
-
- initDbTablePersonsCache()
-}
-
-func InitDbTablePersons() {
- doOnce.Do(func() {
- flag.Parse()
-
- Cmap = &CmapItem{
- Cam: make(map[string]*shardmap.ShardMap),
- }
- })
-
- initDbTablePersonsCache()
-}
-
-func initDbTablePersonsCache() {
- // 鏌ヨ鎵�鏈夌殑搴曞簱鍒楄〃
- var dtApi DbTables
- allTables, err := dtApi.FindAllDbTablesByCurServer()
- if err == nil && allTables != nil && len(allTables) > 0 { //鍒濆鍖栧簳搴撶紦瀛樹俊鎭�
- Cmap.Lock()
- for _, table := range allTables {
- if _, ok := Cmap.Cam[PRE_DBTABLE]; !ok {
- Cmap.Cam[PRE_DBTABLE] = shardmap.New(uint8(*threadnum))
- }
- Cmap.Cam[PRE_DBTABLE].Set(table.Id, table.Enable == 1)
- logger.Debugf("鍒濆鍖栧簳搴揫%s][%s]鍒扮紦瀛樹腑", table.Id, table.TableName)
- }
- Cmap.Unlock()
- }
-
- // 缂撳瓨搴曞簱涓殑鍏ㄩ儴浜哄憳淇℃伅
- var dbpApi DbPersons
- total, e := dbpApi.GetPersonTotal("")
- logger.Debugf("鎵�鏈夊簳搴撳叡鏈�%d鏉¤褰�", total)
- if e == nil && total > 0 {
- queryEachNum := *querynum
- qn := int(total) / *threadnum
- if *querynum < qn {
- queryEachNum = qn
- }
- queryT := int(total) / queryEachNum
- if int(total)%queryEachNum > 0 {
- queryT++
- }
- temptime := time.Now()
- var wg sync.WaitGroup
-
- for i := 0; i < queryT; i++ {
- j := i * queryEachNum
- wg.Add(1)
- go func(qs int) {
- defer wg.Done()
- dbpersons, err := dbpApi.GetPersonsCompareCacheBase(j, queryEachNum)
- if err != nil {
- logger.Error(err)
- return
- }
- logger.Debugf("鑾峰彇%d鏉′汉鍛樹俊鎭�", len(dbpersons))
- Cmap.Lock()
- tableId := ""
- for _, value := range dbpersons {
- tableId = value.Tableid
- if _, ok := Cmap.Cam[value.Tableid]; !ok {
- Cmap.Cam[value.Tableid] = shardmap.New(uint8(*threadnum))
- }
-
- Cmap.Cam[value.Tableid].Set(value.Id, value)
- }
-
- if len(dbpersons) != 0 {
- Cmap.Cam[tableId].Settime()
- }
-
- Cmap.Unlock()
-
- }(j)
- }
- wg.Wait()
- logger.Debug("搴曞簱浜哄憳缂撳瓨瀹屾垚鐢ㄦ椂:", time.Since(temptime))
- }
-}
-
-// UpdateDbPersonsCacheById 鏇存柊缂撳瓨涓殑鍏ㄩ儴浜哄憳淇℃伅
-func UpdateDbPersonsCacheById(id string) {
- var dbpApi DbPersons
- info, err := dbpApi.GetPersonsCompareCacheById(id)
- if err != nil {
- logger.Error(err)
- return
- }
- if info.Tableid != "" {
- Cmap.Lock()
- defer Cmap.Unlock()
- if _, ok := Cmap.Cam[info.Tableid]; !ok {
- Cmap.Cam[info.Tableid] = shardmap.New(uint8(*threadnum))
- }
- Cmap.Cam[info.Tableid].Set(info.Id, info)
- Cmap.Cam[info.Tableid].Settime()
- }
-}
-
-func RealTimeAddPersonInfoToCache(tableId string, id string, faceFeature string, enable int32, carNo string) {
- Cmap.Lock()
- defer Cmap.Unlock()
- if _, ok := Cmap.Cam[tableId]; !ok {
- Cmap.Cam[tableId] = shardmap.New(uint8(*threadnum))
- }
- var ei = protomsg.Esinfo{
- Id: id,
- Tableid: tableId,
- FaceFeature: faceFeature,
- Enable: enable,
- CarNo: carNo,
- }
- Cmap.Cam[tableId].Set(id, &ei)
- logger.Debug("id:", id, ",tableId:", ",len(faceFeature):", len(faceFeature), ",tableId:", tableId, ",enable:", enable)
-}
-
-func RealTimeDelPersonFromCache(tableId string, id string) {
- logger.Debug("DelPersonFromCache,tableId:", tableId, ",id:", id)
- Cmap.Lock()
- defer Cmap.Unlock()
- if _, ok := Cmap.Cam[tableId]; ok {
- Cmap.Cam[tableId].Del(id)
- logger.Debug("DelPerson ok success")
- } else {
- logger.Error("tableId:", tableId, " not exist")
- }
-}
-
-func RealTimeDelTable(tableId string) {
- logger.Debug("RealTimeDelTable tableId:", tableId)
- Cmap.Lock()
- defer Cmap.Unlock()
-
- if dtM, ok := Cmap.Cam[PRE_DBTABLE]; ok {
- dtM.Del(tableId)
- }
- if _, ok := Cmap.Cam[tableId]; ok {
- delete(Cmap.Cam, tableId)
- }
-}
-
-// 浣垮簳搴撶敓鏁�,灏嗗簳搴撲腑鐨勬墍鏈夌敓鏁堢姸鎬佺殑浜虹壒寰佹坊鍔犲埌缂撳瓨
-func RealTimeUpdateTable(tableId string, enable int32) {
- logger.Debug("RealTimeUpdateTable tableId:", tableId, ",enable:", enable)
- Cmap.Lock()
- defer Cmap.Unlock()
-
- if _, ok := Cmap.Cam[PRE_DBTABLE]; !ok {
- Cmap.Cam[PRE_DBTABLE] = shardmap.New(uint8(*threadnum))
- }
- Cmap.Cam[PRE_DBTABLE].Set(tableId, enable == 1)
-}
-
-func UpdateCache(changeMsg *protomsg.EsPersonCacheChange) {
- if changeMsg.Type == protomsg.EsCacheChanged_T_DbTable {
- if changeMsg.Action == protomsg.DbAction_Insert || changeMsg.Action == protomsg.DbAction_Update {
- RealTimeUpdateTable(changeMsg.TableId[0], changeMsg.Enable)
- } else if changeMsg.Action == protomsg.DbAction_Delete {
- RealTimeDelTable(changeMsg.TableId[0])
- }
- } else if changeMsg.Type == protomsg.EsCacheChanged_T_DbTablePerson {
- if changeMsg.Action == protomsg.DbAction_Insert || changeMsg.Action == protomsg.DbAction_Update {
- RealTimeAddPersonInfoToCache(changeMsg.TableId[0], changeMsg.PersonId, changeMsg.Feature, changeMsg.Enable, changeMsg.CarNo)
- } else if changeMsg.Action == protomsg.DbAction_Delete {
- RealTimeDelPersonFromCache(changeMsg.TableId[0], changeMsg.PersonId)
- }
- }
-}
-
-func GetComparePersonBaseInfo(compareArgs protomsg.CompareArgs) []byte {
- if compareArgs.FaceFeature == nil {
- return nil
- }
-
- //鎸囧畾鏈�浣庡垎
- baseScore := thresholdLimit
- if compareArgs.CompareThreshold > thresholdLimit {
- baseScore = compareArgs.CompareThreshold
- }
-
- if compareArgs.IsCompareAll {
- baseScore = 0
- }
-
- var scResult protomsg.SdkCompareResult
-
- //鏈寚瀹氭瘮瀵圭洰鏍噈ap
- if compareArgs.TableIds == nil || len(compareArgs.TableIds) == 0 {
- logger.Debugf("鎺ユ敹鍒板簳搴撴瘮瀵硅姹�, 闃堝��:%f", compareArgs.CompareThreshold)
-
- // 姣斿鏉ユ簮鏄痳uleprocess锛屼紶绌烘瘮鍏ㄩ儴搴曞簱
- if !compareArgs.Source {
- for key, val := range Cmap.Cam {
- // 鍒ゆ柇鏄惁灞炰簬搴曞簱
- if tShard, hasT := Cmap.Cam[PRE_DBTABLE]; hasT {
- if tEnable, tOk := tShard.Get(key); tOk { //瀛樺湪姝ゅ簳搴�
- //搴曞簱鏈夋晥
- if tEnable.(bool) {
- targets := val.Walk(DoSdkCompare, compareArgs.FaceFeature, baseScore, compareArgs.Source, compareArgs.CompareTarget)
- if len(targets) > 0 {
- scResult.CompareResult = append(scResult.CompareResult, targets...)
- }
- }
- }
- }
- }
- } else { //鏉ユ簮鏄痺eb锛屾瘮瀵规墍鏈夋姄鎷嶅拰搴曞簱
- for cKey, val := range Cmap.Cam {
- if compareArgs.AnalyServerId != "" { //姣斿鎸囧畾server浜х敓鐨勬姄鎷嶆暟鎹拰搴曞簱
- if cKey == PRE_CAPTURE_SERVER+compareArgs.AnalyServerId || !strings.HasPrefix(cKey, PRE_CAPTURE_SERVER) {
- targets := val.Walk(DoSdkCompare, compareArgs.FaceFeature, baseScore, compareArgs.Source, compareArgs.CompareTarget)
- if len(targets) > 0 {
- scResult.CompareResult = append(scResult.CompareResult, targets...)
- }
- }
- } else { //绠$悊骞冲彴璇锋眰锛屾瘮瀵规墍鏈夋暟鎹�
- if len(compareArgs.ServerIds) > 0 {
- for _, termDevId := range compareArgs.ServerIds {
- if cKey == PRE_CAPTURE_SERVER+termDevId || !strings.HasPrefix(cKey, PRE_CAPTURE_SERVER) {
- targets := val.Walk(DoSdkCompare, compareArgs.FaceFeature, baseScore, compareArgs.Source, compareArgs.CompareTarget)
- if len(targets) > 0 {
- scResult.CompareResult = append(scResult.CompareResult, targets...)
- }
- }
- }
- } else {
- targets := val.Walk(DoSdkCompare, compareArgs.FaceFeature, baseScore, compareArgs.Source, compareArgs.CompareTarget)
- if len(targets) > 0 {
- scResult.CompareResult = append(scResult.CompareResult, targets...)
- }
- }
- }
- }
- }
- } else { //鎸囧畾姣斿鐩爣map
- if !compareArgs.Source {
- for _, tid := range compareArgs.TableIds { //ruleProcess姣斿鎸囧畾搴曞簱
- shardins, ok := Cmap.Cam[tid]
- if !ok {
- logger.Error("ruleProcess compare get shard error by tableId:", tid)
- continue
- }
- if tShard, hasT := Cmap.Cam[PRE_DBTABLE]; hasT {
- if tEnable, tOk := tShard.Get(tid); tOk { //瀛樺湪姝ゅ簳搴�
- logger.Debug("ruleProcess compare tables,exist tableId:", tid, ",enable:", tEnable)
- if tEnable.(bool) { //搴曞簱鏈夋晥
- targets := shardins.Walk(DoSdkCompare, compareArgs.FaceFeature, baseScore, compareArgs.Source, compareArgs.CompareTarget)
- if len(targets) > 0 {
- scResult.CompareResult = append(scResult.CompareResult, targets...)
- }
- }
- } else {
- logger.Error("ruleProcess compare tables,tShard not exist tableId:", tid)
- }
- } else {
- logger.Error("ruleProcess compare tables,PRE_DBTABLE tableId:", tid, " not exist")
- }
- }
- } else { //web璇锋眰锛屾瘮瀵规寚瀹氱殑鎶撴媿搴撴垨鑰呭簳搴�
- for _, tid := range compareArgs.TableIds {
- shardins, ok := Cmap.Cam[tid]
- if !ok {
- logger.Error("get shard error by tableId:", tid)
- continue
- }
-
- targets := shardins.Walk(DoSdkCompare, compareArgs.FaceFeature, baseScore, compareArgs.Source, compareArgs.CompareTarget)
- if len(targets) > 0 {
- scResult.CompareResult = append(scResult.CompareResult, targets...)
- }
- }
- }
- }
-
- logger.Debug("姣斿缁撴灉: len(scResult.CompareResult):", len(scResult.CompareResult))
- buf, err := proto.Marshal(&scResult)
-
- if err != nil {
- logger.Error("scResult Marshal error!", err)
- return nil
- }
-
- return buf
-}
-
-func DoSdkCompare(ci []byte, co string) float32 {
- co_d, err := base64.StdEncoding.DecodeString(co)
- if err != nil {
- logger.Error("DoSdkCompare err:", err)
- return -1
- }
- sec := face.DecCompare(ci, co_d)
- //logger.Debug("姣斿寰楀垎涓猴細", sec)
-
- sec = ParseScore(sec)
- return sec
-}
-
-func ParseScore(compareScore float32) float32 {
- if compareScore <= 1 {
- compareScore = compareScore * 100
- }
- if compareScore == 100 {
- return 100
- }
- f, _ := strconv.ParseFloat(fmt.Sprintf("%2.2f", compareScore), 32)
-
- return float32(f)
-}
diff --git a/cache/db.go b/cache/db.go
deleted file mode 100644
index c07cd80..0000000
--- a/cache/db.go
+++ /dev/null
@@ -1,146 +0,0 @@
-package cache
-
-import (
- "fmt"
- "strconv"
-
- "sdkCompare/config"
-
- "basic.com/pubsub/protomsg.git"
- "basic.com/valib/logger.git"
- "gorm.io/driver/mysql"
- "gorm.io/gorm"
-)
-
-var db *gorm.DB
-
-func ConnectDB() error {
- var err error
-
- dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4",
- config.DbPersonCompInfo.Username,
- config.DbPersonCompInfo.Password,
- config.DbPersonCompInfo.MysqlAddr,
- config.DbPersonCompInfo.Database)
-
- db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
- // 绂佺敤澶栭敭(鎸囧畾澶栭敭鏃朵笉浼氬湪mysql鍒涘缓鐪熷疄鐨勫閿害鏉�)
- DisableForeignKeyConstraintWhenMigrating: true,
- //// 鎸囧畾琛ㄥ墠缂�
- //NamingStrategy: schema.NamingStrategy{
- // TablePrefix: config.Conf.Mysql.TablePrefix + "_",
- //},
- })
-
- if err != nil {
- logger.Error("mysql database open err: %s", err.Error())
- return err
- }
-
- return nil
-}
-
-type BaseEntity struct {
- Id string `gorm:"primary_key;column:id" json:"id" example:""`
- CreateTime string `gorm:"column:createTime" json:"createTime,omitempty" example:""`
- UpdateTime string `gorm:"column:updateTime" json:"updateTime,omitempty" example:""`
- CreateBy string `gorm:"column:createBy" json:"createBy,omitempty" example:""`
- IsDelete int `gorm:"column:isDelete" json:"isDelete" example:"0 鏈垹闄� 1宸插垹闄�"`
- Enable int `gorm:"column:enable" json:"enable" example:" 1鐢熸晥 0鏈敓鏁�"`
-}
-
-type DbTables struct {
- BaseEntity
- TableName string `gorm:"column:tableName" json:"tableName" example:"搴曞簱123"`
- TableDesc string `gorm:"column:tableDesc" json:"tableDesc" example:"搴曞簱鎻忚堪"`
- TableType string `gorm:"column:tableType" json:"tableType" example:"person,car"`
- BwType string `gorm:"column:bwType" json:"bwType" example:"榛戝悕鍗�:1,鐧藉悕鍗�:0"`
- StartTime string `gorm:"column:startTime" json:"startTime" example:"2019-01-12 12:14:56"`
- EndTime string `gorm:"column:endTime" json:"endTime" example:"2019-10-12 12:14:56"`
- IsSync string `gorm:"column:isSync" json:"isSync" example:"1:鍚屾鍒扮鐞嗗钩鍙帮紝2锛氫笉鍚屾"`
- AnalyServerId string `gorm:"column:analyServerId" json:"analyServerId" example:"鏈湴搴撴墍灞炵殑鍒嗘瀽璁惧id锛屽鏋滄槸鍚屾搴撳氨涓嶉渶瑕佽褰�"`
-}
-
-type DbPersons struct {
- BaseEntity
- TableId string `gorm:"column:tableId" json:"tableId" example:"搴撹〃id"`
- FaceFeature string `gorm:"column:faceFeature" json:"faceFeature" example:"浜鸿劯鐗瑰緛鍊硷紙杞︿富鐗瑰緛锛�"`
- PersonPicUrl string `gorm:"column:personPicUrl" json:"personPicUrl" example:"鍥剧墖璺緞,锛堣溅涓荤収鐗囷級"`
- PersonName string `gorm:"column:personName" json:"personName" example:"浜哄憳濮撳悕锛岋紙杞︿富濮撳悕锛�"`
- Age string `gorm:"column:age" json:"age" example:"骞撮緞"`
- Sex string `gorm:"column:sex" json:"sex" example:"鎬у埆 鐢� 濂筹紙杞︿富鎬у埆锛�"`
- IdCard string `gorm:"column:idCard" json:"idCard" example:"韬唤璇侊紙杞︿富韬唤璇侊級"`
- PhoneNum string `gorm:"column:phoneNum" json:"phoneNum" example:"鎵嬫満鍙风爜"`
- MonitorLevel string `gorm:"column:monitorLevel" json:"monitorLevel" example:"绛夌骇"`
- PicDesc string `gorm:"column:picDesc" json:"picDesc" example:"鐓х墖鏍囪瘑"`
- Reserved string `gorm:"column:reserved" json:"reserved" example:"鍏朵粬"`
-
- FromServerId string `gorm:"column:fromServerId" json:"fromServerId,omitempty" example:"鍏ュ簱serverId"`
-}
-
-type FeatureCacheBase struct {
- Id string `json:"id"`
- TableId string `json:"tableid"`
- FaceFeature string `json:"faceFeature"`
- Enable int32 `json:"enable"`
-
- CarNo string `json:"carNo"`
-}
-
-func (dt *DbTables) FindAllDbTablesByCurServer() (arr []DbTables, err error) {
- sql := "select * from dbtables where isDelete = 0"
- err = db.Raw(sql).Find(&arr).Error
- if err != nil {
- return nil, err
- }
-
- return
-}
-
-func (dbp *DbPersons) GetPersonTotal(tableId string) (total int64, err error) {
- sql := "select count(1) as total from dbtablepersons where isDelete=0 and tableId in (select id from dbtables where isDelete=0)"
- if tableId != "" {
- sql += " and tableId='" + tableId + "'"
- }
- err = db.Raw(sql).Count(&total).Error
- return
-}
-
-func (dbp *DbPersons) GetPersonsCompareCacheBase(from int, size int) (arr []*protomsg.Esinfo, err error) {
- var persons []DbPersons
- sql := "select id,faceFeature,tableId,enable from dbtablepersons where isDelete=0 and tableId in (select id from dbtables where isDelete=0)"
- sql += " order by id asc limit " + strconv.Itoa(from) + "," + strconv.Itoa(size)
- err = db.Raw(sql).Find(&persons).Error
- if err != nil {
- return nil, err
- }
- for _, p := range persons {
- if p.FaceFeature != "" {
- arr = append(arr, &protomsg.Esinfo{
- Id: p.Id,
- Tableid: p.TableId,
- FaceFeature: p.FaceFeature,
- Enable: int32(p.Enable),
- })
- }
- }
- return
-}
-
-func (dbp *DbPersons) GetPersonsCompareCacheById(id string) (info *protomsg.Esinfo, err error) {
- sql := "select id,faceFeature,tableId,enable from dbtablepersons where id = \"" + id + "\""
- var p DbPersons
- err = db.Raw(sql).First(&p).Error
- if err != nil {
- return nil, err
- }
- if p.FaceFeature != "" {
- info = &protomsg.Esinfo{
- Id: p.Id,
- Tableid: p.TableId,
- FaceFeature: p.FaceFeature,
- Enable: int32(p.Enable),
- }
- }
- return
-}
diff --git a/cache/dbWatch.go b/cache/dbWatch.go
deleted file mode 100644
index 128a37d..0000000
--- a/cache/dbWatch.go
+++ /dev/null
@@ -1,53 +0,0 @@
-package cache
-
-import (
- "fmt"
- "sdkCompare/config"
-
- "github.com/go-mysql-org/go-mysql/canal"
-)
-
-type MyEventHandler struct {
- canal.DummyEventHandler
-}
-
-func (h *MyEventHandler) OnRow(e *canal.RowsEvent) error {
- // 杩囨护鍘嗗彶娑堟伅鍜屼笉鍏冲績鐨勮〃
- if e.Table.Name != config.DbPersonCompInfo.PersonTable || e.Header == nil {
- return nil
- }
-
- fmt.Printf("%s %s %v\n", e.Table.Name, e.Action, e.Rows)
-
- return nil
-}
-
-func (h *MyEventHandler) String() string {
- return "MyEventHandler"
-}
-
-func WatchDB() error {
- cfg := canal.NewDefaultConfig()
- cfg.Addr = config.DbPersonCompInfo.MysqlAddr
- cfg.User = config.DbPersonCompInfo.Username
- cfg.Password = config.DbPersonCompInfo.Password
-
- // 鐩戝惉鐨勬暟鎹簱鍜岃〃
- cfg.Dump.TableDB = config.DbPersonCompInfo.Database
- cfg.Dump.Tables = []string{config.DbPersonCompInfo.PersonTable}
-
- cfg.ParseTime = true
-
- c, err := canal.NewCanal(cfg)
- if err != nil {
- return err
- }
-
- // Register a handler to handle RowsEvent
- c.SetEventHandler(&MyEventHandler{})
-
- // Start canal
- err = c.Run()
-
- return err
-}
diff --git a/cache/shardmap/shardmap.go b/cache/shardmap/shardmap.go
index e53aa38..255f0c4 100644
--- a/cache/shardmap/shardmap.go
+++ b/cache/shardmap/shardmap.go
@@ -4,6 +4,7 @@
import (
"basic.com/pubsub/protomsg.git"
+ "sdkCompare/db"
"sync"
//"encoding/json"
//"fmt"
@@ -89,7 +90,7 @@
value interface{}
}
-func (s *ShardMap) Walk(wf wfOp, sourceFea []byte, baseScore float32, isWebComp bool, target string) (targets []*protomsg.SdkCompareEach) {
+func (s *ShardMap) Walk(wf wfOp, sourceFea []byte, baseScore float32) (targets []*protomsg.SdkCompareEach) {
var wg sync.WaitGroup
var lock sync.Mutex
for _, si := range s.shards {
@@ -101,57 +102,25 @@
wg.Add(1)
- go func(st *shardItem, fw wfOp, sf []byte, baseSec float32, isWeb bool) {
+ go func(st *shardItem, fw wfOp, sf []byte, baseSec float32) {
defer wg.Done()
for _, feature := range st.data {
- if eif, ok := feature.(*protomsg.Esinfo); ok {
- if !isWeb { //涓嶄細姣斿鎶撴媿搴�,鍙瘮瀵规湁鏁堢殑浜�
- if eif.Enable == 1 {
- score := float32(0)
- if target == "car" {
- if eif.CarNo != "" {
- score = strComp(sf, eif.CarNo)
- } else {
- continue
- }
- } else {
- score = fw(sf, eif.FaceFeature)
- }
- if score > 0 && score >= baseScore {
- lock.Lock()
- targets = append(targets, &protomsg.SdkCompareEach{
- Id: eif.Id,
- CompareScore: score,
- Tableid: eif.Tableid,
- })
- lock.Unlock()
- }
- }
- } else { //鏉ユ簮鏄痺eb锛屼細姣斿鎶撴媿搴擄紝涓嶇鏄惁鏈夋晥閮介渶瑕佹瘮瀵�
- score := float32(0)
- if target == "car" {
- if eif.CarNo != "" {
- score = strComp(sf, eif.CarNo)
- } else {
- continue
- }
- } else {
- score = fw(sf, eif.FaceFeature)
- }
- if score > 0 && score >= baseScore {
- lock.Lock()
- targets = append(targets, &protomsg.SdkCompareEach{
- Id: eif.Id,
- CompareScore: score,
- Tableid: eif.Tableid,
- })
- lock.Unlock()
- }
+ if eif, ok := feature.(*db.FeatureCacheBase); ok {
+ score := float32(0)
+ score = fw(sf, eif.FaceFeature)
+ if score > 0 && score >= baseScore {
+ lock.Lock()
+ targets = append(targets, &protomsg.SdkCompareEach{
+ Id: eif.Id,
+ CompareScore: score,
+ Tableid: eif.TableId,
+ })
+ lock.Unlock()
}
}
}
- }(&tempsi, wf, sourceFea, baseScore, isWebComp)
+ }(&tempsi, wf, sourceFea, baseScore)
}
wg.Wait()
diff --git a/compare/compare.go b/compare/compare.go
new file mode 100644
index 0000000..8579c57
--- /dev/null
+++ b/compare/compare.go
@@ -0,0 +1,103 @@
+package compare
+
+import (
+ "encoding/base64"
+ "fmt"
+ "strconv"
+
+ "sdkCompare/cache"
+
+ "basic.com/pubsub/protomsg.git"
+ "basic.com/valib/logger.git"
+ "github.com/golang/protobuf/proto"
+)
+
+const thresholdLimit = float32(30)
+
+func GetComparePersonBaseInfo(args protomsg.CompareArgs) []byte {
+ if args.FaceFeature == nil {
+ return nil
+ }
+
+ //鎸囧畾鏈�浣庡垎
+ baseScore := thresholdLimit
+ if args.CompareThreshold > thresholdLimit {
+ baseScore = args.CompareThreshold
+ }
+
+ if args.IsCompareAll {
+ baseScore = 0
+ }
+
+ var scResult protomsg.SdkCompareResult
+
+ var walkedArea = make(map[string]struct{}, 0)
+ // 浼樺厛姣斿浼犲叆鐨勫皬鍖篿d
+ if args.TreeNodes != nil && len(args.TreeNodes) > 0 {
+ for _, id := range args.TreeNodes {
+ if _, ok := cache.CacheMap.Area[id]; ok {
+ targets := cache.CacheMap.Area[id].Walk(DoSdkCompare, args.FaceFeature, baseScore)
+ if len(targets) > 0 {
+ scResult.CompareResult = append(scResult.CompareResult, targets...)
+ }
+
+ walkedArea[id] = struct{}{}
+ }
+ }
+
+ if len(scResult.CompareResult) > 0 {
+ goto done
+ }
+ }
+
+ // 姣斿浠ュ鐨勫皬鍖�
+ if !args.IsCompareAll && len(args.TreeNodes) > 0 {
+ baseScore += 20
+ }
+
+ for key, val := range cache.CacheMap.Area {
+ if _, ok := walkedArea[key]; ok {
+ continue
+ }
+
+ targets := val.Walk(DoSdkCompare, args.FaceFeature, baseScore)
+ if len(targets) > 0 {
+ scResult.CompareResult = append(scResult.CompareResult, targets...)
+ }
+ }
+
+done:
+ logger.Debugf("姣斿缁撴灉 %d鏉�", len(scResult.CompareResult))
+ buf, err := proto.Marshal(&scResult)
+ if err != nil {
+ logger.Error("scResult Marshal error!", err)
+ return nil
+ }
+
+ return buf
+}
+
+func DoSdkCompare(ci []byte, co string) float32 {
+ co_d, err := base64.StdEncoding.DecodeString(co)
+ if err != nil {
+ logger.Error("DoSdkCompare err:", err)
+ return -1
+ }
+ sec := DecCompare(ci, co_d)
+ //logger.Debug("姣斿寰楀垎涓猴細", sec)
+
+ sec = ParseScore(sec)
+ return sec
+}
+
+func ParseScore(compareScore float32) float32 {
+ if compareScore <= 1 {
+ compareScore = compareScore * 100
+ }
+ if compareScore == 100 {
+ return 100
+ }
+ f, _ := strconv.ParseFloat(fmt.Sprintf("%2.2f", compareScore), 32)
+
+ return float32(f)
+}
diff --git a/face/faceCompare.go b/compare/faceSdk.go
similarity index 98%
rename from face/faceCompare.go
rename to compare/faceSdk.go
index 373027d..6ffcf39 100644
--- a/face/faceCompare.go
+++ b/compare/faceSdk.go
@@ -1,4 +1,4 @@
-package face
+package compare
import (
"unsafe"
diff --git a/config/compare.yaml b/config/compare.yaml
new file mode 100644
index 0000000..5ef72d1
--- /dev/null
+++ b/config/compare.yaml
@@ -0,0 +1,13 @@
+database:
+ servePort: 4010
+ mysqlAddr: 192.168.20.119
+ username: root
+ password: c++java123
+ database: faceanalysis
+ personTable: dbtablepersons
+log:
+ path: ./log/
+ level: -1
+ maxSize: 128
+ maxBackups: 30
+ maxAge: 15
diff --git a/config/config.go b/config/config.go
index b528065..330859d 100644
--- a/config/config.go
+++ b/config/config.go
@@ -1,33 +1,14 @@
package config
import (
+ "fmt"
+
"basic.com/valib/logger.git"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
- "log"
)
-type server struct {
- AnalyServerId string `mapstructure:"analyServerId"`
- NetworkAdapter string `mapstructure:"networkAdapter"`
-}
-
-var Server = &server{}
-
-type esinfo struct {
- EsIndex esindexlist `mapstructure:"index"`
-}
-
-type esindexlist struct {
- AiOcean index `mapstructure:"aiOcean"`
-}
-
-type index struct {
- IndexName string `mapstructure:"index"`
- IndexType string `mapstructure:"type"`
-}
-
-type dbpersoncompare struct {
+type database struct {
MysqlAddr string `mapstructure:"mysqlAddr"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
@@ -36,17 +17,7 @@
ServePort int `mapstructure:"servePort"`
}
-type espersoncompare struct {
- ServePort int `mapstructure:"servePort"`
- ESIP string `mapstructure:"esip"`
- ESPort string `mapstructure:"esPort"`
-}
-
-var DbPersonCompInfo = &dbpersoncompare{}
-
-var EsCompServerInfo = &espersoncompare{}
-
-var EsInfo = &esinfo{}
+var DbPersonCompInfo = &database{}
type LogConfig struct {
Path string `mapstructure:"path"` //鏃ュ織瀛樺偍璺緞
@@ -58,29 +29,31 @@
var LogConf = &LogConfig{}
-func Init(env string) {
+func Init() error {
var err error
v := viper.New()
v.SetConfigType("yaml")
- v.SetConfigName(env)
+ v.SetConfigName("compare")
v.AddConfigPath("./")
v.AddConfigPath("./config/")
err = v.ReadInConfig()
if err != nil {
- log.Fatal("error on parsing configuration file")
+ fmt.Printf("error on parsing configuration file, %s, config file compare.yaml\n", err.Error())
+ return err
}
+
read2Conf(v)
v.WatchConfig()
v.OnConfigChange(func(in fsnotify.Event) {
read2Conf(v)
})
+
+ return nil
}
func read2Conf(v *viper.Viper) {
- v.UnmarshalKey("es", EsInfo)
- v.UnmarshalKey("server", Server)
- v.UnmarshalKey("dbpersoncompare", DbPersonCompInfo)
- v.UnmarshalKey("espersoncompare", EsCompServerInfo)
+ v.UnmarshalKey("database", DbPersonCompInfo)
v.UnmarshalKey("log", LogConf)
+
logger.SetLevel(LogConf.Level)
}
diff --git a/config/config.yaml b/config/config.yaml
deleted file mode 100644
index 436cdd2..0000000
--- a/config/config.yaml
+++ /dev/null
@@ -1,25 +0,0 @@
-server:
- analyServerId: DSVAD010120230222
- networkAdapter: enp6s0
-dbpersoncompare:
- servePort: 4010
- mysqlAddr: 192.168.20.119
- username: "root"
- password: "c++java123"
- database: "faceanalysis"
- personTable: "dbtablepersons"
-espersoncompare:
- servePort: 4011
- esip: "127.0.0.1"
- esPort: 9200
-es:
- index:
- aiOcean:
- index: ai_ocean
- type: analysisModel
-log:
- path: ./log/
- level: -1
- maxSize: 128
- maxBackups: 30
- maxAge: 15
diff --git a/config/dev.yaml b/config/dev.yaml
deleted file mode 100644
index a1ec4af..0000000
--- a/config/dev.yaml
+++ /dev/null
@@ -1,19 +0,0 @@
-es:
- shards: 1,2,3
- index:
- # 浜鸿劯鎶撴媿璁板綍
- videopersons:
- index: videopersons
- type: perVideoPicture
- # 搴曞簱 鍒楄〃
- dbtables:
- index: dbtables
- type: dbpersontables
- # 搴曞簱浜哄憳搴�
- dbtablepersons:
- index: dbtablepersons
- type: dbpersons
- # 琛屼负鎶撴媿璁板綍*
- personaction:
- index: personaction
- type: perVideoAction
\ No newline at end of file
diff --git a/db/base.go b/db/base.go
new file mode 100644
index 0000000..4f8c5e8
--- /dev/null
+++ b/db/base.go
@@ -0,0 +1,18 @@
+package db
+
+type BaseEntity struct {
+ Id string `gorm:"primary_key;column:id" json:"id" example:""`
+ CreateTime string `gorm:"column:createTime" json:"createTime,omitempty" example:""`
+ UpdateTime string `gorm:"column:updateTime" json:"updateTime,omitempty" example:""`
+ CreateBy string `gorm:"column:createBy" json:"createBy,omitempty" example:""`
+ IsDelete int `gorm:"column:isDelete" json:"isDelete" example:"0 鏈垹闄� 1宸插垹闄�"`
+ Enable int `gorm:"column:enable" json:"enable" example:" 1鐢熸晥 0鏈敓鏁�"`
+}
+
+type FeatureCacheBase struct {
+ Id string
+ AreaId string
+ TableId string
+ FaceFeature string
+ Enable int32
+}
diff --git a/db/db.go b/db/db.go
new file mode 100644
index 0000000..2aa5dfd
--- /dev/null
+++ b/db/db.go
@@ -0,0 +1,39 @@
+package db
+
+import (
+ "fmt"
+
+ "sdkCompare/config"
+
+ "basic.com/valib/logger.git"
+ "gorm.io/driver/mysql"
+ "gorm.io/gorm"
+)
+
+var db *gorm.DB
+
+func ConnectDB() error {
+ var err error
+
+ dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4",
+ config.DbPersonCompInfo.Username,
+ config.DbPersonCompInfo.Password,
+ config.DbPersonCompInfo.MysqlAddr,
+ config.DbPersonCompInfo.Database)
+
+ db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
+ // 绂佺敤澶栭敭(鎸囧畾澶栭敭鏃朵笉浼氬湪mysql鍒涘缓鐪熷疄鐨勫閿害鏉�)
+ DisableForeignKeyConstraintWhenMigrating: true,
+ //// 鎸囧畾琛ㄥ墠缂�
+ //NamingStrategy: schema.NamingStrategy{
+ // TablePrefix: config.Conf.Mysql.TablePrefix + "_",
+ //},
+ })
+
+ if err != nil {
+ logger.Error("mysql database open err: %s", err.Error())
+ return err
+ }
+
+ return nil
+}
diff --git a/db/person.go b/db/person.go
new file mode 100644
index 0000000..40240c7
--- /dev/null
+++ b/db/person.go
@@ -0,0 +1,87 @@
+package db
+
+import (
+ "basic.com/pubsub/protomsg.git"
+ "strconv"
+)
+
+type DbPersons struct {
+ BaseEntity
+ TableId string `gorm:"column:tableId" json:"tableId" example:"搴撹〃id"`
+ FaceFeature string `gorm:"column:faceFeature" json:"faceFeature" example:"浜鸿劯鐗瑰緛鍊硷紙杞︿富鐗瑰緛锛�"`
+ PersonPicUrl string `gorm:"column:personPicUrl" json:"personPicUrl" example:"鍥剧墖璺緞,锛堣溅涓荤収鐗囷級"`
+ PersonName string `gorm:"column:personName" json:"personName" example:"浜哄憳濮撳悕锛岋紙杞︿富濮撳悕锛�"`
+ Age string `gorm:"column:age" json:"age" example:"骞撮緞"`
+ Sex string `gorm:"column:sex" json:"sex" example:"鎬у埆 鐢� 濂筹紙杞︿富鎬у埆锛�"`
+ IdCard string `gorm:"column:idCard" json:"idCard" example:"韬唤璇侊紙杞︿富韬唤璇侊級"`
+ PhoneNum string `gorm:"column:phoneNum" json:"phoneNum" example:"鎵嬫満鍙风爜"`
+ MonitorLevel string `gorm:"column:monitorLevel" json:"monitorLevel" example:"绛夌骇"`
+ PicDesc string `gorm:"column:picDesc" json:"picDesc" example:"鐓х墖鏍囪瘑"`
+ Reserved string `gorm:"column:reserved" json:"reserved" example:"鍏朵粬"`
+ FromServerId string `gorm:"column:fromServerId" json:"fromServerId,omitempty" example:"鍏ュ簱serverId"`
+ ResidentialArea string `gorm:"column:residential_area;type:varchar(255)" json:"residentialArea" example:"灏忓尯"`
+ Community string `gorm:"column:community;type:varchar(255)" json:"community" example:"绀惧尯"`
+ LastAppearanceTime int64 `gorm:"column:last_appearance_time;type:int;not null;default:0" json:"lastAppearanceTime" example:"123456789"`
+ SnapshotCount int `gorm:"column:snapshot_count;type:varchar(255)" json:"snapshotCount" example:"10" comment:"鎶撴媿娆℃暟"`
+ DaysAppeared int `gorm:"column:days_appeared;type:int(11);not null;default:0" json:"daysAppeared" example:"5" comment:"鍑虹幇澶╂暟"`
+ Location string `gorm:"column:location;type:varchar(255)" json:"location" example:"寤烘。鍦扮偣" comment:"寤烘。鍦扮偣"`
+ LastLocation string `gorm:"column:last_location;type:varchar(255)" json:"lastLocation" example:"鏈�鍚庡嚭鐜板湴鐐�" comment:"鏈�鍚庡嚭鐜板湴鐐�"`
+ FaceAngleYaw int `gorm:"column:face_angle_yaw;type:int(11)" json:"faceAngleYaw" example:"15" comment:"浜鸿劯瑙掑害鍋忚埅瑙�"`
+ FaceAngleRoll int `gorm:"column:face_angle_roll;type:int(11)" json:"faceAngleRoll" example:"16" comment:"浜鸿劯瑙掑害婊氳浆瑙�"`
+ FaceAnglePitch int `gorm:"column:face_angle_pitch;type:int(11)" json:"faceAnglePitch" example:"15" comment:"浜鸿劯瑙掑害淇话瑙�"`
+ PersonalStatusName string `gorm:"column:personal_status;type:varchar(31);comment:AI鏍囩" json:"-"` //AI鏍囩
+ PersonalStatus string `gorm:"-" json:"personalStatus"` //AI鏍囩瀵瑰簲鍚嶇О
+ Labels []string `gorm:"-" json:"labels"` //鎵嬪姩娣诲姞鐨勬爣绛惧搴斿悕绉�
+ AreaID string `json:"areaID" gorm:"index;column:communityID;type:varchar(299);"` //甯搁┗灏忓尯 domain unit ID
+ OrgID string `json:"orgID" gorm:"index;column:org_id;type:varchar(299);"` //甯搁┗娲惧嚭鎵� domain unit ID
+}
+
+func (dbp *DbPersons) GetPersonTotal(tableId string) (total int64, err error) {
+ sql := "select count(1) as total from dbtablepersons where isDelete=0 and tableId in (select id from dbtables where isDelete=0)"
+ if tableId != "" {
+ sql += " and tableId='" + tableId + "'"
+ }
+ err = db.Raw(sql).Count(&total).Error
+ return
+}
+
+func (dbp *DbPersons) GetPersonsCompareCacheBase(from int, size int) (arr []*FeatureCacheBase, err error) {
+ var persons []DbPersons
+ sql := "select id, tableId, faceFeature, communityID, enable from dbtablepersons where isDelete=0 and tableId in (select id from dbtables where isDelete=0)"
+ sql += " order by id asc limit " + strconv.Itoa(from) + "," + strconv.Itoa(size)
+ err = db.Raw(sql).Find(&persons).Error
+ if err != nil {
+ return nil, err
+ }
+
+ for _, p := range persons {
+ if p.FaceFeature != "" {
+ arr = append(arr, &FeatureCacheBase{
+ Id: p.Id,
+ TableId: p.TableId,
+ AreaId: p.AreaID,
+ FaceFeature: p.FaceFeature,
+ Enable: int32(p.Enable),
+ })
+ }
+ }
+ return
+}
+
+func (dbp *DbPersons) GetPersonsCompareCacheById(id string) (info *protomsg.Esinfo, err error) {
+ sql := "select id,faceFeature,tableId,enable from dbtablepersons where id = \"" + id + "\""
+ var p DbPersons
+ err = db.Raw(sql).First(&p).Error
+ if err != nil {
+ return nil, err
+ }
+ if p.FaceFeature != "" {
+ info = &protomsg.Esinfo{
+ Id: p.Id,
+ Tableid: p.TableId,
+ FaceFeature: p.FaceFeature,
+ Enable: int32(p.Enable),
+ }
+ }
+ return
+}
diff --git a/db/personStatus.go b/db/personStatus.go
new file mode 100644
index 0000000..8d79f8a
--- /dev/null
+++ b/db/personStatus.go
@@ -0,0 +1,27 @@
+package db
+
+type PersonStatus struct {
+ CommunityID string `gorm:"column:communityID;type:varchar(299);"` //甯搁┗灏忓尯
+ DocumentNumber string `gorm:"column:documentNumber;type:varchar(299);"` //妗f缂栧彿
+ Status string `gorm:"column:status"` //鏍囩
+ FrequentAddress string `gorm:"column:frequentAddress; type:varchar(299);"` //甯搁┗鍦板潃
+}
+
+func (ps *PersonStatus) TableName() string {
+ return "person_status"
+}
+
+func (ps *PersonStatus) GetPersonAccessedAreas() (map[string][]string, error) {
+ var results []PersonStatus
+ err := db.Table(ps.TableName()).Find(&results).Error
+ if err != nil {
+ return nil, err
+ }
+
+ var communityMap = make(map[string][]string, 0)
+ for _, p := range results {
+ communityMap[p.DocumentNumber] = append(communityMap[p.DocumentNumber], p.CommunityID)
+ }
+
+ return communityMap, nil
+}
diff --git a/discovery/cache.go b/discovery/cache.go
deleted file mode 100644
index 7f921eb..0000000
--- a/discovery/cache.go
+++ /dev/null
@@ -1,80 +0,0 @@
-package discovery
-
-import (
- "basic.com/pubsub/cache.git/shardmap"
- "basic.com/pubsub/protomsg.git"
- "basic.com/valib/logger.git"
- "errors"
- "github.com/gogo/protobuf/proto"
- "sdkCompare/config"
- "sdkCompare/util"
-)
-
-const (
- SERVER_KEY = "SERVERINFO"
-)
-
-var cMap *shardmap.ShardMap
-var withBus bool
-
-func SetBus(wb bool) error {
- withBus = wb
- if !withBus {
- serverIp, _, e := util.GetLocalIP(config.Server.NetworkAdapter)
- if e != nil {
- return e
- }
- cMap = shardmap.New(uint8(32))
- cMap.Set(SERVER_KEY, protomsg.LocalConfig{
- AlarmIp: serverIp,
- AlarmPort: 9200,
- })
- }
- return nil
-}
-
-func initCacheData(initChan chan bool) {
-
- initChan <- true
-}
-
-var newUpdateMsg = &protomsg.DbChangeMessage{}
-
-func updateData(b []byte) {
- if err := proto.Unmarshal(b, newUpdateMsg); err != nil {
- logger.Debug("dbChangeMsg unmarshal err:", err)
- return
- }
- switch newUpdateMsg.Table {
- case protomsg.TableChanged_T_Server:
- default:
- logger.Debug("other updateData operation")
-
- }
-}
-
-func GetServerInfo() (conf protomsg.LocalConfig, err error) {
- config, b := cMap.Get(SERVER_KEY)
- if b {
- return config.(protomsg.LocalConfig), nil
- } else {
- return conf, errors.New("conf not found")
- }
-}
-
-// 鍒ゆ柇涓�涓暟缁勬槸鍚﹀寘鍚彟涓�涓暟缁勭殑鎵�鏈夊厓绱�
-func arrayContains(list []string, arr []string) bool {
- c := 0
- if arr == nil || list == nil {
- return false
- }
- for _, s := range arr {
- for _, t := range list {
- if s == t {
- c++
- break
- }
- }
- }
- return c == len(arr)
-}
diff --git a/main.go b/main.go
index 7ea4ffb..52ccec1 100644
--- a/main.go
+++ b/main.go
@@ -3,15 +3,15 @@
import (
"context"
"flag"
- "os"
"path"
- "path/filepath"
- "sdkCompare/proto/facecompare"
"strconv"
"time"
"sdkCompare/cache"
+ "sdkCompare/compare"
"sdkCompare/config"
+ "sdkCompare/db"
+ "sdkCompare/proto/facecompare"
"basic.com/pubsub/protomsg.git"
"basic.com/valib/logger.git"
@@ -23,36 +23,27 @@
"nanomsg.org/go-mangos/transport/tcp"
)
-var (
- envirment string
- procName string
- targetType1 string
-)
+const procName = "faceCompare"
func init() {
- flag.StringVar(&envirment, "e", "config", "")
-
- flag.StringVar(&targetType1, "targetType1", "FaceDetect", "cache feature")
-
flag.Parse()
vaversion.Usage()
+}
- config.Init(envirment)
+func main() {
+ err := config.Init()
+ if err != nil {
+ return
+ }
- procName = filepath.Base(os.Args[0])
- var logFile = path.Join(config.LogConf.Path, procName+".log")
+ var logFile = path.Join(config.LogConf.Path, "faceCompare.log")
// 鏃ュ織鍒濆鍖�
logger.InitLogger(logFile, config.LogConf.Level, config.LogConf.MaxSize, config.LogConf.MaxBackups, config.LogConf.MaxAge)
logger.Info("logger init success !")
-}
-
-func main() {
- //esutil.InitLog(logger.Debug)
- logger.Debug("This is a new server about sdk compare, proc name ", procName)
serveUrl := "tcp://0.0.0.0:"
- if err := cache.ConnectDB(); err != nil {
+ if err := db.ConnectDB(); err != nil {
logger.Error(err.Error())
return
}
@@ -60,7 +51,7 @@
cache.InitDbTablePersons()
serveUrl = serveUrl + strconv.Itoa(config.DbPersonCompInfo.ServePort)
- logger.Debugf("%s serve url:%s", procName, serveUrl)
+ logger.Infof("%s serve url:%s", procName, serveUrl)
Recv(serveUrl)
}
@@ -73,6 +64,7 @@
if sock, err = rep.NewSocket(); err != nil {
logger.Error("new rep socket err:", err)
}
+
sock.AddTransport(ipc.NewTransport())
sock.AddTransport(tcp.NewTransport())
if err = sock.Listen(url); err != nil {
@@ -82,54 +74,54 @@
for {
select {
case <-ctx.Done():
- logger.Debug("ctx done")
+ logger.Info("ctx done")
return
default:
msg, err = sock.Recv()
- if err != nil {
+ if err != nil || len(msg) <= 0 {
continue
}
- if len(msg) > 0 {
- var compareType facecompare.CompareRequest
- err = proto.Unmarshal(msg, &compareType)
- if err != nil {
- logger.Error("compareType json unmarshal error")
+ var request facecompare.CompareRequest
+ err = proto.Unmarshal(msg, &request)
+ if err != nil {
+ logger.Warn("CompareRequest json unmarshal error")
+ continue
+ }
+
+ var result []byte
+ if request.CompareType == facecompare.CompareType_Compare {
+ var compareArgInfo protomsg.CompareArgs
+ var cacheChangeInfo protomsg.EsPersonCacheChange
+ if err = proto.Unmarshal(request.Payload, &compareArgInfo); err == nil {
+ timeStart := time.Now()
+ result = compare.GetComparePersonBaseInfo(compareArgInfo)
+ logger.Debug("鐢ㄦ椂锛�", time.Since(timeStart))
+ } else if err = proto.Unmarshal(request.Payload, &cacheChangeInfo); err == nil {
+ cache.UpdateCache(&cacheChangeInfo)
+ } else {
+ logger.Warn("CompareArgs or EsPersonCacheChange json unmarshal error")
continue
}
- var result []byte
- if compareType.CompareType == facecompare.CompareType_Compare {
- var compareArgInfo protomsg.CompareArgs
- var cacheChangeInfo protomsg.EsPersonCacheChange
- if err = proto.Unmarshal(compareType.Payload, &compareArgInfo); err == nil {
- timeStart := time.Now()
- result = cache.GetComparePersonBaseInfo(compareArgInfo)
- logger.Debug("鐢ㄦ椂锛�", time.Since(timeStart))
- } else if err = proto.Unmarshal(compareType.Payload, &cacheChangeInfo); err == nil {
- cache.UpdateCache(&cacheChangeInfo)
- } else {
- logger.Error("CompareArgs or EsPersonCacheChange json unmarshal error")
- continue
+ } else if request.CompareType == facecompare.CompareType_UpdateCache {
+ var compareEvent protomsg.CompareEvent
+ if err = proto.Unmarshal(request.Payload, &compareEvent); err == nil {
+ if compareEvent.EventType == protomsg.CompareEventType_ReInitCache { //鍔犲叆闆嗙兢鍚庨噸鏂板垵濮嬪寲缂撳瓨
+ cache.ReInitDbTablePersonsCache()
+ } else if compareEvent.EventType == protomsg.CompareEventType_UpdateCache { //搴撲腑鏂板鏇存柊缂撳瓨
+ id := string(compareEvent.Payload)
+ cache.UpdateDbPersonsCacheById(id)
+ logger.Info("--------------鏇存柊浜哄憳缂撳瓨, id: ", id)
}
- } else if compareType.CompareType == facecompare.CompareType_UpdateCache {
- var compareEvent protomsg.CompareEvent
- if err = proto.Unmarshal(compareType.Payload, &compareEvent); err == nil {
- if compareEvent.EventType == protomsg.CompareEventType_ReInitCache { //鍔犲叆闆嗙兢鍚庨噸鏂板垵濮嬪寲缂撳瓨
- cache.ReInitDbTablePersonsCache()
- } else if compareEvent.EventType == protomsg.CompareEventType_UpdateCache { //搴撲腑鏂板鏇存柊缂撳瓨
- id := string(compareEvent.Payload)
- cache.UpdateDbPersonsCacheById(id)
- logger.Debug("--------------鏇存柊浜哄憳缂撳瓨, id: ", id)
- }
- } else {
- logger.Error("CompareEvent json unmarshal error")
- continue
- }
+ } else {
+ logger.Warn("CompareEvent json unmarshal error")
+ continue
}
- err = sock.Send(result)
- if err != nil {
- logger.Error("send reply err:", err.Error())
- }
+ }
+
+ err = sock.Send(result)
+ if err != nil {
+ logger.Warn("send reply err:", err.Error())
}
}
}
diff --git a/util/util.go b/util/util.go
index a104088..30914cc 100644
--- a/util/util.go
+++ b/util/util.go
@@ -8,34 +8,34 @@
)
// 鑾峰彇鏈満缃戝崱IP
-func GetLocalIP(networkName string) (ipv4 string,mask string, err error) {
+func GetLocalIP(networkName string) (ipv4 string, mask string, err error) {
interfaces, err := net.Interfaces()
if err != nil {
- return "","", err
+ return "", "", err
}
for _, i := range interfaces {
byName, err := net.InterfaceByName(i.Name)
if err != nil {
- return "","", err
+ return "", "", err
}
addresses, err := byName.Addrs()
for _, v := range addresses {
- if ipnet, ok:=v.(*net.IPNet);ok && !ipnet.IP.IsLoopback(){
- if ipnet.IP.To4() !=nil{
- if byName.Name == networkName{
+ if ipnet, ok := v.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
+ if ipnet.IP.To4() != nil {
+ if byName.Name == networkName {
maskStr := ipnet.Mask.String()
mask64, _ := strconv.ParseUint(maskStr, 16, 32)
- return ipnet.IP.String(),IpIntToString(int(mask64)),nil
+ return ipnet.IP.String(), IpIntToString(int(mask64)), nil
}
}
}
}
}
- return "","", errors.New("ipv4 not found")
+ return "", "", errors.New("ipv4 not found")
}
-func IpIntToString(ipInt int) string{
+func IpIntToString(ipInt int) string {
ipSegs := make([]string, 4)
var len int = len(ipSegs)
buffer := bytes.NewBufferString("")
@@ -53,3 +53,20 @@
}
return buffer.String()
}
+
+// 鍒ゆ柇涓�涓暟缁勬槸鍚﹀寘鍚彟涓�涓暟缁勭殑鎵�鏈夊厓绱�
+func ArrayContains(list []string, arr []string) bool {
+ c := 0
+ if arr == nil || list == nil {
+ return false
+ }
+ for _, s := range arr {
+ for _, t := range list {
+ if s == t {
+ c++
+ break
+ }
+ }
+ }
+ return c == len(arr)
+}
--
Gitblit v1.8.0