派生自 development/c++

pansen
2019-03-07 d3b7bbe7102cd089680a828f5d8f6402c8cf6342
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//
// Created by basic on 18-8-24.
//
#include "JudgmentRetrogradeTool.h"
#include <basic/util/opencv/CvMath.hpp>
#include <QtCore/QDebug>
#include <basic/db/Elasticsearch/EsDBTool.h>
#include <basic/util/opencv/CvUtil.h>
#include <uuid/uuid.h>
#include <basic/util/app/AppPreference.hpp>
 
JudgmentRetrogradeTool::JudgmentRetrogradeTool(const SdkRule &rule) :
    m_triggerElement(0, 0),
    m_sdkRule(rule),
    pManagerEsDB(nullptr),
    m_bSetWH(false),
    pointArray(nullptr),
    npts(0),
    m_rpcClient("RtspAnalysServer", "127.0.0.1",appPref.getIntData("RpcServerPort"),"tcp")
{
    pManagerEsDB = new EsDBTool(appPref.getStringData("ipAdd"), appPref.getIntData("ipPort"));
}
 
JudgmentRetrogradeTool::~JudgmentRetrogradeTool() {
    if (pManagerEsDB) {
        delete pManagerEsDB;
        pManagerEsDB = nullptr;
    }
    if(pointArray)
    {
        delete pointArray;
        pointArray=nullptr;
    }
 
}
bool JudgmentRetrogradeTool::init(QString area, QString line)
{
    m_area=area;
    m_line=line;
}
 
bool JudgmentRetrogradeTool::setMask(QString area, QString line) {
 
    //#todo string ->json
    QJsonArray arrayAreas = getJsonArrayFromQString(area);
    if (arrayAreas.isEmpty()) {
        return false;
    }
    int size=arrayAreas.size();
    QVector<QPoint> vec;
    float sizeW=(float)appPref.getIntData(m_sdkRule.strCamId.toStdString()+"width")/480;
    float sizeH=(float)appPref.getIntData(m_sdkRule.strCamId.toStdString()+"height")/270;
    for (int i = 0; i < arrayAreas.size(); ++i) {
    pointArray=new cv::Point2i[size];
        QJsonValue jsonValue = arrayAreas[i];
        QJsonObject obj = jsonValue.toObject();
        int x = obj.value("x").toDouble() *sizeW;
        int y = obj.value("y").toDouble() *sizeH;
        vec.push_back(QPoint(x, y));
        DBG("width="<<sizeW);
        DBG("height="<<sizeH);
    }
 
 
    m_polygon = std::move(QPolygon(vec));
//    QPoint left[]={QPoint(100,6),QPoint(100,1200),QPoint(850,1200),QPoint(850,6)} ;
//    QPoint right[]={QPoint(850,6),QPoint(1800,6),QPoint(1800,1200),QPoint(850,1200)} ;
//    for(auto &item:left)
//    {
//          m_UpPolygon<<(item);
//    }
//    for(auto &item:right)
//    {
//          m_DownPolygon<<(item);
//    }
 
    cv::Point start, end;
    QJsonArray arrayLine = getJsonArrayFromQString(line);
    if (arrayLine.size() == 2) {
        QJsonValue jsonValue = arrayLine[0];
        QJsonObject obj = jsonValue.toObject();
        start.x = obj.value("x").toDouble();
        start.y = obj.value("y").toDouble();
 
 
        QJsonValue jsonValue2 = arrayLine[1];
        QJsonObject obj2 = jsonValue2.toObject();
        end.x = obj2.value("x").toDouble();
        end.y = obj2.value("y").toDouble();
 
    } else {
        return false;
    }
    setUpBaseline(start, end);
 
    //setDownBaseline(start,end);
    //    setUpBaseline();
    //    setUpDetectionArea();
    return true;
}
 
void JudgmentRetrogradeTool::setYoloObjects(std::vector<ScoredRect> value) {
 
    mObjs = value;
 
}
 
 
void JudgmentRetrogradeTool::setPerRect(const long &id, cv::Rect rect) {
    setPerPoint(id, getCenterPoint(rect));
}
 
void JudgmentRetrogradeTool::setPerPoint(const long &id, cv::Point2f result) {
    auto &listCache = m_mapPerPoint[id];
    cv::Point2f vp;
    vp.x = result.x;
    vp.y = result.y;
    listCache.push_back(vp);
    if (listCache.size() >= 2) {
        VectorPoint tt;
        tt.start_Point = listCache.front();
        tt.end_Point = listCache.back();
        auto &temp = m_mapPerDirection[id];
        //#TODO tt.end_Point in UP Area
        QPoint center(tt.end_Point.x, tt.end_Point.y);
        if (m_polygon.containsPoint(center, Qt::OddEvenFill)) {
            temp.push_back(JRTOOL::low_filter(temp.back(), getTheta(m_UpBaseLine, tt)));
            //DBG("ID="<<id<<" left getTheta="<<getTheta(m_DownBaseLine, tt));
        } else {
            temp.clear();
        }
        listCache.clear();
    }
}
 
bool JudgmentRetrogradeTool::getPerRet(const long &id) {
    bool ret = (m_mapPerRet.find(id) == m_mapPerRet.end()) ? false :m_mapPerRet[id]>=m_sdkRule.nTriggerDelay * 8;
    return ret;
}
 
void JudgmentRetrogradeTool::setUpBaseline(cv::Point start, cv::Point end) {
    m_UpBaseLine.start_Point = start;
    m_UpBaseLine.end_Point = end;
}
 
 
void JudgmentRetrogradeTool::setDownBaseline(cv::Point start, cv::Point end) {
    m_DownBaseLine.start_Point = start;
    m_DownBaseLine.end_Point = end;
}
 
float JudgmentRetrogradeTool::getTheta(BaseLine &directionVectorPoint, VectorPoint &point) {
    return JRTOOL::getAngelOfTwoVector(directionVectorPoint, point);
}
 
void JudgmentRetrogradeTool::threadFunc() {
    func();
    fireConnectors();
}
 
void JudgmentRetrogradeTool::func() {
    if (!isInWeek(m_sdkRule.weekRuleVec))
        return;
    bool state=false;
    for (auto &scoredRect :mObjs) {
        if (scoredRect.score < m_sdkRule.fSensitivity)continue;
 
        if (getPerRet(scoredRect.id))//judgment.bool
        {
 
            DBG("scoredRect.id" << scoredRect.id << " val=" << m_mapPerRet[scoredRect.id]);
            m_mapPerRet[scoredRect.id] -=2400;
//            auto t_image = image(
//                CvUtil::zoomRectEqual(scoredRect.rect, 1.2, 1.2) & cv::Rect(0, 0, image.cols, image.rows)).clone();
            //  auto t_image = image(scoredRect.rect& cv::Rect(0, 0, image.cols, image.rows)).clone();
 
            if(pointArray==nullptr)  return;
            const cv::Point2i* ppt[1] = { pointArray };
 
            int npt[]={npts};
            cv::polylines(image, ppt,npt, 1, true,cv::Scalar(255,255,0));
            cv::arrowedLine(image,m_UpBaseLine.start_Point,m_UpBaseLine.end_Point,cv::Scalar(0,0,255));
 
            cv::rectangle(image, scoredRect.rect,  cv::Scalar(0, 0, 255), 2);
            std::string imgUrl = uploadImgToFdfs(image);
            saveInfoToEs(imgUrl, scoredRect);
            state=true;
 
 
           // break;
        }
//          m_triggerElement.triggerOnce();
//          if(m_triggerElement.getState())
//          {
 
//              auto t_image = image(
//                  CvUtil::zoomRectEqual(scoredRect.rect, 1.2, 1.2) & cv::Rect(0, 0, image.cols, image.rows)).clone();
//              //  auto t_image = image(scoredRect.rect& cv::Rect(0, 0, image.cols, image.rows)).clone();
//              std::string imgUrl = uploadImgToFdfs(t_image);
//              saveInfoToEs(imgUrl, scoredRect);
//          }
 
        setPerRect(scoredRect.id, scoredRect.rect);
    }
    m_triggerElement.setState(state);
    m_triggerElement.triggerOnce();
    for (auto &item : m_mapPerDirection) {
        int a = 0;
        int b = 0;
        auto &temp = item.second;
        for (auto &item2 : temp) {
            if (item2 > 100) {
                b++;
            } else if (item2 < 80) {
                a++;
            }
        }
        if(b!=0)
        {
            bool ret = a >= b ? true : false;
           //  DBG("id="<<item.first<<" a="<<a<<"  b="<<b<<" ret="<<ret);
            if (!ret) {
                m_mapPerRet[item.first]+=1;
               // m_mapPerRet.insert(std::make_pair(item.first, false));
                // m_mapPerRet[item.first] = false;
 
            }
        }
 
 
 
        if (temp.size() > 9) {
            temp.pop_front();
        }
    }
 
 
 
}
 
std::string JudgmentRetrogradeTool::uploadImgToFdfs(cv::Mat &image) {
    std::string strImgUrl = "http://";
    if (fdfsClient != nullptr && fdfsClient->fastFds != nullptr) {
        fdfsClient->rwLock.rdlock();
        std::vector<unsigned char> buffer;
        CvUtil::cvMat2Buffer(image, buffer);
        std::string strImgUrlTmp = "";
        fdfsClient->fastFds->uploadFile(buffer, strImgUrlTmp, "jpg");
      //  strImgUrl.append(fdfsClient->fastFds->getIp() + "/" + strImgUrlTmp);
        strImgUrl.clear();
        strImgUrl = strImgUrlTmp;
//                    strImgUrl.append("/").append(strImgUrlTmp);
        fdfsClient->rwLock.unlock();
    }
    return strImgUrl;
}
 
bool JudgmentRetrogradeTool::saveInfoToEs(const std::string &imgUrl, const ScoredRect &obj) {
 
 
    string str_uuid;
    uuid_t t_uuid;
    char str[36];
    uuid_generate(t_uuid);
    uuid_unparse(t_uuid, str);
    str_uuid = str;
 
 
    Json::Value t_json;
    t_json["Id"] = str_uuid; //主键
//#todo
 
    t_json["videoReqNum"] = m_sdkRule.strCamId.toStdString();
    t_json["sdkType"] = to_string(m_sdkRule.nSdkType);
//        t_json["Gender"] = obj.score;
 
    t_json["picName"] = "";// 抓拍照片名称
    t_json["content"] = "";// 内容描述
    t_json["personPicUrl"] = "";// 人员 地库图片
    t_json["ChannlId"] = getProperty("ch_id"); // 通道id
    t_json["likeDate"] = AppUtil::getTimeSecString(); // 比对时间
    t_json["picAddress"] = m_sdkRule.strAddr.toStdString();// 抓拍地址
    DBG("picAddress="<<m_sdkRule.strAddr.toStdString());
    t_json["picMaxUrl"] = ""; // 大图路径
 
 
    //   t_json["Age"] ="";
    t_json["picDate"] = obj.properties["time"]; // 抓拍时间,必须有
    t_json["picLocalUrl"] = "";  // 本地路径
    t_json["isDelete"] = "1";//默认1 ,0无效 1有效
 
    t_json["likePer"] = 1.0; // 相似值
 
    t_json["BaseName"] = "";// 地库名称
 
    t_json["videoNum"] = m_sdkRule.strCamId.toStdString();//Video设备编号
    t_json["picSmUrl"] = imgUrl; // 抓拍图片
    t_json["indeviceid"] = appPref.getStringData("fxDevID");;// 设备id
    t_json["idcard"] = "";
    t_json["personId"] = "";
    t_json["indevicename"] = appPref.getStringData("fxDevNAME");// 设备名称
    t_json["FaceFeature"] = "base64";
    t_json["personIsHub"] = m_triggerElement.getTriggerState() ? "1" : "4"; //1: 报警  2: 可疑  3: 安全  4: 未知
    t_json["videoIp"] = getProperty("local_ip");//当前服务器IP地址
    t_json["ack_alarm"] = m_triggerElement.getTriggerState() ? "0" : "";  //  que ren shi fou bao jing
    t_json["cluster_id"] = appPref.getStringData("clusterID");; // ji qun id
    std::string imgKey = obj.properties["imgKey"];
    t_json["imgKey"]=imgKey;
    INFO("SaveImageKey ToES:  "<<obj.properties["imgKey"]);
    try {
        auto server = m_rpcClient.getServer();
        if (!server)
        {
            ERR("server is null");
            //return false;
        }
        INFO("Record Video "<<imgKey);
        server->recordVideo(imgKey);
    }
    catch (std::exception &e)
    {
        ERR("Record Video Err: "<<imgKey <<"   Message: "<<e.what());
        //return false;
    }
 
    bool retface = false;
    if (pManagerEsDB)
        retface = pManagerEsDB->insertData("personaction", "perVideoAction", t_json.toStyledString(), str_uuid);
 
    if (retface) {
        INFO("perVideoAction db success");
    } else {
        ERR("personaction db fail");
    }
}
 
bool JudgmentRetrogradeTool::isInWeek(const std::vector<LActRuleWeekRecord> &ruleWeek) {
 
    int nWeek = QDate::currentDate().dayOfWeek();
    for (int i = 0; i < ruleWeek.size(); ++i) {
        if (ruleWeek[i].m_nType == nWeek) {
            QString strCurrent = QDateTime::currentDateTime().toString("hh:mm");
            if (strCurrent >= ruleWeek[i].m_strBegin && strCurrent <= ruleWeek[i].m_strEnd) {
                return true;
            }
        }
    }
    return false;
}
 
QJsonArray JudgmentRetrogradeTool::getJsonArrayFromQString(const QString strJson) {
    QJsonDocument jsonDocument = QJsonDocument::fromJson(strJson.toLocal8Bit());
    if (jsonDocument.isNull()) {
        qDebug() << "please check the string" << strJson.toLocal8Bit();
        return QJsonArray();
    }
    QJsonArray jsonArray = jsonDocument.array();
    return jsonArray;
}
 
void JudgmentRetrogradeTool::setUpDetectionArea() {
 
}
 
void JudgmentRetrogradeTool::setDownDetectionArea() {
 
}
 
bool JudgmentRetrogradeTool::getTriggerState() const {
    return m_triggerElement.getTriggerState();
}
 
void JudgmentRetrogradeTool::setImage(const cv::Mat &value) {
    value.copyTo(image);
    if(!m_bSetWH)
    {
        setMask(m_area,m_line);
        m_bSetWH=true;
    }
}