forked from MechatronicsBlog/TensorFlowQtVPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auxutils.cpp
299 lines (243 loc) · 7.95 KB
/
auxutils.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
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
#include "auxutils.h"
#include <QApplication>
#include <QScreen>
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QCryptographicHash>
#include <QFileInfo>
#include <QDir>
#include <QStandardPaths>
#include <QDebug>
#include "colormanager.h"
#ifdef Q_OS_ANDROID
const QString assetsPath = "assets:/assets";
#else
const QString assetsPath = "assets";
#endif
QString AuxUtils::getAssetsPath()
{
return assetsPath;
}
QString AuxUtils::resolveModelFilePath(QString modelName)
{
QString file = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + QDir::separator() + modelName;
QString def = assetsPath + QDir::separator() + modelName;
return copyIfNotExistOrUpdate(file,def);
}
QString AuxUtils::urlToFile(QString path)
{
QUrl url(path);
if ( url.isValid() && url.isLocalFile())
return url.toLocalFile();
return path;
//return path.replace("file://","");
}
bool AuxUtils::fileExist(QString filename)
{
QString file = urlToFile(filename);
if (file.trimmed().isEmpty()) return false;
return QFile::exists(file);
}
QByteArray AuxUtils::fileMD5(QString filename)
{
QCryptographicHash md5gen(QCryptographicHash::Md5);
QFile file(filename);
if (file.open(QIODevice::ReadOnly))
{
md5gen.addData(file.readAll());
file.close();
return md5gen.result().toHex();
}
return "";
}
QString AuxUtils::copyIfNotExistOrUpdate(QString file, QString defFile)
{
bool copy = false;
if (!fileExist(file))
{
QFileInfo fInfo(file);
QDir fdir = fInfo.absoluteDir();
if(!fdir.exists())
if (!fdir.mkpath(fdir.absolutePath()))
return "";
copy = true;
}
else
{
QByteArray md5source = fileMD5(defFile);
QByteArray md5dest = fileMD5(file);
if (md5source != md5dest)
copy = true;
}
if(copy && !copyFile(defFile,file)) return "";
qDebug() << "File OK:" << file;
return file;
}
void AuxUtils::deleteFile(QString filename)
{
QFile file(urlToFile(filename));
file.close();
file.remove() ? qDebug() << "File deleted: " + filename : qDebug() << "Error deleting file: " + filename + ", " +file.errorString();
}
bool AuxUtils::copyFile(QString origin, QString destination)
{
QString o = urlToFile(origin);
QString d = urlToFile(destination);
if (QFile::exists(d)) deleteFile(d);
if (QFile::copy(o,d))
{
qDebug() << "File copied from: " << o << ", to:" << d;
return true;
}
else
{
qDebug() << "File could NOT be copied from: " << o << ", to:" << d;
return false;
}
}
bool AuxUtils::isBGRvideoFrame(QVideoFrame f)
{
return f.pixelFormat() == QVideoFrame::Format_BGRA32 ||
f.pixelFormat() == QVideoFrame::Format_BGRA32_Premultiplied ||
f.pixelFormat() == QVideoFrame::Format_BGR32 ||
f.pixelFormat() == QVideoFrame::Format_BGR24 ||
f.pixelFormat() == QVideoFrame::Format_BGR565 ||
f.pixelFormat() == QVideoFrame::Format_BGR555 ||
f.pixelFormat() == QVideoFrame::Format_BGRA5658_Premultiplied;
}
QImage AuxUtils::rotateImage(QImage img, double rotation)
{
QPoint center = img.rect().center();
QMatrix matrix;
matrix.translate(center.x(), center.y());
matrix.rotate(rotation);
return img.transformed(matrix);
}
double AuxUtils::dpi()
{
return QApplication::screens().count() > 0 ? QApplication::screens().first()->logicalDotsPerInch() : 160;
}
// FIXME: Font size in video frames
double AuxUtils::sp(double pixel)
{
return pixel * (dpi() / 160) * qApp->devicePixelRatio();
}
QImage AuxUtils::drawText(QImage image, QRectF rect, QString text, Qt::AlignmentFlag pos, Qt::GlobalColor borderColor, double borderSize, Qt::GlobalColor fontColor, QFont font)
{
QPainter p;
QRectF r = rect;
QPainterPath path;
QPen pen;
QBrush brush;
QStringList lines;
if (p.begin(&image))
{
// Configure font
font.setPixelSize(AuxUtils::sp(FONT_PIXEL_SIZE_TEXT));
// Configure pen
pen.setWidthF(borderSize);
pen.setStyle(Qt::SolidLine);
pen.setColor(borderColor);
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
// Configure brush
brush.setStyle(Qt::SolidPattern);
brush.setColor(fontColor);
// Get lines
lines = text.split('\n',QString::SkipEmptyParts);
// Calculate text position
QFontMetrics fm(font);
for(int i=0;i<lines.count();i++)
{
// Calculate x0 and y0 positions
int x = ((r.width()) - fm.width(lines.at(i)))/2;
int y = pos == Qt::AlignBottom ? (r.height()) - fm.height()*(lines.count()-i) : (fm.height()*(i+1));
// Add text to path
path.addText(r.left()+x,r.top()+y,font,lines.at(i));
}
// Set pen, brush, font and draw path
p.setRenderHint(QPainter::Antialiasing);
p.setPen(pen);
p.setBrush(brush);
p.setFont(font);
p.drawPath(path);
}
return image;
}
QRectF AuxUtils::frameMatchImg(QImage img, QSize rectSize)
{
QSize isize = img.size();
rectSize.scale(isize, Qt::KeepAspectRatio);
QPoint center = img.rect().center();
return QRectF(center.x()-rectSize.width()*0.5,center.y()-rectSize.height()*0.5,rectSize.width(),rectSize.height());
}
QImage AuxUtils::drawBoxes(QImage image, QRect rect, QStringList captions, QList<double> confidences, QList<QRectF> boxes, double minConfidence, bool rgb)
{
Q_UNUSED(rect);
ColorManager cm;
QPainter p;
QBrush brush;
QPen pen;
QFont font;
QPen fPen;
QBrush bBrush;
QPen bPen;
if (p.begin(&image))
{
// Configure pen
pen.setStyle(Qt::SolidLine);
pen.setWidthF(LINE_WIDTH);
// Configure font pen
fPen.setStyle(Qt::SolidLine);
fPen.setColor(Qt::black);
// Configure back pen
bPen.setStyle(Qt::SolidLine);
// Configure brush
brush.setStyle(Qt::NoBrush);
// Configure back brush
bBrush.setStyle(Qt::SolidPattern);
// Configure font
font.setCapitalization(QFont::Capitalize);
font.setPixelSize(AuxUtils::sp(FONT_PIXEL_SIZE_BOX));
// Configure painter
p.setRenderHint(QPainter::Antialiasing);
p.setFont(font);
QFontMetrics fm(font);
// Draw each box
for(int i=0;i<boxes.count();i++)
{
// Check min confidence value
if (confidences[i] >= minConfidence)
{
// Draw box
cm.setRgb(rgb);
pen.setColor(cm.getColor(captions[i]));
p.setPen(pen);
p.setBrush(brush);
p.drawRect(boxes[i]);
// Format text
QString confVal = QString::number(qRound(confidences[i] * 100)) + " %";
QString text = captions[i] + " - " + confVal;
// Text rect
int width = fm.width(text)+FONT_WIDTH_MARGIN;
int height = fm.height();
int left = boxes[i].left()>=0 ? boxes[i].left() : boxes[i].right()-width;
int top = boxes[i].top()-fm.height()>=0 ? boxes[i].top()-fm.height() : boxes[i].bottom();
// Text position
int tLeft = left+FONT_WIDTH_MARGIN/2;
int tTop = boxes[i].top()-fm.height()>=0 ? boxes[i].top() - FONT_HEIGHT_MARGIN : boxes[i].bottom() + height - FONT_HEIGHT_MARGIN;
// Draw text background
bPen.setColor(pen.color());
bBrush.setColor(pen.color());
p.setPen(bPen);
p.setBrush(bBrush);
p.drawRect(left,top,width,height);
// Draw tex
p.setPen(fPen);
p.drawText(tLeft,tTop,text);
}
}
}
return image;
}