派生自 development/c++

pansen
2018-12-17 ac00f7d508e020e2e5d1a0d497f43155ed34e6a6
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
//
// Created by basic on 18-8-24.
//
 
#include "JudgmentRetrogradeTool.h"
#include <basic/util/opencv/CvMath.hpp>
#include <QtCore/QDebug>
 
JudgmentRetrogradeTool::JudgmentRetrogradeTool():
TimerElement(1000),
m_triggerElement(0,50)
{
 
}
 
JudgmentRetrogradeTool::~JudgmentRetrogradeTool() {
 
}
 
bool JudgmentRetrogradeTool::init(QString area,QString line) {
    //#todo string ->json
    QJsonArray arrayAreas = getJsonArrayFromQString(area);
    if(arrayAreas.isEmpty())
    {
        return false;//do not detect
    }
    QVector<QPoint> vec;
    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 ;
        vec.push_back(QPoint(x,y));
    }
 
 
    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").toInt() ;
        start.y = obj.value("y").toInt() ;
 
 
        QJsonValue jsonValue2 = arrayLine[1];
        QJsonObject obj2 = jsonValue2.toObject();
        end.x = obj2.value("x").toInt() ;
        end.y = obj2.value("y").toInt() ;
 
    }else{
        return false;
    }
    setUpBaseline(start,end);
 
    //setDownBaseline(start,end);
    //    setUpBaseline();
    //    setUpDetectionArea();
    return true;
}
 
void JudgmentRetrogradeTool::setYoloObjects(std::vector<ScoredRect> value)
{
    mObjs=value;
    for(auto & item :value){
        setPerRect(item.id,item.rect);
    }
}
 
 
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()) ? true : m_mapPerRet[id];
    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::timerFunc()
{
    func();
    fireConnectors();
}
 
void JudgmentRetrogradeTool::func() {
    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++;
            }
        }
       // DBG("id="<<item.first<<" a="<<a<<"  b="<<b);
        bool ret = a >= b ? true : false;
        m_mapPerRet[item.first] = ret;
        if (temp.size() > 9) {
            temp.pop_front();
        }
    }
 
    for (auto scoredRect:mObjs) {
        if (!getPerRet(scoredRect.id))//judgment.bool
        {
            m_triggerElement.setState(true);
            break;
        }
        else
        {
            m_triggerElement.setState(false);
 
        }
    }
    m_triggerElement.triggerOnce();
}
 
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();
}