派生自 development/c++

pansen
2019-01-10 59408e43480b48b6acd6e76641ff1a97811e7ab5
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
#include "PerimeterElement.h"
//#include <basic/event/EventBus.hpp>
#include <basic/debug/Debug.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <basic/timer_counter/Clocktimer.h>
#include <QJsonDocument>
#include <QJsonObject>
#include <QString>
#include <basic/util/app/AppPreference.hpp>
#include <basic/db/Elasticsearch/EsDBTool.h>
#include <basic/util/opencv/CvUtil.h>
#include <uuid/uuid.h>
#include <jsoncpp/json/json.h>
PerimeterElement::PerimeterElement(int alarm_people_num):
m_nAlarmPeopleNum(alarm_people_num),
PipeElement(true),
trackingTrigger(nullptr),
mRealNum(0),
m_triggerElement(0,50)
{
 
    trackingTrigger= new TrackingTrigger(0.5);
 
 
}
 
PerimeterElement::~PerimeterElement()
{
    if(trackingTrigger)
    {
        delete trackingTrigger;
        trackingTrigger=nullptr;
    }
}
 
 
 
void PerimeterElement::threadInitial()
{
 
}
 
 
void PerimeterElement::threadFunc()
{
 
    int num=0;
    string detKey = getProperty("dev_id") + getProperty("ch_id")+getProperty("sdkType")+".det";
     float t_det_sc =0.75;
 
     t_det_sc= appPref.getFloatData(detKey) == -1 ? 0.75 : appPref.getFloatData(detKey);
 
 
//           t_det_sc= appPref.getFloatData(t_camIdex + "perimeter.det") == -1 ? 0.75 : appPref.getFloatData(t_camIdex + "perimeter.det");
 
 
    for(auto obj:m_objs){
            if(obj.type!=0) continue;
            //todo
            if(obj.prob < ((1-0.5)/2+0.5))continue;
 
//            ScoredRect scoredRect;
 
            int x = obj.rcObj.left * image.cols;
            int y = obj.rcObj.top * image.rows;
            int w = (obj.rcObj.right - obj.rcObj.left) * image.cols;
            int h = (obj.rcObj.bottom - obj.rcObj.top) * image.rows;
//            scoredRect.rect = cv::Rect(x, y, w, h);
//            scoredRect.score = scoredRect.rect.area() > 0 ? obj.prob : 0;
           // cv::Rect rec=obj.rect;
            QRect rect(x,y,w,h);
            QPoint center = rect.center();
            if(mPolygon.containsPoint(center,Qt::OddEvenFill))
            {
               // scoredRect.isMask=true;
                //if(bInWeekTime)
                   num++;
                  auto t_image = image(cv::Rect(x, y, w, h)& cv::Rect(0, 0, image.cols, image.rows)).clone();
                 std::string imgUrl=uploadImgToFdfs(t_image);
                 saveInfoToEs(imgUrl,obj);
            }
//            if(trackingTrigger->triggerOnce(scoredRect))
//            {
//                trackingTrigger->getLastRect().properties["id"] = to_string(scoredRect.id);
//                trackingTrigger->getLastRect().properties["type"] = cocoData[obj.type];
//            }
 
    }
    trackingTrigger->triggerLine();
    if(num>=m_nAlarmPeopleNum)
    {
        m_triggerElement.setState(true);
 
    }
    else
    {
        m_triggerElement.setState(false);
    }
    m_triggerElement.triggerOnce();
    mRealNum=num;
 
    fireConnectors();
}
 
 
 
 
 
 
void PerimeterElement::setSensitivity(float value)
{
    sensitivity = value;
}
 
 
 
void PerimeterElement::setObjsResults(const YoloDetect::ObjInfos &value)
{
    m_objs = value;
}
 
std::vector<ScoredRect> PerimeterElement::getLastScoreRects() const {
    return trackingTrigger->getLastScoreRects();
}
 
void PerimeterElement::setYoloObjects(std::vector<ScoredRect> value)
{
    mObjs=value;
}
 
int PerimeterElement::getRealNum() const
{
    //DBG("getRealNum "<<mRealNum);
    return mRealNum;
}
void PerimeterElement::setMask(std::string mask)
{
    QJsonArray arrayAreas = getJsonArrayFromQString(QString::fromStdString(mask));
    if(arrayAreas.isEmpty())
    {
        return;//do not detect
    }
    for(int i = 0;i < arrayAreas.size();++i)
    {
        QJsonValue jsonValue = arrayAreas[i];
        QJsonObject obj = jsonValue.toObject();
        int x = obj.value("x").toInt()*2;
        int y = obj.value("y").toInt()*2;
        mPolygon<<(QPoint(x,y));
 
    }
}
QJsonArray PerimeterElement::getJsonArrayFromQString(const QString& strJson)
{
    QJsonDocument jsonDocument = QJsonDocument::fromJson(strJson.toLocal8Bit());
    if( jsonDocument.isNull() ){
        //DBG("please check the string"<< strJson.toLocal8Bit());
        return QJsonArray();
    }
    QJsonArray jsonArray = jsonDocument.array();
    return jsonArray;
}
 
void PerimeterElement::setImage(const cv::Mat &value)
{
//    if (value.size != image.size) {
//        image = cv::Mat(value.rows, value.cols, CV_8UC3, sharedMemory->data());
//    }
//    value.copyTo(image);
}
bool PerimeterElement::getTriggerState() const {
    return m_triggerElement.getTriggerState();
}
std::string PerimeterElement::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 PerimeterElement::saveInfoToEs(const std::string& imgUrl,const ::YoloDetect::ObjInfo& obj)
{
 
        //#todo
        EsDBTool pManagerEsDB(appPref.getStringData("ipAdd"), appPref.getIntData("ipPort"));
 
        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["picName"] = "wait todo";
        t_json["DataType"] = cocoData[obj.type];
        t_json["Score"] = obj.prob;
 
        t_json["likeDate"] = AppUtil::getTimeSecString();//比较时间
        t_json["picAddress"] = getProperty("str_addr");//抓拍地址
        t_json["picSmUrl"] = imgUrl;//人员抓小图
        t_json["picDate"] = getProperty("time");
        t_json["viType"] = "2";//只有4种类型 1:personface 2:personbody 3:car 4:bicycle 5:none 未知类型
        t_json["personIsHub"] = "4";//1: 报警  2: 可疑  3: 安全  4: 未知
        t_json["videoIp"] = getProperty("local_ip");//当前服务器IP地址
 
 
        t_json["videoNum"] = getProperty("path");//Vide编号 外键
        t_json["videoReqNum"] = getProperty("dev_id");//Video设备编号
        t_json["ChannlId"] = getProperty("ch_id");//通道id
        t_json["isDelete"] = "1";//默认1 ,0无效 1有效
 
        t_json["indeviceid"] = appPref.getStringData("fxDevID");
        t_json["indevicename"] = appPref.getStringData("fxDevNAME");
        bool retface = false;
        std::string name=getProperty("sdkType")+"det";
        retface = pManagerEsDB.insertData(name, "info", t_json.toStyledString(), str_uuid);
        if (retface)
        {
            INFO(name<<"db success");
        }
        else
        {
            ERR(name<<"db fail");
        }
}