-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathplatefile.cpp
134 lines (105 loc) · 2.94 KB
/
platefile.cpp
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
#include <QtCore>
#include <fstream>
#include "platefile.h"
PlateFile::PlateFile(const QString &platePath)
: m_plateFile(platePath)
, m_parsed(false)
{
if(!exists())
return;
try
{
m_yaml = YAML::LoadFile(QDir::toNativeSeparators(m_plateFile).toStdString());
}
catch(...)
{
qWarning("Error parsing file '%s'", qPrintable(m_plateFile));
return;
}
if(!m_yaml["plate_corners_gt"])
return;
// cache polygon (8 x,y numbers)
int x, y;
QPolygon polygon;
QString corners = QString::fromStdString(m_yaml["plate_corners_gt"].as<std::string>());
QTextStream ts(&corners, QIODevice::ReadOnly);
while(true)
{
ts.skipWhiteSpace();
if(ts.atEnd())
break;
ts >> x >> y;
polygon.append(QPoint(x, y));
}
if(polygon.size() == 4)
{
m_plateCorners = polygon;
m_parsed = true;
}
else
qWarning("Polygon in file '%s' has only %d point(s) thus it's invalid", qPrintable(m_plateFile), polygon.size());
}
PlateFile::PlateFile(const QString &platePathTemplate, int index)
: PlateFile(platePathTemplate + QString("-%1.yaml").arg(index))
{
m_plateFileTemplate = platePathTemplate;
}
bool PlateFile::writeToNumberedFile(int index, QString *error)
{
const QString &yamlPath = m_plateFileTemplate + QString("-%1.yaml").arg(index);
qDebug("Writing INDEXED YAML %d '%s'", index, qPrintable(yamlPath));
return writeToFile(yamlPath, error);
}
bool PlateFile::writeToStandaloneFile(QString *error)
{
qDebug("Writing HEAP YAML '%s'", qPrintable(plateFile()));
return writeToFile(m_plateFile, error);
}
bool PlateFile::exists()
{
return QFileInfo(m_plateFile).exists();
}
void PlateFile::setPlateCorners(const QPolygon &plateCorners)
{
m_plateCorners = plateCorners;
QStringList cornersStringList;
cornersStringList.reserve(m_plateCorners.size() * 2);
foreach(const QPoint &point, m_plateCorners)
{
cornersStringList.append(QString::number(point.x()));
cornersStringList.append(QString::number(point.y()));
}
m_yaml["plate_corners_gt"] = cornersStringList.join(" ").toStdString();
}
// static
PlateFileList PlateFile::fromImageFile(const QString &fileTemplate)
{
PlateFileList plateList;
int index = 0;
// find associated car-nnn.yaml files
while(true)
{
PlateFile plateFile(fileTemplate, index++);
if(!plateFile.exists())
break;
if(plateFile.isValid())
plateList.append(plateFile);
}
return plateList;
}
bool PlateFile::writeToFile(const QString &yamlPath, QString *error)
{
const std::string &yamlFilePath = yamlPath.toStdString();
try
{
std::ofstream fout(yamlFilePath);
fout << m_yaml;
}
catch(...)
{
if(error)
*error = QObject::tr("Cannot write into a file");
return false;
}
return true;
}