Skip to content

Commit c13a2be

Browse files
authored
Merge pull request #14 from andywang0607/load-exist-annotation
Load exist annotation
2 parents cd419fc + 5deed78 commit c13a2be

File tree

7 files changed

+98
-16
lines changed

7 files changed

+98
-16
lines changed

AutoAnnotationTool/CV/cvmodule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void CVModule::SetContours(QVector<LabelData*> &dataVec, int labelIdx, std::vect
7373
QPoint resultPoint = QPoint(tmp.x*(1.0f/factor),tmp.y*(1.0f/factor));
7474
tmpPoly.push_back(resultPoint);
7575
}
76-
dataVec[labelIdx]->resultPoly = tmpPoly;
76+
dataVec[labelIdx]->poly = tmpPoly;
7777
qDebug()<< Q_FUNC_INFO << "end";
7878
}
7979

AutoAnnotationTool/datasaver.cpp

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ void DataSaver::SaveData(int mode)
119119
polyInfo.insert("group_id",QJsonValue::Null);
120120
polyInfo.insert("shape_type","polygon");
121121
QJsonArray ptArray;
122-
int ptNum = labelCollector()->dataVec().at(i)->resultPoly.size();
122+
int ptNum = labelCollector()->dataVec().at(i)->poly.size();
123123
for(int j = 0; j<ptNum; ++j){
124124
QJsonArray curPtArray;
125-
curPtArray.append(labelCollector()->dataVec().at(i)->resultPoly.at(j).x() * labelCollector()->getFactorScaled());
126-
curPtArray.append(labelCollector()->dataVec().at(i)->resultPoly.at(j).y() * labelCollector()->getFactorScaled());
125+
curPtArray.append(labelCollector()->dataVec().at(i)->poly.at(j).x() * labelCollector()->getFactorScaled());
126+
curPtArray.append(labelCollector()->dataVec().at(i)->poly.at(j).y() * labelCollector()->getFactorScaled());
127127
ptArray.append(curPtArray);
128128
}
129129
polyInfo.insert("points",ptArray);
@@ -140,6 +140,50 @@ void DataSaver::SaveData(int mode)
140140
return;
141141
}
142142

143+
void DataSaver::LoadData(int mode)
144+
{
145+
QFileInfo fi(GetSavingPath());
146+
if(!fi.exists()) return;
147+
QFile file(GetSavingPath());
148+
file.open(QIODevice::ReadOnly | QIODevice::Text);
149+
QJsonParseError JsonParseError;
150+
QJsonDocument JsonDocument = QJsonDocument::fromJson(file.readAll(), &JsonParseError);
151+
file.close();
152+
153+
QJsonObject rootObject = JsonDocument.object();
154+
QJsonValueRef ref = rootObject.find("shapes").value();
155+
QJsonObject m_addvalue = ref.toObject();
156+
QJsonArray shapeAll = ref.toArray();
157+
158+
QStringList labelName;
159+
160+
for (const auto& element : shapeAll) {
161+
QString label = element.toObject()["label"].toString();
162+
if(!labelName.contains(label)) labelName.push_back(label);
163+
QString shapeType = element.toObject()["shape_type"].toString();
164+
QJsonArray pointArray = element.toObject()["points"].toArray();
165+
if(shapeType == "rectangle"){
166+
QPointF tl(pointArray[0].toArray()[0].toDouble() / labelCollector()->getFactorScaled(),
167+
pointArray[0].toArray()[1].toDouble() / labelCollector()->getFactorScaled());
168+
QPointF br(pointArray[1].toArray()[0].toDouble() / labelCollector()->getFactorScaled(),
169+
pointArray[1].toArray()[1].toDouble() / labelCollector()->getFactorScaled());
170+
QRectF tmpRect(tl, br);
171+
labelCollector()->appendData(tmpRect, label);
172+
}
173+
else if(shapeType == "polygon"){
174+
QPolygonF tmpPoly;
175+
for (const auto& point : pointArray) {
176+
QJsonArray pointArray = point.toArray();
177+
QPointF polyPoint(pointArray[0].toDouble() / labelCollector()->getFactorScaled(),
178+
pointArray[1].toDouble() / labelCollector()->getFactorScaled());
179+
tmpPoly.append(polyPoint.toPoint());
180+
}
181+
labelName.contains(label) ? labelCollector()->dataVec().at(labelName.indexOf(label))->poly.swap(tmpPoly) :
182+
labelCollector()->appendData(tmpPoly,label);
183+
}
184+
}
185+
}
186+
143187
void DataSaver::setLabelCollector(LabelCollector *labelCollector)
144188
{
145189
if (m_labelCollector == labelCollector)

AutoAnnotationTool/datasaver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class DataSaver : public QObject
1313
LabelCollector * labelCollector() const;
1414
public:
1515
Q_INVOKABLE void SaveData(int mode); //0:all, 1:rectangle, 2:polygon
16+
Q_INVOKABLE void LoadData(int mode); //0:all, 1:rectangle, 2:polygon
1617
private:
1718
QString GetSavingPath();
1819
QString GetImagePath();

AutoAnnotationTool/labelcollector.cpp

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ void LabelCollector::paint(QPainter *painter){
5454
painter->drawRect((*iter)->rect);
5555
// Draw result polygon
5656
painter->setPen(m_penVec.at(3));
57-
painter->drawPolygon((*iter)->resultPoly);
57+
painter->drawPolygon((*iter)->poly);
5858

5959
// Draw result point
6060
painter->setPen(m_penVec.at(2));
61-
QVector<QPoint>::iterator pointIter;
61+
QVector<QPointF>::iterator pointIter;
6262
int dataIdx = std::distance(m_dataVec.begin(),iter);
63-
for(pointIter=(*iter)->resultPoly.begin();pointIter!=(*iter)->resultPoly.end();pointIter++){
64-
int pointIdx = std::distance((*iter)->resultPoly.begin(),pointIter);
63+
for(pointIter=(*iter)->poly.begin();pointIter!=(*iter)->poly.end();pointIter++){
64+
int pointIdx = std::distance((*iter)->poly.begin(),pointIter);
6565
if(dataIdx == polySelectResult.boxIdx && pointIdx == polySelectResult.polyIdx){
6666
QRadialGradient gradient(*pointIter,5,*pointIter);
6767
gradient.setColorAt(0,QColor(220,118,51));
@@ -177,7 +177,8 @@ void LabelCollector::setImage(const QImage &image){
177177
m_image = image;
178178
if(image.width() != this->width() && image.height() != this->height()){
179179
m_imageScaled = image.scaled(this->width(), this->height(), Qt::KeepAspectRatio);
180-
factorScaled = (float)image.width() / (float)m_imageScaled.width();
180+
factorScaled = qMin((float)image.width() / (float)m_imageScaled.width(),
181+
(float)image.height() / (float)m_imageScaled.height());
181182
}
182183
else{
183184
factorScaled = 1;
@@ -187,10 +188,9 @@ void LabelCollector::setImage(const QImage &image){
187188
imageHeight = m_imageScaled.height();
188189
// Redraw the image
189190
update();
190-
emit imageChanged();
191191
// Remove all label while iamge change
192192
RemoveAllLabel();
193-
193+
emit imageChanged();
194194
}
195195

196196
void LabelCollector::setImgSrc(QString imgSrc)
@@ -261,7 +261,7 @@ void LabelCollector::mouseMoveEvent(QMouseEvent *event)
261261
m_lastPoint = event->localPos();
262262
PosBoundaryCheck(m_lastPoint);
263263
if(polySelectResult.isSelect){
264-
m_dataVec.at(polySelectResult.boxIdx)->resultPoly.setPoint(polySelectResult.polyIdx, m_lastPoint.toPoint());
264+
m_dataVec.at(polySelectResult.boxIdx)->poly[polySelectResult.polyIdx] = m_lastPoint;
265265
}
266266
else if(!polySelectResult.isSelect && rectCornerSelectResult.isSelect){
267267
switch (rectCornerSelectResult.corner){
@@ -442,8 +442,8 @@ void LabelCollector::GetPolygonSelectResult(QPointF currentPos)
442442
PolygonSelectResult res;
443443
QVector<LabelData*>::iterator it;
444444
for(it=m_dataVec.begin(); it!=m_dataVec.end(); it++){
445-
QPolygon checkedPoly = ((*it)->resultPoly);
446-
QPolygon::iterator polyIter;
445+
QPolygonF checkedPoly = ((*it)->poly);
446+
QPolygonF::iterator polyIter;
447447
for(polyIter = checkedPoly.begin(); polyIter != checkedPoly.end(); polyIter++){
448448
if(DistanceBetween2Point(*polyIter,currentPos) < thresDistance){
449449
polySelectResult.boxIdx = std::distance(m_dataVec.begin(), it);
@@ -565,6 +565,31 @@ void LabelCollector::appendData(QRectF rect)
565565
emit postItemAppended();
566566
}
567567

568+
void LabelCollector::appendData(QRectF rect, QString labelClass)
569+
{
570+
if(!(qAbs(rect.width())>2 && qAbs(rect.height())>2)) return;
571+
emit preItemAppended();
572+
LabelData *tmp = new LabelData(rect, labelClass);
573+
m_dataVec.push_back(tmp);
574+
emit postItemAppended();
575+
}
576+
577+
void LabelCollector::appendData(QPolygonF poly, QString labelClass)
578+
{
579+
emit preItemAppended();
580+
LabelData *tmp = new LabelData(poly, labelClass);
581+
m_dataVec.push_back(tmp);
582+
emit postItemAppended();
583+
}
584+
585+
void LabelCollector::appendData(QRectF rect, QPolygonF poly, QString labelClass)
586+
{
587+
emit preItemAppended();
588+
LabelData *tmp = new LabelData(rect, poly, labelClass);
589+
m_dataVec.push_back(tmp);
590+
emit postItemAppended();
591+
}
592+
568593
void LabelCollector::setFileIdx(int fileIdx)
569594
{
570595
if (m_fileIdx == fileIdx)

AutoAnnotationTool/labelcollector.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public slots:
6565
void setImgSrc(QString imgSrc);
6666
public slots:
6767
void appendData(QRectF rect);
68+
void appendData(QRectF rect, QString labelClass);
69+
void appendData(QPolygonF poly, QString labelClass);
70+
void appendData(QRectF rect, QPolygonF poly, QString labelClass);
6871

6972
void setFileIdx(int fileIdx);
7073

AutoAnnotationTool/labeldata.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ struct LabelData{
1010
public:
1111
LabelData(const QRectF &m_rect):
1212
labelClass(""), rect(m_rect), penIdx(0), isSelect(false){}
13+
LabelData(const QRectF &m_rect, const QString m_labelClass):
14+
labelClass(m_labelClass), rect(m_rect), penIdx(0), isSelect(false){}
15+
LabelData(const QPolygonF &m_poly, const QString m_labelClass):
16+
labelClass(m_labelClass), poly(m_poly), penIdx(0), isSelect(false){}
17+
LabelData(const QRectF &m_rect, const QPolygonF &m_poly, const QString m_labelClass):
18+
labelClass(m_labelClass), rect(m_rect), poly(m_poly), penIdx(0), isSelect(false){}
1319
LabelData(const LabelData &e)
1420
{
1521
labelClass = e.labelClass;
1622
penIdx = e.penIdx;
1723
rect = e.rect;
1824
isSelect = e.isSelect;
19-
resultPoly = QPolygon(e.resultPoly);
25+
poly = QPolygonF(e.poly);
2026
}
2127
~LabelData()
2228
{
@@ -25,7 +31,7 @@ struct LabelData{
2531
int penIdx; //0: normal, 1:highlight
2632
bool isSelect;
2733
QRectF rect;
28-
QPolygon resultPoly; // record scaled point show in GUI
34+
QPolygonF poly; // record scaled point show in GUI
2935
};
3036

3137
#endif // LABELDATA_H

AutoAnnotationTool/main.qml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ Window {
9292
onHeightChanged: {
9393
labelCollector.setImage(labelCollector.image)
9494
}
95+
onImageChanged: {
96+
dataSaver.LoadData(0)
97+
}
9598
}
9699
Rectangle{
97100
id: listRect

0 commit comments

Comments
 (0)