pansen
2019-01-26 ec3b048867bed0009dd818b69a21983768fca8ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// Created by ps on 2/26/18.
//
 
#include <cstring>
#include <THFeature_i.h>
#include <basic/util/app/AppPreference.hpp>
#include <basic/util/ShareMemory/ShareMemoryTool.hpp>
#include "FaceDBCompareServer.h"
 
using namespace std;
 
FaceDBCompareServer::FaceDBCompareServer() : m_erlangDbTool(nullptr), m_sqliteFaceEncap(nullptr) {
 
}
 
FaceDBCompareServer::FaceDBCompareServer(ErlangTool::ErlangDbTool *erlangDbTool) : m_erlangDbTool(erlangDbTool),
                                                                                   m_sqliteFaceEncap(nullptr) {
 
}
 
FaceDBCompareServer::FaceDBCompareServer(SqliteFaceEncap *sqliteFaceEncap) : m_erlangDbTool(nullptr),
                                                                             m_sqliteFaceEncap(sqliteFaceEncap) {
 
}
 
//FaceDBCompareServer::FaceDBCompareServer(Ice::CommunicatorHolder& ich) : minThreshold(10)  {
//#ifdef TestCode
//
//#else
//    try {
//        base_alarm = ich->stringToProxy("ServerAlarm");
//    }catch(std::exception ex){
//        ERR("ServerAlarm connceticon fail" << ex.what());
//    };
//#endif
//}
 
FaceDBCompareServer::~FaceDBCompareServer() {
 
}
 
FaceResults FaceDBCompareServer::getTopResult(std::thread::id key) {
    if (m_dbRWLocks.find(key) == m_dbRWLocks.end()) {
        m_dbRWLocks[key] = RWLock();
    }
    RWLock &t_rwl = m_dbRWLocks[key];
//    t_rwl.wrlock();
    FaceResults t_result = topResult[key];
    topResult.erase(key);
    t_rwl.unlock();
    return t_result;
}
 
//#todo map 1->2 modify 2->1
bool FaceDBCompareServer::compare(thread::id key, AlarmData *alarmData, int topN, float score) {
    if (m_dbRWLocks.find(key) == m_dbRWLocks.end()) {
        m_dbRWLocks[key] = RWLock();
    }
//    锁任务的对比结果
    RWLock &t_rwl = m_dbRWLocks[key];
//    ClockTimer clockTimer("compare ");
    RWLock t_rwLock;
 
    m_rwLock.rdlock();
    mapFaceResults t_compareResults;
 
    parallelFor(ParallelForThreardSize::CPU_Number, [&](string &key, FeatureData &data) {
        //#todo
        double sc = 0;
        for (auto &t_fea : data.features) {
            double t_sc = m_casiaFaceWapper.compareFeature(alarmData->feature, t_fea);
//                获取当前对比的最高分?
            sc = t_sc > sc ? t_sc : sc;
        }
//            FaceResult tface{data.face_id, data.uuid, alarmData->tableName, sc, false};
        FaceResult tface{0, data.uuid, alarmData->tableName, sc, data.faceUrl, data.idcard, "", data.enabled};
        t_rwLock.wrlock();
//            如果当前记录的所有特征的最高分小于结果中的最低分则不保存
        if (sc < score || sc < t_compareResults.end()->second.confidence) {
            t_rwLock.unlock();
            return;
        }
        t_compareResults.insert(make_pair(tface.confidence, tface));
        if (topN != 0 && t_compareResults.size() > topN) {
            t_compareResults.erase((--t_compareResults.end()), t_compareResults.end());
        }
        t_rwLock.unlock();
    });
 
    m_rwLock.unlock();
 
    int maxSearchFaces = alarmData->num;
 
    t_rwl.wrlock();
    auto &t_topResult = topResult[key];
    t_topResult.clear();
    for (auto &item : t_compareResults) {
        auto &it = item.second;
        //#todo
//        if (it.confidence <= 0.6) {
//            continue;
//        }
        FaceResult t_CR{it.id, it.uuid, it.tableName, it.confidence, it.face_img_url, it.idCard, it.alarmRet,
                        it.enabled};
        //cout << __FUNCTION__ << " -> " << __LINE__ << " ->  scroe" << it.confidence << endl;
        t_topResult.push_back(t_CR);
    }
    while (topResult.size() > maxSearchFaces)
        t_topResult.pop_back();
//    获取结果后释放
//    t_rwl.unlock();
    return true;
}
 
void FaceDBCompareServer::loadDBData(std::string str_config) {
    string log = "loadDBData  " + str_config;
    ClockTimer clk(log);
 
    Json::Value t_json;
    Json::FastWriter writer;
    Json::Reader reader;
 
    if (reader.parse(str_config, t_json)) {
 
        m_rwLock.wrlock();
        if (t_json["\"syncTpye\""].type() == Json::nullValue) {
//            同步库/特征是base64
//            #todo sqlite search data from file
            auto str_tab = t_json["\"tableName\""].asString();
            m_tableName = str_tab.substr(1, str_tab.length() - 2);//.append("_fea");
 
 
 
//            string tet = "./syncDBClient ";
//            string str_json = writer.write(t_json);
//            tet.append("\"" + str_json.substr(0, str_json.length() - 1) + "\" ");
//            INFO(tet);
//            system(tet.c_str());
            dataMap.clear();
            try {
                // init
//                BISTL::BiMapFeaData biMapFeaData(m_tableName);
//                auto mymap = biMapFeaData.getMap();
 
                auto mymap = m_erlangDbTool->loadFaceFeaData(m_tableName);
                for (auto &item : mymap) {
                    auto &it = item.second;
                    string str2;
                    str2 = base64.Decode(it.feature.data(), it.feature.size());
                    std::vector<unsigned char> t_fea;
                    t_fea.resize(str2.size());
                    memcpy(t_fea.data(), str2.data(), str2.size());
                    auto &test = dataMap[it.id];
                    test.uuid = it.id;
                    test.features.push_back(t_fea);
                    test.faceUrl = it.img;
                    test.idcard = it.idcard;
                }
//                //#todo
//                for (auto it = mymap->begin(); it != mymap->end(); it++) {
//                    string str_uuid(it->second.m_id.data());
//                    string ft(it->second.m_feature.data());
//                    string imgUrl(it->second.m_imgUrl.data());
//                    string strIdCard(it->second.m_idcard.data());
//                    string str2;
//                    str2 = base64.Decode(ft.data(), ft.length());
//                    std::vector<unsigned char> t_fea;
//                    t_fea.resize(str2.size());
//                    memcpy(t_fea.data(), str2.data(), str2.size());
//                    auto &test = dataMap[str_uuid];
//                    test.uuid = str_uuid;
//                    test.features.push_back(t_fea);
//                    test.faceUrl = imgUrl;
//                    test.idcard = strIdCard;
//                }
            } catch (const std::exception &e) {
                printf("Exception:%s\n", e.what());
//                BISTL::shared_memory_object::remove(m_tableName.c_str());
            }
//            BISTL::shared_memory_object::remove(m_tableName.c_str());
        } else {
//            本地库/特征是blob
            m_tableName = t_json["tableName"].asCString();
            auto t_res = m_sqliteFaceEncap->getFacesFromTable(m_tableName);
            auto t_faceInfoCache = m_sqliteFaceEncap->getFaceInfoFromTable(m_tableName);
            auto &res = *t_res;
            //#todo
            DBG(m_tableName << "clear before size is " << dataMap.size());
            dataMap.clear();
            DBG(m_tableName << "clear after size is " << dataMap.size());
            for (auto &item : res) {
                auto &t_sen = item.second;
                std::vector<unsigned char> t_fea;
                t_fea.resize(t_sen.faceFeature.size());
                memcpy(t_fea.data(), t_sen.faceFeature.data(), t_sen.faceFeature.size());
                auto &test = dataMap[t_sen.uuid];
                test.uuid = t_sen.uuid;
                test.features.push_back(t_fea);
                test.faceUrl = t_sen.faceurl;
                test.idcard = t_faceInfoCache[t_sen.uuid].idCard;
                test.enabled = t_sen.enable;
            }
        }
//            告诉刷新函数,当前底库的数据准备完成
        appPref.setIntData(m_tableName, 1);
        DBG(m_tableName << " size is " << dataMap.size());
        m_rwLock.unlock();
    } else {
        ERR("json format error :: " << str_config);
        appPref.setIntData(str_config, 1);
    }
 
}